Starting with Python doing zoj1011 non-zero Exit code error.

Source: Internet
Author: User
Tags ord

Recently did the Zhejiang University OJ 1011th problem, met a strange thing. This problem I use C + + and PHP do, after the submission is correct. However, after rewriting the Python code with the same logic, the result of the Non-zero Exit code was generated. Most of the results from Python's findings indicate that the program has not caught an exception during execution. After repeated submissions, it is determined that the exception that is not caught is indexerror, which is the array subscript out-of-bounds exception. Further troubleshooting is determined due to the existence of a letter in the tree node of the test example that has more than the range of the number of parameters given in the first row. The workaround is to infer if the subscript is out of bounds before using the subscript as the index, and return 0 if it crosses the boundary.

But what makes me wonder is why this error is not reflected in C + + and PHP code? The reason is because in these languages there is no subscript to cross the argument! For example, the following C + + code is able to execute, absolutely no problem, just the result of the output may be indeterminate.

#include <iostream>using namespace Std;int main () {int a[2];cout << A[-1];return 0;}

Here the array name A is actually equivalent to a constant address value, and the A[-1] code represents an integer value represented by the content at the address where the address value is offset by an integer variable size. The following PHP code is also capable of executing:

<?php$a = [up];p rint $a [-1];? >

The result of the output is an empty string of strings, and perhaps in PHP, an empty string is returned by default for elements that are out of bounds.

In other words, C + + and PHP code can rely entirely on the "lucky coincidence", C + + in the cross-border address stored exactly null,php is returned empty string, this value in the subsequent inference just can make the function finally return the correct result. The fact is that in C + + and PHP code the same is required to check whether or not out of bounds!

This incident has made me realize once again the importance of proactively checking for anomalies and doing logical protection. In a more robust language like Python,java, exceptions to program execution can be detected and thrown on their own initiative. Languages like C and C + + are very likely to allow programs to "silently" execute in the wrong state, which can be very difficult to determine when the wrong result is obtained. Programming needs to be cautious, the user input data is what is possible, just as far as possible to consider a comprehensive, talent to avoid future bug occurrence.

Finally, I enclose the Python code of my zoj1011:

Import Sysclass React:leftsig = 0rightSig = 0def __init__ (self, Leftsig, rightsig): Self.leftsig = Leftsigself.rightsig = Rightsigreacttable = [[] for Col in range (+)] for row in range (a)]tree = [0 for x in range (2100)]signalnum = 0ac Ceptednum = 0elementNum = 0nodeN = 0def readtable (): Global reacttable, tree, Signalnum, Acceptednum, Elementnum, nodenfor I in range (Signalnum): to J in range (Elementnum): line = Sys.stdin.readline (). Strip () A = Line.split () reacttable[i][j] = [] For TMP in range (0, Len (a), 2): Reacttable[i][j].append (React (int (a[tmp)), int (a[tmp+1])) def readtree (level): Globa L reacttable, tree, Signalnum, Acceptednum, elementnum, Nodennoden = 0for i in range (level+1): line = Sys.stdin.readline (). Strip () A = Line.split () for tmp in A:tree[noden] = Tmpnoden + 1def Displaytableandtree (level): Global reacttable, tree, s Ignalnum, Acceptednum, Elementnum, Nodenprint "--------------------" for I in Range (Signalnum): for J in range (Elementnum) : For react in reacttable[i][j]:p rint react.leftsig, REACT.RIGHTSIG,PRINTC = 0for i in range (level+1): for J in Range (1<<i):p rint tree[c],c + = 1p Rintdef judge (Signal, ELEIDX): Global reacttable, tree, Signalnum, Acceptednum, Elementnum, nodenif eleidx >= Noden an D signal >= Signalnum-acceptednum:return 1if eleidx >= noden:return 0if tree[eleidx] = = ' * ' and signal >= SIGNALN Um-acceptednum:return 1if Tree[eleidx] < ' a ' or Tree[eleidx] >= chr (Ord (' a ') + Elementnum): Return 0for react in REAC Ttable[signal][ord (Tree[eleidx])-ord (' A '): #try: If Judge (React.leftsig, 2*eleidx+1) and judge (React.rightsig, eleidx+2): Return 1#except indexerror: #return 0return 0kcase = 1while 1:line = Sys.stdin.readline (). Strip () A = Line.split () signalnum = Int (a[0]) acceptednum = Int (a[1]) elementnum = Int (a[2]) if Signalnum = = 0 and Acceptednum = = 0 and Elementnum = = 0:breakif kcase > 1:printprint "nta%d:"% (kcase) readtable () while 1:line = Sys.stdin.readline (). Strip () level = Int (l INE) if level = = -1:break REAdtree (level) #displayTableAndTree (level) if Judge (0, 0): print "Valid" Else:print "Invalid" kcase + = 1 



Starting with Python doing zoj1011 non-zero Exit code error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.