proffreda

lab05_extra.py

Oct 11th, 2016
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. from lab05 import *
  2.  
  3. ## Linked Lists ##
  4.  
  5. #Q5
  6.  
  7. def has_prefix(s, prefix):
  8. """Returns whether prefix appears at the beginning of linked list s.
  9.  
  10. >>> x = link(3, link(4, link(6, link(6))))
  11. >>> print_link(x)
  12. 3 4 6 6
  13. >>> has_prefix(x, empty)
  14. True
  15. >>> has_prefix(x, link(3))
  16. True
  17. >>> has_prefix(x, link(4))
  18. False
  19. >>> has_prefix(x, link(3, link(4)))
  20. True
  21. >>> has_prefix(x, link(3, link(3)))
  22. False
  23. >>> has_prefix(x, x)
  24. True
  25. >>> has_prefix(link(2), link(2, link(3)))
  26. False
  27. """
  28. "*** YOUR CODE HERE ***"
  29.  
  30. def has_sublist(s, sublist):
  31. """Returns whether sublist appears somewhere within linked list s.
  32.  
  33. >>> has_sublist(empty, empty)
  34. True
  35. >>> aca = link('A', link('C', link('A')))
  36. >>> x = link('G', link('A', link('T', link('T', aca))))
  37. >>> print_link(x)
  38. G A T T A C A
  39. >>> has_sublist(x, empty)
  40. True
  41. >>> has_sublist(x, link(2, link(3)))
  42. False
  43. >>> has_sublist(x, link('G', link('T')))
  44. False
  45. >>> has_sublist(x, link('A', link('T', link('T'))))
  46. True
  47. >>> has_sublist(link(1, link(2, link(3))), link(2))
  48. True
  49. >>> has_sublist(x, link('A', x))
  50. False
  51. """
  52. "*** YOUR CODE HERE ***"
  53.  
  54. def has_61A_gene(dna):
  55. """Returns whether linked list dna contains the CATCAT gene.
  56.  
  57. >>> dna = link('C', link('A', link('T')))
  58. >>> dna = link('C', link('A', link('T', link('G', dna))))
  59. >>> print_link(dna)
  60. C A T G C A T
  61. >>> has_61A_gene(dna)
  62. False
  63. >>> end = link('T', link('C', link('A', link('T', link('G')))))
  64. >>> dna = link('G', link('T', link('A', link('C', link('A', end)))))
  65. >>> print_link(dna)
  66. G T A C A T C A T G
  67. >>> has_61A_gene(dna)
  68. True
  69. >>> has_61A_gene(end)
  70. False
  71. """
  72. "*** YOUR CODE HERE ***"
  73.  
  74.  
  75. #Q6
  76.  
  77. def count_change(amount, denominations):
  78. """Returns the number of ways to make change for amount.
  79.  
  80. >>> denominations = link(50, link(25, link(10, link(5, link(1)))))
  81. >>> print_link(denominations)
  82. 50 25 10 5 1
  83. >>> count_change(7, denominations)
  84. 2
  85. >>> count_change(100, denominations)
  86. 292
  87. >>> denominations = link(16, link(8, link(4, link(2, link(1)))))
  88. >>> print_link(denominations)
  89. 16 8 4 2 1
  90. >>> count_change(7, denominations)
  91. 6
  92. >>> count_change(10, denominations)
  93. 14
  94. >>> count_change(20, denominations)
  95. 60
  96. """
  97. "*** YOUR CODE HERE ***"
Advertisement
Add Comment
Please, Sign In to add comment