Guest User

Untitled

a guest
Apr 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import string
  2. import random
  3.  
  4. def Get_number():
  5. x = int(input("Enter a number:\n"))
  6. return x
  7.  
  8. def Usr_msg(x, y):
  9. """Print a message telling the user whether there were any
  10. duplicates, and if so how many"""
  11. if len(x) == len(y):
  12. print("The list had no duplicates")
  13. elif len(x) - len(y) == 1:
  14. print("The list had one duplicate")
  15. else:
  16. u = len(x) - len(y)
  17. print("The list had " + str(u) + " duplicates\n")
  18.  
  19. def a_no_dup_set(a):
  20. """Generate list of elements in a without duplicates using sets"""
  21. b = list(set(a))
  22. return b
  23.  
  24. def a_no_dup_loop(a):
  25. """Generate list of elements in a without duplicates using a loop"""
  26. b = []
  27. for i in a:
  28. if not i in b:
  29. b.append(i)
  30. return b
  31.  
  32. x = Get_number()
  33. #Generate random list of letters
  34. a = random.choices(string.ascii_lowercase, k = x)
  35.  
  36. a_no_dup = a_no_dup_set(a)
  37. #a_no_dup = a_no_dup_loop(a)
  38. print(a, a_no_dup, sep = "\n")
  39. Usr_msg(a, a_no_dup)
Add Comment
Please, Sign In to add comment