Guest User

Untitled

a guest
Sep 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. def parse(people):
  2. """
  3. returns a list of tuples; first element in tuple - last name; second element - a dict
  4. """
  5. people_dicts = [dict(zip(("first", "last", "email"), person.split())) for person in people.splitlines()]
  6. return [(d["last"], d) for d in people_dicts]
  7.  
  8. def santafy(l):
  9.  
  10. def index(i):
  11. return i % len(l)
  12.  
  13. def swap(x, y):
  14. l[x], l[y] = l[y], l[x]
  15.  
  16. for x in range(len(l)):
  17. if l[index(x)][0] == l[index(x + 1)][0]:
  18. for xx in range(len(l)):
  19. if l[index(x + 1 + xx)][0] != l[index(x + 1 + xx + 1)][0]:
  20. swap(index(x + 1 + xx), index(x + 1 + xx + 1))
  21. return santafy(l)
  22.  
  23. return l
  24.  
  25. def report(l):
  26. def index(i):
  27. return i % len(l)
  28.  
  29. for x in range(len(l)):
  30. print "{0} is Santa for {1} {2}".format(l[index(x)][1]["email"], l[index(x+1)][1]["first"], l[index(x+1)][1]["last"])
  31.  
  32.  
  33. report(santafy(parse("""Luke Skywalker <luke@theforce.net>
  34. Leia Skywalker <leia@therebellion.org>
  35. Harry Potter <harry@hogwarts.com>
  36. Lily Potter <lily@magic.org>
  37. Bruce Wayne <bruce@imbatman.com>
  38. Sarah Connor <sarah@terminator.com>
  39. John Connor <john@skynet.com>""")))
Add Comment
Please, Sign In to add comment