proffreda

lab04.py

Sep 21st, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. # Q1
  2. def skip_add(n):
  3. """ Takes a number x and returns x + x-2 + x-4 + x-6 + ... + 0.
  4.  
  5. >>> skip_add(5) # 5 + 3 + 1 + 0
  6. 9
  7. >>> skip_add(10) # 10 + 8 + 6 + 4 + 2 + 0
  8. 30
  9. """
  10. "*** YOUR CODE HERE ***"
  11.  
  12.  
  13. # Q6
  14. def gcd(a, b):
  15. """Returns the greatest common divisor of a and b.
  16. Should be implemented using recursion.
  17.  
  18. >>> gcd(34, 19)
  19. 1
  20. >>> gcd(39, 91)
  21. 13
  22. >>> gcd(20, 30)
  23. 10
  24. >>> gcd(40, 40)
  25. 40
  26. """
  27. "*** YOUR CODE HERE ***"
  28.  
  29.  
  30. # Q7
  31. def hailstone(n):
  32. """Print out the hailstone sequence starting at n, and return the
  33. number of elements in the sequence.
  34.  
  35. >>> a = hailstone(10)
  36. 10
  37. 5
  38. 16
  39. 8
  40. 4
  41. 2
  42. 1
  43. >>> a
  44. 7
  45. """
  46. "*** YOUR CODE HERE ***"
  47.  
  48.  
  49. # Q8
  50. def fibonacci(n):
  51. """Return the nth fibonacci number.
  52.  
  53. >>> fibonacci(11)
  54. 89
  55. >>> fibonacci(5)
  56. 5
  57. >>> fibonacci(0)
  58. 0
  59. >>> fibonacci(1)
  60. 1
  61. """
  62. "*** YOUR CODE HERE ***"
  63.  
  64.  
  65. # Q9
  66. def paths(m, n):
  67. """Return the number of paths from one corner of an
  68. M by N grid to the opposite corner.
  69.  
  70. >>> paths(2, 2)
  71. 2
  72. >>> paths(5, 7)
  73. 210
  74. >>> paths(117, 1)
  75. 1
  76. >>> paths(1, 157)
  77. 1
  78. """
  79. "*** YOUR CODE HERE ***"
Advertisement
Add Comment
Please, Sign In to add comment