SimeonTs

SUPyF Exam Preparation 2 - 04. Train System

Aug 16th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.40 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1578#3
  4.  
  5. SUPyF Exam Preparation 2 - 04. Train System
  6. Problem:
  7. The Bulgarian Dreadful Zug Company (BDZ) just upgraded their ticket systems from something running on DOS
  8. 5 to a state-of-the-art system running a fancy web app. Of course, nobody bothered to migrate all the discount
  9. cards over to the new system, so all the passengers trying to buy a ticket with a discount nearly miss their train,
  10. since the cashier, grandma Penka, needs to enter it into the new system. Luckily, she heard about this great thing,
  11. called Python from her neighbor, grandma Gina, and how she can use it to automate this task.
  12. In this problem, you assume the role of grandma Penka. You have a 15-minute lunch break, so you decide to use that time
  13. to write up a Python script to migrate all the data.
  14. Input / Constraints
  15. • On the first line, you will receive the number of existing cards N – integer in range [0-5]
  16. • On the next N lines, you will receive the existing cards in the format “firstName lastName ticketNum”.
  17. o   Existing cards will always have a valid card number
  18. • Then, until you receive the command “time to leave!”, keep reading lines in the format:
  19. o   “createTicket firstName lastName destination cardNumber”
  20. The input data will always be in the format specified. There is no need to check it explicitly.
  21. When you receive all the existing cards, insert them into the system.
  22. Cards are tied to the person’s full name (first name + space (“ “) + last name). A person can have multiple cards.
  23. After that, your lunch break is over and you need to start issuing tickets to people again.
  24. A standard “issue ticket” command looks like this:
  25. • “createTicket firstName lastName destination cardNumber”
  26. You need to check if this person already has a card with that number under that name. If they do,
  27. issue their ticket with the card number. If not, you need to check if the card number is valid.
  28. A valid card number’s digit sum is divisible by 7 (example: 297296  (2+9+7+2+9+6) % 7 == 0  valid).
  29. If the card number is invalid, print “card {cardNumber} is not valid!” and issue the ticket without a discount.
  30. If the card number is valid, but it already belongs to another passenger, print “card {cardNumber} already exists for
  31. another passenger!” and issue the ticket without a discount.
  32. If the card doesn’t already belong to another passenger and is not already in the existing cards,
  33. you need to issue that passenger a card. Insert it into the cards and print (“issuing card {cardNumber}”).
  34. After that, issue them a ticket with a discount.
  35. The price of the ticket is the sum of the ASCII codes of the destination name, divided by 100
  36. • Example: vidin  v+i+d+i+n  538 / 100  5.38
  37. Every ticket bought with a valid card that belongs to the passenger is 50% cheaper.
  38. Output
  39. When you receive “time to leave!”, print all passengers in the following format:
  40. fullName:
  41. --{destination}: {ticketPrice:.2f}
  42. --{destination}: {ticketPrice:.2f} (using card {cardNumber})
  43. ...
  44. total: {ticketPrice:.2f}BGN
  45. Sort the passengers by the sum of their ticket prices (descending).
  46. Sort each passenger’s tickets by the ticket’s price (descending).
  47. If a ticket was bought without a discount, don’t print “using card…” after it.
  48.  
  49. Examples:
  50. Input:
  51. 4
  52. pesho ivanov 297296
  53. ivan tsekov 652530
  54. gosho goshov 547989
  55. ivan tsekov 468845
  56. createTicket pesho ivanov vidin 297296
  57. createTicket ivan petrov ruska_bela 590432
  58. createTicket ivan petrov sofia 590430
  59. createTicket pesho ivanov boychinovtsi 590554
  60. createTicket bay ivan montana 912839
  61. time to leave!
  62.  
  63. Output:
  64. card 590432 is not valid!
  65. issuing card 590430
  66. issuing card 590554
  67. card 912839 is not valid!
  68. ivan petrov:
  69. --ruska_bela: 10.49lv
  70. --sofia: 2.65lv (using card 590430)
  71. total: 13.14lv
  72. pesho ivanov:
  73. --boychinovtsi: 6.57lv (using card 590554)
  74. --vidin: 2.69lv (using card 297296)
  75. total: 9.26lv
  76. bay ivan:
  77. --montana: 7.50lv
  78. total: 7.50lv
  79.  
  80. Input:
  81. 1
  82. georgi georgiev 586790
  83. createTicket pesho petrov vidin 297296
  84. createTicket pesho petrov montana 630534
  85. createTicket pesho petrov plovdiv 630534
  86. createTicket bay ivan vidin 297296
  87. createTicket bay ivan sofia 111111
  88. createTicket bay ivan montana 111111
  89. createTicket bay ivan mezdra 111111
  90. time to leave!
  91.  
  92. Output:
  93. issuing card 297296
  94. issuing card 630534
  95. card 297296 already exists for another passenger!
  96. card 111111 is not valid!
  97. card 111111 is not valid!
  98. card 111111 is not valid!
  99. bay ivan:
  100. --montana: 7.50lv
  101. --mezdra: 6.43lv
  102. --vidin: 5.38lv
  103. --sofia: 5.30lv
  104. total: 24.61lv
  105. pesho petrov:
  106. --plovdiv: 3.86lv (using card 630534)
  107. --montana: 3.75lv (using card 630534)
  108. --vidin: 2.69lv (using card 297296)
  109. total: 10.30lv
  110.  
  111. Input:
  112. 3
  113. ivan ivanov 094859
  114. ceko cekov 328994
  115. krali marko 774154
  116. createTicket ivan ivanov montana 094859
  117. createTicket ivan ivanov vidin 094859
  118. createTicket ivan ivanov plovdiv 094859
  119. createTicket krali marko vidin 774154
  120. createTicket krali marko sofia 774154
  121. createTicket bay ivan mezdra 000006
  122. createTicket ceko cekov montana 328994
  123. time to leave!
  124.  
  125. Output:
  126. card 000006 is not valid!
  127. ivan ivanov:
  128. --plovdiv: 3.86lv (using card 094859)
  129. --montana: 3.75lv (using card 094859)
  130. --vidin: 2.69lv (using card 094859)
  131. total: 10.30lv
  132. bay ivan:
  133. --mezdra: 6.43lv
  134. total: 6.43lv
  135. krali marko:
  136. --vidin: 2.69lv (using card 774154)
  137. --sofia: 2.65lv (using card 774154)
  138. total: 5.34lv
  139. ceko cekov:
  140. --montana: 3.75lv (using card 328994)
  141. total: 3.75lv
  142. """
  143.  
  144.  
  145. class Person:
  146.     def __init__(self, f_name: str, l_name: str, cards: []):
  147.         self.f_name = f_name
  148.         self.l_name = l_name
  149.         self.cards = cards
  150.         self.full_name = f"{f_name} {l_name}:"
  151.         self.tickets = []
  152.         self.total_from_tickets = 0
  153.  
  154.  
  155. all_people = []
  156.  
  157. n = int(input())
  158.  
  159. for i in range(n):
  160.     first_name, last_name, card_num = input().split()
  161.     # card_num = int(card_num)
  162.  
  163.     person_in_all_people = False
  164.     for person in all_people:
  165.         if person.f_name == first_name and person.l_name == last_name:
  166.             person_in_all_people = True
  167.             person.cards += [card_num]
  168.     if not person_in_all_people:
  169.         p = Person(f_name=first_name, l_name=last_name, cards=[card_num])
  170.         all_people += [p]
  171.  
  172. while True:
  173.     command = input()
  174.     if command == "time to leave!":
  175.         break
  176.     create_ticket, first_name_n, last_name_n, destination, card_number = command.split()
  177.     # card_number = int(str_card_number)
  178.  
  179.     person_already_in_all_people = False
  180.     for person_n in all_people:
  181.  
  182.         if person_n.f_name == first_name_n and person_n.l_name == last_name_n:
  183.             person_already_in_all_people = True
  184.             person_has_that_card = False
  185.             for card_n in person_n.cards:
  186.                 if card_n == card_number:
  187.                     person_has_that_card = True
  188.                     price_discounted_ticket = (sum(list(map(ord, destination))) / 100) / 2
  189.                     discounted_ticket_data = [f"--{destination}: {price_discounted_ticket:.2f}lv (using card {card_number})", price_discounted_ticket]
  190.                     person_n.tickets += [discounted_ticket_data]
  191.                     person_n.total_from_tickets += price_discounted_ticket
  192.                     break
  193.  
  194.             if not person_has_that_card:
  195.                 card_is_valid = ((sum(list(map(int, [int(item) for item in str(card_number)])))) % 7) == 0
  196.  
  197.                 if not card_is_valid:
  198.                     print(f"card {card_number} is not valid!")
  199.                     price_ticket = sum(list(map(ord, destination))) / 100
  200.                     ticket_data = [f"--{destination}: {price_ticket:.2f}lv", price_ticket]
  201.                     person_n.tickets += [ticket_data]
  202.                     person_n.total_from_tickets += price_ticket
  203.                     continue
  204.  
  205.                 someone_else_card = False
  206.                 for person_s in all_people:
  207.                     if person_s.f_name != first_name_n and person_s.l_name != last_name_n:
  208.                         for card in person_s.cards:
  209.                             if card_number == card:
  210.                                 someone_else_card = True
  211.                                 print(f"card {card_number} already exists for another passenger!")
  212.                                 price_ticket = sum(list(map(ord, destination))) / 100
  213.                                 ticket_data = [f"--{destination}: {price_ticket:.2f}lv", price_ticket]
  214.                                 person_n.tickets += [ticket_data]
  215.                                 person_n.total_from_tickets += price_ticket
  216.  
  217.                 if card_is_valid and (not someone_else_card):
  218.                     print(f"issuing card {card_number}")
  219.                     person_n.cards += [card_number]
  220.                     price_discounted_ticket = (sum(list(map(ord, destination))) / 100) / 2
  221.                     discounted_ticket_data = [f"--{destination}: {price_discounted_ticket:.2f}lv (using card {card_number})", price_discounted_ticket]
  222.                     person_n.tickets += [discounted_ticket_data]
  223.                     person_n.total_from_tickets += price_discounted_ticket
  224.  
  225.     if not person_already_in_all_people:
  226.         card_is_valid = ((sum(list(map(int, [int(item) for item in str(card_number)])))) % 7) == 0
  227.  
  228.         if not card_is_valid:
  229.             print(f"card {card_number} is not valid!")
  230.             price_ticket = sum(list(map(ord, destination))) / 100
  231.             ticket_data = [f"--{destination}: {price_ticket:.2f}lv", price_ticket]
  232.             p = Person(f_name=first_name_n, l_name=last_name_n, cards=["dummy_card"])
  233.             p.tickets += [ticket_data]
  234.             p.total_from_tickets += price_ticket
  235.             all_people += [p]
  236.             continue
  237.  
  238.         someone_else_card = False
  239.         for person_s in all_people:
  240.             if person_s.f_name != first_name_n or person_s.l_name != last_name_n:
  241.                 for card in person_s.cards:
  242.                     if card_number == card:
  243.                         someone_else_card = True
  244.                         print(f"card {card_number} already exists for another passenger!")
  245.                         price_ticket = sum(list(map(ord, destination))) / 100
  246.                         ticket_data = [f"--{destination}: {price_ticket:.2f}lv", price_ticket]
  247.                         p = Person(f_name=first_name_n, l_name=last_name_n, cards=["dummy_card"])
  248.                         p.tickets += [ticket_data]
  249.                         p.total_from_tickets += price_ticket
  250.                         all_people += [p]
  251.  
  252.         if card_is_valid and (not someone_else_card):
  253.             print(f"issuing card {card_number}")
  254.             p = Person(f_name=first_name_n, l_name=last_name_n, cards=[card_number])
  255.             price_discounted_ticket = (sum(list(map(ord, destination))) / 100) / 2
  256.             discounted_ticket_data = [f"--{destination}: {price_discounted_ticket:.2f}lv (using card {card_number})", price_discounted_ticket]
  257.             p.tickets += [discounted_ticket_data]
  258.             p.total_from_tickets += price_discounted_ticket
  259.             all_people += [p]
  260.  
  261.  
  262. for person in sorted(all_people, key=lambda x: x.total_from_tickets, reverse=True):
  263.     if person.total_from_tickets > 0:
  264.         print(f"{person.full_name}")
  265.         for ticket in sorted(person.tickets, key=lambda x: x[1], reverse=True):
  266.             print(f"{ticket[0]}")
  267.         print(f"total: {person.total_from_tickets:.2f}lv")
Add Comment
Please, Sign In to add comment