如何查看 Codility(和其他站点)测试的输入值?
How to see the input values for Codility's (and other sites') tests?
所以,我为 MaxProfit 练习编写了一个解决方案(link to the task's description) from lesson 9, in codility for programmers。我得到了 88% 的最终分数(代码在底部,但请随意阅读整个 post在这里我详细解释了我的问题是什么,我相信这是一个不知道如何深入阅读页面代码的问题,但也许无法从客户端访问这些信息,我不知道)。
这是我正在解决的第 25 个练习,之前的所有练习都是以 100% 的最终分数解决的,当然,我仍然拖着很多周前的这个疑问,关于如何查看实际测试 codility 的 运行(不仅仅是测试的标题和 got/expected 值),因为有时它不会显示,所以你不得不猜测,即使它总是让你知道您可能在哪里失败,在测试的标题中,以及当期望另一个代码时从您的代码中获得什么价值。
当您获得有关失败测试的详细信息时,这是页面向您抛出的非常详细的输出。
Analysis summary
The following issues have been detected: wrong answers.(This is always shown if there are problems)
[0, 2000] expected 2000 got 0
(You don't always get details about the input but you always get more details about the failing and succeeded tests at the bottom)
在此之后,您始终可以看到有关测试的以下详细信息(即使一切正常),这很有用,但有时还不够。
以下输出是我在本练习中的代码的(部分)结果(您实际上可以看到包含完整结果的页面 here)
Detected time complexity: O(N)
▶example length=6✔OK
▶simple_1 V-pattern> sequence, length=7✔OK
▶simple_desc descending and ascending sequence length=5✔OK
▶simple_empty empty and [0,200000] sequence✔OK
▶two_hills two increasing subsequences✘WRONG ANSWER
got 3000 expected 99000-> 1.0.020 WRONG ANSWER, got 3000 expected 99000
正如你在上次测试中看到的那样,我得到了一个错误的答案。但我没有看到有关输入的详细信息,也看不到它在我的代码中如何失败。它只说
two_hills two increasing subsequences and got 3000 expected 99000
但这还不够!我测试了 [2,3,7,22, 1, 22, 51]
作为输入,其中 returns 50,还有 [1, 22, 51, 2, 3, 7, 22]
,同样返回 50,这是两个递增的子序列,我说的对吗?所以我真的不明白。但是这个问题又是关于,我如何知道页面正在测试 two_hills
以及实际上所有其他测试的值(正如我之前所说,有时 显示测试失败时的输入,不总是,但它never 显示成功测试的输入)。
这个输入能在浏览器的控制台中看到吗?我找不到它。
最后,这是我的代码。
using System;
class Solution {
public int solution(int[] A) {
var maxProfit = 0;
if ( A.Length > 2)
{
var minBuyPrice = Math.Min(A[0], A[1]);
var maxSellPrice = minBuyPrice;
for (int i = 2; i < A.Length; i++)
{
if ( minBuyPrice > A[i])
{
minBuyPrice = A[i];
maxSellPrice = A[i];
}
maxSellPrice = Math.Max(maxSellPrice, A[i]);
maxProfit = Math.Max(maxProfit, maxSellPrice - minBuyPrice);
}
}
else if ( A.Length == 2)
{
return Math.Max(0, A[1] - A[0]);
}
return maxProfit;
}
}
我摆脱了那个丑陋的 else if
语句并获得了 100%,这是我的最终代码。
using System;
class Solution {
public int solution(int[] A) {
var maxProfit = 0;
if (A.Length >= 2)
{
var minBuyPrice = Math.Min(A[0], A[1]);
var maxSellPrice = Math.Max(0, A[1] - A[0]);
maxProfit = maxSellPrice;
for (int i = 2; i < A.Length; i++)
{
if (minBuyPrice > A[i])
{
minBuyPrice = A[i];
maxSellPrice = A[i];
}
maxSellPrice = Math.Max(maxSellPrice, A[i]);
maxProfit = Math.Max(maxProfit, maxSellPrice - minBuyPrice);
}
}
return maxProfit;
}
}
所以,我为 MaxProfit 练习编写了一个解决方案(link to the task's description) from lesson 9, in codility for programmers。我得到了 88% 的最终分数(代码在底部,但请随意阅读整个 post在这里我详细解释了我的问题是什么,我相信这是一个不知道如何深入阅读页面代码的问题,但也许无法从客户端访问这些信息,我不知道)。
这是我正在解决的第 25 个练习,之前的所有练习都是以 100% 的最终分数解决的,当然,我仍然拖着很多周前的这个疑问,关于如何查看实际测试 codility 的 运行(不仅仅是测试的标题和 got/expected 值),因为有时它不会显示,所以你不得不猜测,即使它总是让你知道您可能在哪里失败,在测试的标题中,以及当期望另一个代码时从您的代码中获得什么价值。
当您获得有关失败测试的详细信息时,这是页面向您抛出的非常详细的输出。
Analysis summary
The following issues have been detected: wrong answers.(This is always shown if there are problems)
[0, 2000] expected 2000 got 0
(You don't always get details about the input but you always get more details about the failing and succeeded tests at the bottom)
在此之后,您始终可以看到有关测试的以下详细信息(即使一切正常),这很有用,但有时还不够。
以下输出是我在本练习中的代码的(部分)结果(您实际上可以看到包含完整结果的页面 here)
Detected time complexity: O(N)
▶example length=6✔OK
▶simple_1 V-pattern> sequence, length=7✔OK
▶simple_desc descending and ascending sequence length=5✔OK
▶simple_empty empty and [0,200000] sequence✔OK
▶two_hills two increasing subsequences✘WRONG ANSWER
got 3000 expected 99000-> 1.0.020 WRONG ANSWER, got 3000 expected 99000
正如你在上次测试中看到的那样,我得到了一个错误的答案。但我没有看到有关输入的详细信息,也看不到它在我的代码中如何失败。它只说
two_hills two increasing subsequences and got 3000 expected 99000
但这还不够!我测试了 [2,3,7,22, 1, 22, 51]
作为输入,其中 returns 50,还有 [1, 22, 51, 2, 3, 7, 22]
,同样返回 50,这是两个递增的子序列,我说的对吗?所以我真的不明白。但是这个问题又是关于,我如何知道页面正在测试 two_hills
以及实际上所有其他测试的值(正如我之前所说,有时 显示测试失败时的输入,不总是,但它never 显示成功测试的输入)。
这个输入能在浏览器的控制台中看到吗?我找不到它。
最后,这是我的代码。
using System;
class Solution {
public int solution(int[] A) {
var maxProfit = 0;
if ( A.Length > 2)
{
var minBuyPrice = Math.Min(A[0], A[1]);
var maxSellPrice = minBuyPrice;
for (int i = 2; i < A.Length; i++)
{
if ( minBuyPrice > A[i])
{
minBuyPrice = A[i];
maxSellPrice = A[i];
}
maxSellPrice = Math.Max(maxSellPrice, A[i]);
maxProfit = Math.Max(maxProfit, maxSellPrice - minBuyPrice);
}
}
else if ( A.Length == 2)
{
return Math.Max(0, A[1] - A[0]);
}
return maxProfit;
}
}
我摆脱了那个丑陋的 else if
语句并获得了 100%,这是我的最终代码。
using System;
class Solution {
public int solution(int[] A) {
var maxProfit = 0;
if (A.Length >= 2)
{
var minBuyPrice = Math.Min(A[0], A[1]);
var maxSellPrice = Math.Max(0, A[1] - A[0]);
maxProfit = maxSellPrice;
for (int i = 2; i < A.Length; i++)
{
if (minBuyPrice > A[i])
{
minBuyPrice = A[i];
maxSellPrice = A[i];
}
maxSellPrice = Math.Max(maxSellPrice, A[i]);
maxProfit = Math.Max(maxProfit, maxSellPrice - minBuyPrice);
}
}
return maxProfit;
}
}