【问题标题】:My solution to this programming challenge is wrong because it outputs the wrong answer for 10/11 test cases. What are those test cases?我对这个编程挑战的解决方案是错误的,因为它为 10/11 测试用例输出了错误的答案。那些测试用例是什么?
【发布时间】:2012-09-17 04:57:14
【问题描述】:

我正在做这个编程挑战,可以在 www.interviewstreet.com 找到(它的第一个挑战值 30 分)。

当我提交解决方案时,我收到了一个结果,说答案是错误的,因为它只通过了 1/11 的测试用例。但是,我觉得已经测试了各种案例并且不明白我做错了什么。知道这些测试用例可能是什么会很有帮助,这样我就可以测试我的程序。

这是一个问题(在下面的灰线之间):


象限查询(30 分)

平面上有 N 个点。第 i 个点具有坐标 (xi, yi)。执行以下查询:

1) 反射点 i 和 j 之间的所有点,包括沿 X 轴。此查询表示为“X i j”
2) 反射点 i 和 j 之间的所有点,包括沿 Y 轴。此查询表示为“Y i j”
3) 计算点 i 和 j 之间有多少点,包括位于 4 个象限中的每一个。此查询表示为“C i j”

输入:
第一行包含 N,点数。接下来是 N 行。
第 i 行包含用空格分隔的 xi 和 yi。
下一行包含 Q 查询数。接下来的 Q 行每行包含一个查询,采用上述形式之一。
所有索引都是 1 索引。

输出:
为“C i j”类型的每个查询输出一行。对应的行包含 4 个整数;分别在第 1、2、3 和 4 象限中具有 [i..j] 范围内索引的点数。

约束:
1 1 您可以假设 X 或 Y 轴上没有任何点。
所有 (xi,yi) 都适合 32 位有符号整数
在所有查询中,1

示例输入:
4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
是 1 2
C 1 3
样本输出:
1 1 1 1
1 1 0 0
0 2 0 1

说明:
当查询说“X i j”时,这意味着将索引 i 和 j 之间的所有点都包括并反映沿 X 轴的那些点。这里的 i 和 j 与点的坐标无关。它们是指数。 i 指 i 点,j 指 j 点

“C 1 4”要求您“考虑索引在 {1,2,3,4} 中的点集。在这些点中,有多少分别位于第一、第二、第三和第四四边形? 答案显然是 1 1 1 1。

接下来,我们沿 X 轴反映索引“2 4”之间的点。所以新坐标是:
1 1
-1 -1
-1 1
1 1

现在“C 3 4”是“考虑在 {3,4} 中具有索引的点集。在这些点中,有多少分别位于第一、第二、第三和第四四边形?点 3 位于象限 2,点 4 位于象限 1。 所以答案是 1 1 0 0


我正在使用 PHP 进行编码,测试方法是使用 STDIN 和 STDOUT。

有什么想法可以用来测试我的代码吗?我不明白为什么我会失败 10 / 11 个测试用例。

另外,如果你有兴趣,这里是我的代码:

// The global variable that will be changed
$points = array();

/******** Functions ********/
// This function returns the number of points in each quadrant. 
function C($beg, $end) {
    // $quad_count is a local array and not global as this gets reset for every C operation
    $quad_count = array("I" => 0, "II" => 0, "III" => 0, "IV" => 0);

    for($i=$beg; $i<$end+1; $i++) {
        $quad = checkquad($i);
        $quad_count[$quad]++;
    }

    return $quad_count["I"]." ".$quad_count["II"]." ".$quad_count["III"]." ".$quad_count["IV"];        
}

// Reflecting over the x-axis means taking the negative value of y for all given points
function X($beg, $end) {
    global $points;

    for($i=$beg; $i<$end+1; $i++) {
        $points[$i]["y"] = -1*($points[$i]["y"]);
    }
}

// Reflecting over the y-axis means taking the negative value of x for all given points    
function Y($beg, $end) {
    global $points;

    for($i=$beg; $i<$end+1; $i++) {
        $points[$i]["x"] = -1*($points[$i]["x"]);
    }
}

// Determines which quadrant a given point is in
function checkquad($i) {
    global $points;

    $x = $points[$i]["x"];
    $y = $points[$i]["y"];

    if ($x > 0) {
        if ($y > 0) {
            return "I";
        } else {
            return "IV";
        }
    } else {
        if ($y > 0) {
            return "II";
        } else {
            return "III";
        }
    }
}


// First, retrieve the number of points that will be provided. Make sure to check constraints.
$no_points = intval(fgets(STDIN));    
if ($no_points > 100000) {
    fwrite(STDOUT, "The number of points cannot be greater than 100,000!\n");
    exit;
}

// Remember the points are 1 indexed so begin key from 1. Store all provided points in array format. 
for($i=1; $i<$no_points+1; $i++) {
    global $points;

    list($x, $y) = explode(" ",fgets(STDIN)); // Get the string returned from the command line and convert to an array
    $points[$i]["x"] = intval($x);
    $points[$i]["y"] = intval($y);
}

// Retrieve the number of operations that will be provied. Make sure to check constraints. 
$no_operations = intval(fgets(STDIN));    
if($no_operations > 100000) {
    fwrite(STDOUT, "The number of operations cannot be greater than 100,000!\n");
    exit;
}

// Retrieve the operations, determine the type and send to the appropriate functions. Make sure i <= j.  
for($i=0; $i<$no_operations; $i++) {
    $operation = explode(" ",fgets(STDIN));
    $type = $operation[0];

    if($operation[1] > $operation[2]) {
        fwrite(STDOUT, "Point j must be further in the sequence than point i!\n");
        exit;
    }

    switch ($type) {
        case "C":
            $output[$i] = C($operation[1], $operation[2]);
            break;
        case "X":
            X($operation[1], $operation[2]);
            break;
        case "Y":
            Y($operation[1], $operation[2]);
            break;
        default:
            $output[$i] = "Sorry, but we do not recognize this operation. Please try again!";
    }
}

// Print the output as a string
foreach($output as $line) {
    fwrite(STDOUT, $line."\n");
}



更新: 我终于找到了一个我的程序失败的测试用例。现在我试图确定原因。这是关于大数测试的一个很好的教训。

10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
12
C 1 10
X 1 3
C 5 5
是 2 10
C 10 10
C 1 10
X 1 3
C 5 5
是 2 10
C 10 10
X 3 7
C 9 9
我将通过初始化一个错误数组并确定哪些操作导致问题来正确测试这一点。

【问题讨论】:

  • 您提供的问题链接返回错误。我想现在没有人能够回答。如果您有副本,请将其粘贴到您的问题中。这就是为什么我们总是建议不要依赖外部链接...
  • 好的,感谢您指出这一点。解决它。希望对您有所帮助并且仍然可读。
  • 你可能想稍微缩短一下标题。
  • 您能否将标题更改为与正在解决的问题更相关的我。你也可以清理格式吗?
  • 这样更清楚吗?哪些区域需要格式化?

标签: php algorithm testing


【解决方案1】:

我发现了一个失败的测试用例并理解了原因。我在这里发布这个答案,所以每个人都很清楚。

我对程序设置了一个约束,使得 j 必须大于 i,否则应该返回错误。我注意到以下测试用例有错误:

10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1
C 2 10

操作 C 返回的错误。本质上,程序认为“2”大于“10”。我发现的原因如下:

使用 fgets() 时,返回一个字符串。如果您在该行上执行诸如explode() 或substr() 之类的字符串操作,则您正在将该初始字符串中的数字再次转换为字符串。所以这意味着 10 变为“10”,然后在字符串操作之后变为“0”。

对此的一种解决方案是使用 sscanf() 函数并基本上告诉程序期待一个数字。示例:对于“C 2 10”,您可以使用:
$operation_string = fgets(STDIN);
list($type, $begpoint, $endpoint) = sscanf($operation_string, "%s %d %d");

我使用 sscanf() 提交了新的解决方案,现在已经通过了 3/11 的测试用例。由于超出了 CPU 时间限制,它不再检查任何测试用例。所以,现在我必须回去优化我的算法。

回去工作! :)

【讨论】:

  • 这是我在回答中告诉你的。 :P "2" &lt; "10""2" &gt; "10 "
  • 不,那是不同的。根据我的程序,“2”
  • 但我认为您接近正确的想法并为我指出了正确的方向。所以谢谢:)
【解决方案2】:

回答“那些测试用例是什么?”试试这个“解决方案”:

<?php
$postdata = http_build_query(
    array(
        'log' => file_get_contents('php://stdin')
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

file_get_contents('http://myserver/answer.php', false, $context);
?>

在您的服务器上:

<?php
$fp = fopen('/tmp/answers.log', 'a');
fputs($fp, $_POST['log']."\n");
fclose($fp);
?>

编辑:

我做到了。并提出这是您的主要问题(我认为):

$operation = explode(" ",fgets(STDIN));

将其更改为:

$operation = explode(" ",trim(fgets(STDIN)));

因为否则"9" &gt; "41 " 由于字符串比较。你应该在你读到一行的任何地方进行修复。

【讨论】:

  • 我认为多余的空格不是问题。我通过 STDIN 使用不同的变量进行了测试,并且输出更大。运行成功。
【解决方案3】:

据我猜测,此解决方案行不通。即使您解决了错误答案问题,解决方案也会超时。

我能够找到一种在 O(1) 时间内返回象限计数的方法。

但无法在更短的时间内进行反射。 :(

【讨论】:

  • 基本上,使用累积象限计数并在 O(1) 中为“C”查询返回它们。
  • 嗯,就算法而言,我想我总是可以回去优化。但首先,我试图找出测试用例失败的原因。我缺少的基本逻辑可能有问题。
  • 我猜它也显示相同的(“测试用例失败”)超时。他们不会让法官一直等待您的答复。
  • 好点尼格尔。由于发现一个错误,我再次测试,现在我能够通过 3/11 测试用例,但随后 CPU 时间限制超过了。现在,我需要专注于优化。
  • 您对我的代码有什么优化建议吗? O(1) 是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多