Advertisement
kingbode

Untitled

Nov 3rd, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 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. enters = 0
  28. exits = 0
  29.  
  30. def print_Names_List(_list):
  31.     global enters, exits
  32.  
  33.     if type(_list[0]) != tuple:
  34.         enters += len([i for i in transactions if i == 'e'])
  35.         exits += len([i for i in transactions if i == 'x'])
  36.         inside = enters - exits
  37.         return f"only {_list[0]} passed door. ({inside} inside"
  38.     else:
  39.  
  40.         enters += len([i for i in _list if i[1] == 'e'])
  41.         exits += len([i for i in _list if i[1] == 'x'])
  42.         inside = enters - exits
  43.         _list = [i[0] for i in _list]
  44.  
  45.         if len(_list) == 2:
  46.             return f"{_list[0]} and {_list[1]} passed door. ({inside} inside)"
  47.         else:
  48.             return ", ".join(_list[:-1]) + f" and {_list[-1]} passed door.. ({inside} inside)"
  49.  
  50. d_log = {
  51.     7:(('Eric','e')),
  52.     8:(('John','e'),('Michael','e'),('Eric','x')),
  53.     9:(('Terry','e'), ('Eric','e'),('John','x'),('John','e')),
  54.     10:(('John','x'),('Michael','x'))
  55.     }
  56.  
  57.  
  58. _hours = list(d_log.keys())
  59.  
  60. # 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
  61. for hour in _hours:
  62.     transactions = list(d_log[hour])
  63.     print(f'Between {hour} and {hour+1} {print_Names_List(transactions)}')
  64.  
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement