简体   繁体   English

Python调试中的Magic Square

[英]Magic Square in Python Debugging

Problem originally is in this link. 问题最初在链接中。 I wrote a Python code but I got 64 points (total points is 100) and this indicates that my code has some missing points. 我编写了Python代码,但得到了64分(总分是100),这表明我的代码有一些缺失点。 I passed 11 of 16 test cases but 5 test cases have problematic for me. 我通过了16个测试用例中的11个,但是5个测试用例对我来说有问题。 Could you say where my code has some missing points and how can I fix it? 您能否说一下我的代码中有哪些遗漏之处,以及如何解决?

import math

m = int(raw_input())
liste = []
y_liste = []
md = 0
ad = 0
sum = 0
sum2 = 0

for k in range(m):
    temp = str(raw_input())
    liste.append(temp)
    liste[k] = liste[k].split(" ")
    liste[k] = [int(i) for i in liste[k]]

for k in range(m):
    md += liste[k][k]
    ad += liste[k][m-k-1]



if md == ad:
    print 0
else:
    for k in range(m):
        for l in range(m):
            sum2 += liste[l][k]
            sum += liste[k][l]
        if sum2 != md and -(k+1) is not y_liste:
            y_liste.append(-(k+1))
        if sum != md and (k+1) is not y_liste:
            y_liste.append(k+1)
        sum2 = 0
        sum = 0

    if md != ad:
        y_liste.append(0)
    print len(y_liste)
    y_liste.sort()
    for i in y_liste:
        print i

Problem Statement 问题陈述

Magic Square 魔术广场

Johnny designed a magic square (square of numbers with the same sum for all rows, columns and diagonals ie both the main diagonal - meaning the diagonal that leads from the top-left corner towards bottom-right corner - and the antidiagonal - meaning the diagonal that leads from top-right corner towards bottom-left corner). 约翰尼(Johnny)设计了一个幻方(所有行,列和对角线都具有相同总和的数字平方,即主对角线-表示从左上角到右下角的对角线-反对角线-表示对角线从右上角到左下角)。 Write a program to test it. 编写程序进行测试。

Task 任务
Write a program that will check if the given square is magic (ie has the same sum for all rows, columns and diagonals). 编写一个程序,检查给定的正方形是否是魔术(即,所有行,列和对角线的总和都相同)。

Input 输入
First line: N , the size of the square (1 <= N <= 600). 第一行:N,正方形的大小(1 <= N <= 600)。 Next N lines: The square, N space separated integers pre line, representing the entries per each row of the square. 下N行:正方形,N个空格分隔的整数,位于前一行,表示正方形每一行的条目。

Output 产量
First line: M , the number of lines that do not sum up to the sum of the main diagonal (ie the one that contains the first element of the square). 第一行:M,总和不等于主对角线总和的行数(即包含平方的第一个元素的行)。 If the Square is magic, the program should output 0. Next M lines: A sorted (in incremental order ) list of the lines that do not sum up to the sum of the main diagonal. 如果Square是魔术,则程序应输出0。接下来的M行:不按总和的对角线排序的列表(按递增顺序)。 The rows are numbered 1,2,…,N; 行编号为1,2,…,N; the columns are numbered -1,-2,…,-N; 列编号为-1,-2,…,-N; and the antidiagonal is numbered zero. 反对角线编号为零。

Note: There is a newline character at the end of the last line of the output. 注意:在输出的最后一行的末尾有一个换行符。
Sample Input 1 样本输入1
3 3
8 1 6 8 1 6
3 5 7 3 5 7
4 9 2 4 9 2

Sample Output 1 样本输出1
0 0

Sample Input 2 样本输入2
4 4
16 3 2 13 16 3 2 13
5 10 11 8 5 10 11 8
6 9 7 12 6 9 7 12
4 15 14 1 4 15 14 1

Sample Output 2 样本输出2
3 3
-2 -2
-1 -1
0 0

Explanation of Sample Output 2 样本输出2的说明
The input square looks as follows: http://i.stack.imgur.com/JyMgc.png (Sorry for link but I cannot add image due to reputation) 输入方块如下所示: http : //i.stack.imgur.com/JyMgc.png (对不起,链接,但由于声誉我无法添加图像)

The square has 4 rows (labeled from 1 to 4 in orange) and 4 columns (labeled from -1 to -4 in green) as depicted in the image above. 如上图所示,正方形具有4行(以1到4标记为橙色)和4列(以-1到-4标记为绿色)。 The main diagonal and antidiagonal of the square are highlighted in red and blue respectively. 正方形的主要对角线和反对角线分别用红色和蓝色突出显示。

The main diagonal has sum = 16 + 10 + 7 +1 = 34. 主对角线的总和= 16 + 10 + 7 +1 = 34。
The antidiagonal has sum = 13 + 11 + 9 + 4 = 37. This is different to the sum of the main diagonal so value 0 corresponding to the antidiagonal should be reported. 对角线的总和= 13 + 11 + 9 + 4 =37。这与主对角线的总和不同,因此应报告对应于对角线的0值。
Row 1 has sum = 16 + 3 + 2 + 13 = 34. 第1行的总和= 16 + 3 + 2 + 13 = 34。
Row 2 has sum = 5 + 10 + 11 + 8 = 34. 第2行的总和= 5 + 10 + 11 + 8 = 34。
Row 3 has sum = 6 + 9 + 7 + 12 = 34. 第3行的总和= 6 + 9 + 7 + 12 = 34。
Row 4 has sum = 4 + 15 + 14 + 1 = 34. 第4行的总和= 4 + 15 + 14 + 1 = 34。
Column -1 has sum = 16 + 5 + 6 + 4 = 31. This is different to the sum of the main diagonal so value -1 should be reported. 列-1的总和= 16 + 5 + 6 + 4 =31。这与主对角线的总和不同,因此应报告值-1。
Column -2 has sum = 3 + 10 + 9 + 15 = 37. This is different to the sum of the main diagonal so value -2 should be reported. 列-2的总和= 3 + 10 + 9 + 15 =37。这与主对角线的总和不同,因此应报告值-2。
Column -3 has sum = 2 + 11 + 7 + 14 = 34. 列-3的总和= 2 + 11 + 7 + 14 = 34。
Column -4 has sum = 13 + 8 + 12 + 1 = 34. 列-4的总和= 13 + 8 + 12 + 1 = 34。
Based on the above, there are 3 lines that do not sum up to the sum of the elements of the main diagonal. 基于上述内容,有3条线的总和不等于主对角线元素的总和。 Since they should be sorted in incremental order, the output should be: 由于它们应按递增顺序排序,因此输出应为:
3 3
-2 -2
-1 -1
0 0

Your explanation doesn't discuss this clause which is a potential source of error: 您的解释没有讨论此子句,这可能是错误的来源:

if md == ad:
    print 0
else:

It says that if the main diagonal and antidiagonal add up to the same value, print just a 0 (no bad lines) indicating the magic square is valid (distinct from reporting a 0 in the list of bad lines). 它说,如果主对角线和反对角线的总和相同,则仅打印0(无坏线),表明幻方是有效的(与坏线列表中的0区别)。 Consider this valid magic square: 考虑这个有效的魔术方块:

 9  6  3 16
 4 15 10  5
14  1  8 11
 7 12 13  2

If I swap 13 and 11, the diagonals still equal each other but the square is invalid. 如果我交换13和11,对角线仍然彼此相等,但平方无效。 So the above code doesn't appear to be correct. 因此以上代码似乎不正确。 In the else clause for the above if statement, you test: 在上述if语句的else子句中,您测试:

if md != ad:
    y_liste.append(0)

a fact you already know to be true from the previous/outer test so your code seems to be out of agreement with itself. 从先前的/外部测试中您已经知道这是事实,因此您的代码似乎与自己不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM