Programmer's Cold joke python version

Source: Internet
Author: User

In Bole online saw a cold joke, feel very interesting.

1 voidtellstory ()2 {3printf"there used to be a mountain \ n");4printf"There's a temple on the hill \ n");5printf"There's an old monk and a little monk in the temple.");6printf"One day \ n");7printf"the Little Monk said to the old monk \ n");8printf""Tell me a story." \ n");9printf"so the old monk said: \ n");Ten tellstory (); One}

The original code was written in C and tried to convert to Python:

1 #!/bin/env2 #Encoding:utf-83 4 deftellstory (CNT):5     Print "there used to be a mountain \ n"6     Print "There's a temple on the hill \ n"7     Print "There's an old monk and a little monk in the temple."8     Print "One day \ n"9     Print "the Little Monk said to the old monk \ n"Ten     Print ""Tell me a story." \ n" One     Print "so the old monk said: \ n" A     Print "-----------\ n%s years ago \ n"%CNT -CNT + = 2 - tellstory (CNT) the  - if __name__=='__main__': -Tellstory (1)

However, due to the Python compiler's limit on the maximum number of recursive nesting levels, this version does not perform well, so Google has the following versions:

#!/bin/env#Encoding:utf-8 fromSysImportMaxintdeftellstory (CNT): Rtn_str="'Rtn_str+="there used to be a mountain \ n"Rtn_str+="There's a temple on the hill \ n"Rtn_str+="There's an old monk and a little monk in the temple."Rtn_str+="One day \ n"Rtn_str+="the Little Monk said to the old monk \ n"Rtn_str+=""Tell me a story." \ n"Rtn_str+="so the old monk said: \ n"Rtn_str+="-----------\ n%s years ago \ n"%CNTyieldRtn_strif __name__=='__main__':     forIinchXrange (1, Maxint, 2):         forEinchtellstory (i):PrintE

Use Yield+xrange instead of Print+range to avoid memoryerror caused by too large intermediate variables.

This version will execute indefinitely until the maximum integer "9223372036854775807" of the py is reached or the system memory is low.

Summarize:

1. The function of the + + operator in Python differs greatly from other languages:

>>> i = 1
>>> ++i
1
>>> i++
File "<stdin>", line 1
i++
^
Syntaxerror:invalid syntax
>>>

To implement a self-add or decrement operation, it is best to use + = or-=.

2. In Python, the Range method generates a list directly for traversal or other operations, and xrange works like a generator that uses yield, and calculates a value to return each time it is called. Therefore, when traversing, the performance of xrange is better than range, especially in the case of huge data volume.

Programmer's Cold joke python version

Related Article

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.