Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. # Question 8: Longest Repetition
  2.  
  3. # Define a procedure, longest_repetition, that takes as input a
  4. # list, and returns the element in the list that has the most
  5. # consecutive repetitions. If there are multiple elements that
  6. # have the same number of longest repetitions, the result should
  7. # be the one that appears first. If the input list is empty,
  8. # it should return None.
  9.  
  10. def longest_repetition(ls):
  11. if ls == []:
  12. return None
  13. appearance = 1
  14. max_number = 1
  15. e = ls[0]
  16. for i in range(1, len(ls)):
  17. if ls[i] == ls[i-1]:
  18. appearance += 1
  19. if appearance > max_number:
  20. max_number = appearance
  21. e = ls[i]
  22. else:
  23. appearance = 1
  24. return e
  25.  
  26. print longest_repetition([2, 2, 3, 3, 3])
  27. # Incorrect. Your procedure did not return
  28. # `3` for the input
  29. # `[2, 2, 3, 3, 3]`.
  30.  
  31. #For example,
  32.  
  33. # print longest_repetition([1, 2, 2, 3, 3, 3, 2, 2, 1])
  34. # # 3
  35. #
  36. # print longest_repetition(['a', 'b', 'b', 'b', 'c', 'd', 'd', 'd'])
  37. # # b
  38. #
  39. # print longest_repetition([1,2,3,4,5])
  40. # # 1
  41. #
  42. # print longest_repetition([])
  43. # None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement