Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/anaconda/bin/python
- # coding: utf-8
- def for_sum(l):
- sum = 0
- for i in l:
- sum += i
- return sum
- def while_sum(l):
- sum = 0
- while len(l) > 0:
- sum += l.pop()
- return sum
- def recurr_sum(l):
- if len(l) == 0:
- return 0
- elif len(l) == 1:
- return l[0]
- else:
- return l[0] + recurr_sum(l[1:])
- def concat(l1, l2):
- ret = []
- while len(l1) > 0 or len(l2) > 0:
- ret.append(l2.pop())
- ret.append(l1.pop())
- ret.reverse()
- return ret
- def fib(n):
- ret = []
- ret.append(0)
- ret.append(1)
- for i in range(2, n):
- ret.append(ret[i-1] + ret[i-2])
- return ret[n-1]
- def max_digit(l):
- max = -1
- import itertools
- perms = itertools.permutations(l)
- for p in perms:
- s = ''
- for d in p:
- s += str(d)
- if max < int(s):
- max = int(s)
- return max
- def eval_100():
- import itertools
- ops = "+- "
- iter = itertools.product('1', ops, '2', ops, '3', ops, '4', ops,
- '5', ops, '6', ops, '7', ops, '8', ops, '9')
- for i in iter:
- ret = eval(''.join(i).replace(' ', ''))
- if ret == 100:
- print(''.join(i).replace(' ', ''))
- if __name__ == "__main__":
- l = [1, 2, 3, 4, 5]
- print(for_sum(l))
- l = [1, 2, 3, 4, 5]
- print(while_sum(l))
- l = [1, 2, 3, 4, 5]
- print(recurr_sum(l))
- l1 = [1, 2, 3, 4, 5]
- l2 = ["a", "b", "c", "d", "e"]
- print(concat(l1, l2))
- print(fib(101))
- print(max_digit([50, 2, 1, 9]))
- eval_100()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement