Created
May 21, 2011 09:23
-
-
Save PyYoshi/984393 to your computer and use it in GitHub Desktop.
繰り返し系制御文の使用を禁止されているので再帰呼び出しによる実装を行った。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__author__ = 'Yoshihiro Misawa' | |
def recursion(int_list, cnt=0): | |
try: | |
xlen = len(int_list) -1 | |
if int_list[cnt] > 0: | |
result = int_list[cnt]**2 | |
else: | |
result = 0 | |
if cnt < xlen: | |
print 'cnt:', cnt, 'xlen:', xlen, 'result:', result | |
return result + recursion(int_list, cnt+1) | |
elif cnt == xlen: | |
print 'cnt:', cnt, 'xlen:', xlen, 'result:', result | |
return result | |
except TypeError, e: | |
print e | |
if __name__ == '__main__': | |
try: | |
print recursion([3, -1, 0, 1, 14, -15, 0, -0.5]) #206 | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment