Skip to content

Instantly share code, notes, and snippets.

@PyYoshi
Created May 21, 2011 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PyYoshi/984393 to your computer and use it in GitHub Desktop.
Save PyYoshi/984393 to your computer and use it in GitHub Desktop.
繰り返し系制御文の使用を禁止されているので再帰呼び出しによる実装を行った。
#!/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