Advertisement
kingbode

Untitled

Nov 3rd, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. '''
  2. As junior consultant you're asked to write a small notification system for passages based on logs from doors in secure area of company.
  3. # log format: {hour:(('Name','e/x',)),} e/x: Entry or eXit
  4. d_log = {7:(('Eric','e')),8:(('John','e'),('Michael','e'),('Eric','x')),9:(('Terry','e'), ('Eric','e'),('John','x'),('John','e')),10:(('John','x'),('Michael','x'))}
  5. code should print something like below output
  6.  
  7. output:
  8. '''
  9. # Between 7 and 8 only Eric passed door.
  10. # Between 8 and 9 John, Michael and Eric passed door.
  11. # Between 9 and 10 Terry, Eric, John and John passed door.
  12. # Between 10 and 11 John and Michael passed door.
  13. '''
  14. # bonus: expand system to keep track of people entering / exiting and amount in area
  15. output:
  16. '''
  17. # Between 7 and 8 only Eric entered Secure Area. (1 inside)
  18. # Between 8 and 9 John and Michael entered, only Eric exited Secure area. (2 inside)
  19. # Between 9 and 10 Terry, Eric and John entered, only John exited Secure area. (4 inside)
  20. # Between 9 and 10 John and Michael exited Secure area. (2 inside)
  21. '''
  22. #Bonus 2. some suggestions/ examples of other ways to extend the code/ idea
  23. Remember it’s not about being first it’s about learning from doing and from what other people have done..
  24. so share your code in a format others can easily copy to test/ extend
  25. '''
  26.  
  27. def print_Names_List(_list):
  28.     if type(_list[0]) != tuple:
  29.         return f"only {_list[0]} passed door."
  30.     else:
  31.  
  32.         _list = [i[0] for i in _list]
  33.  
  34.         if len(_list) == 2:
  35.             return f"{_list[0]} and {_list[1]} passed door."
  36.         else:
  37.             return ", ".join(_list[:-1]) + f" and {_list[-1]} passed door."
  38.  
  39. d_log = {
  40.     7:(('Eric','e')),
  41.     8:(('John','e'),('Michael','e'),('Eric','x')),
  42.     9:(('Terry','e'), ('Eric','e'),('John','x'),('John','e')),
  43.     10:(('John','x'),('Michael','x'))
  44.     }
  45.  
  46.  
  47. _hours = list(d_log.keys())
  48.  
  49. # every transaction in the log is a tuple of 2 elements: (name, entrance/exit) as a value of the dictionary and the key is the hour
  50. for hour in _hours:
  51.     transactions = list(d_log[hour])
  52.     print(f'Between {hour} and {hour+1} {print_Names_List(transactions)}')
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement