diff options
author | Martin Možina <martin.mozina@fri.uni-lj.si> | 2016-10-17 12:31:13 +0200 |
---|---|---|
committer | Martin Možina <martin.mozina@fri.uni-lj.si> | 2016-10-17 12:31:13 +0200 |
commit | 6dbe02d844b81d455dda3086fc0f212f7abdca06 (patch) | |
tree | 44681eafeb49a26d174c6791eee575b16cc0a6b3 /python/problems/recursion/find_sum/find_sum.py | |
parent | 29bc34810cb1d1fbd208f6b5f2dad73188ca7f12 (diff) |
Added an exercise on recursion.
Added lecture notes for lists and for loop.
Diffstat (limited to 'python/problems/recursion/find_sum/find_sum.py')
-rw-r--r-- | python/problems/recursion/find_sum/find_sum.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/python/problems/recursion/find_sum/find_sum.py b/python/problems/recursion/find_sum/find_sum.py new file mode 100644 index 0000000..85f472b --- /dev/null +++ b/python/problems/recursion/find_sum/find_sum.py @@ -0,0 +1,19 @@ +def find_sum(xs, gs): + if gs < 0: + return False + if gs == 0: + return True + if not xs: + return False + return find_sum(xs[1:], gs-xs[0]) or find_sum(xs[1:], gs) + +print (find_sum([2,7,3,1,4], 10)) +print (find_sum([2,3,2,4], 10)) +print (find_sum([], 10)) +print (find_sum([1,2,3], 2)) +print (find_sum([1,2,3], 7)) +print (find_sum([2,7,3,1,4], 9)) +print (find_sum([2,7,3,2,4], 17)) + + + |