是什么导致三角形解决方案中我的总和中出现 NZEC(非零退出代码)错误? [英] What causes the NZEC (Non Zero Exit Code) error in my Sums in a Triangle solution?

查看:33
本文介绍了是什么导致三角形解决方案中我的总和中出现 NZEC(非零退出代码)错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 codechef 上做这个练习题.我已经在 C 中解决了这个问题,并试图在 Python 2.7 中做同样的事情.我在 codechef 判断上收到 NZEC 错误,即非零退出代码".我不明白为什么会发生这种情况.该程序在我的电脑上运行良好.什么样的极端情况会导致这个问题?

I was doing this practice problem on codechef. I have already solved this in C and was trying to do the same in Python 2.7. I am getting NZEC error on codechef judge which is "Non Zero Exit Code". I don't understand why this could be happening. The program is working fine on my computer. What kind of corner case can be giving this problem?

import sys
from itertools import islice

def p(): 
    cases = int(sys.stdin.readline())
    for case in xrange(cases):
        height = int(sys.stdin.readline())
        triangle = [map(int, i.split()) for i in islice(sys.stdin,height)]

        prev_row = triangle[0]
        for i in xrange(1, height):
            cur_row = triangle[i]

            cur_row[0] += prev_row[0]
            cur_row[len(cur_row) - 1] += prev_row[len(prev_row) - 1]

            for j in xrange(1, len(cur_row) - 1):
                if(prev_row[j - 1] > prev_row[j]):
                    cur_row[j] += prev_row[j - 1]
                else:
                    cur_row[j] += prev_row[j]

            prev_row = cur_row

        print max(prev_row)

p()

推荐答案

不要混合使用文件对象作为迭代器,并在对象上调用 .readline().

Don't mix using the file object as an iterator, and calling .readline() on the object.

通过在 sys.stdin 上使用 islice(),您将对象视为迭代器,在下面调用 file.next()兜帽.来自 .next() 文档:

By using islice() on sys.stdin you are treating the object as an iterator, calling file.next() under the hood. From the .next() documentation:

为了使 for 循环成为循环文件行的最有效方式(一种非常常见的操作),next() 方法使用了一个隐藏的预读缓冲区.由于使用了预读缓冲区,将 next() 与其他文件方法(如 readline())结合使用时无法正常工作.

In order to make a for loop the most efficient way of looping over the lines of a file (a very common operation), the next() method uses a hidden read-ahead buffer. As a consequence of using a read-ahead buffer, combining next() with other file methods (like readline()) does not work right.

解决方案是不使用 .readline() 不使用文件对象作为迭代器.在这种情况下,使用 next(sys.stdin) 而不是 sys.stdin.readline() 以一致地将对象用作迭代器.在任何情况下,这都比使用 .readline() 更有效:

The solution is to not use .readline() or not to use the file object as iterator. In this case, use next(sys.stdin) instead of sys.stdin.readline() to consistently use the object as an iterator. That is more efficient than using .readline() in any case:

def p(): 
    cases = int(next(sys.stdin))
    for case in xrange(cases):
        height = int(next(sys.stdin))
        triangle = [map(int, i.split()) for i in islice(sys.stdin, height)]

甚至:

def p(): 
    for case in xrange(int(next(sys.stdin))):
        triangle = [map(int, i.split()) for i in islice(sys.stdin, int(next(sys.stdin)))]

这篇关于是什么导致三角形解决方案中我的总和中出现 NZEC(非零退出代码)错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆