Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #!/anaconda/bin/python
  2. # coding: utf-8
  3.  
  4. def for_sum(l):
  5. sum = 0
  6. for i in l:
  7. sum += i
  8. return sum
  9.  
  10. def while_sum(l):
  11. sum = 0
  12. while len(l) > 0:
  13. sum += l.pop()
  14. return sum
  15.  
  16. def recurr_sum(l):
  17. if len(l) == 0:
  18. return 0
  19. elif len(l) == 1:
  20. return l[0]
  21. else:
  22. return l[0] + recurr_sum(l[1:])
  23.  
  24. def concat(l1, l2):
  25. ret = []
  26. while len(l1) > 0 or len(l2) > 0:
  27. ret.append(l2.pop())
  28. ret.append(l1.pop())
  29. ret.reverse()
  30. return ret
  31.  
  32. def fib(n):
  33. ret = []
  34. ret.append(0)
  35. ret.append(1)
  36. for i in range(2, n):
  37. ret.append(ret[i-1] + ret[i-2])
  38. return ret[n-1]
  39.  
  40. def max_digit(l):
  41. max = -1
  42. import itertools
  43. perms = itertools.permutations(l)
  44. for p in perms:
  45. s = ''
  46. for d in p:
  47. s += str(d)
  48. if max < int(s):
  49. max = int(s)
  50. return max
  51.  
  52. def eval_100():
  53. import itertools
  54. ops = "+- "
  55. iter = itertools.product('1', ops, '2', ops, '3', ops, '4', ops,
  56. '5', ops, '6', ops, '7', ops, '8', ops, '9')
  57. for i in iter:
  58. ret = eval(''.join(i).replace(' ', ''))
  59. if ret == 100:
  60. print(''.join(i).replace(' ', ''))
  61.  
  62. if __name__ == "__main__":
  63. l = [1, 2, 3, 4, 5]
  64. print(for_sum(l))
  65. l = [1, 2, 3, 4, 5]
  66. print(while_sum(l))
  67. l = [1, 2, 3, 4, 5]
  68. print(recurr_sum(l))
  69.  
  70. l1 = [1, 2, 3, 4, 5]
  71. l2 = ["a", "b", "c", "d", "e"]
  72. print(concat(l1, l2))
  73.  
  74. print(fib(101))
  75.  
  76. print(max_digit([50, 2, 1, 9]))
  77. eval_100()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement