Advertisement
SimeonTs

SUPyF Exam 24.03.2019 - 03. Apartments

Aug 13th, 2019
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.52 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1590#2
  4.  
  5. SUPyF Exam 24.03.2019 - 03. Apartments
  6.  
  7. Problem:
  8. Input / Constraints
  9. We have the task to create database for a businessman, who buys apartments in different neighborhoods here in Sofia.
  10. At the first stage he does a research for available apartments.
  11. He will give you a neighborhood name and a list of block numbers in format:
  12. {neighborhood} -> {block_num,block_num,block_num}
  13. When you receive the command ‘collectApartments’ you should stop adding research results and start assigning them with
  14. the real data. Note that the businessman can give you a neighborhood or a block number which haven’t been researched.
  15. In this case just do nothing, but if he gives you a researched neighborhood and a researched block number in this
  16. neighborhood assign the values for it. The data will come in the following format:
  17.  
  18. { neighborhood}&{block_number} -> {count_of_available_apartments}|{price_for_one_apartment}
  19.  
  20. It’s  possible to receive existing neighborhood and block_number and already assigned count_of_available_apartments
  21. with given price. In this case REPLACE  the old info with the new one.
  22. Output
  23. When you recieve a command which says 'report', you should print all apartments data ordered by name of the neighborhood
  24. ascending after that by block_number ascending in the following format:
  25. Neighborhood: {neighborhood}
  26. * Block number: {block_number} -> { count_of_available_apartments } apartments for sale. Price for one: {price_for_one_apartment }
  27. • If  there is no available apartments in this block in this neighborhood just print 0 for the available apartments count.
  28. • If there is no price, just print for price_for_one_apartment ‘None’.
  29.  
  30. Examples:
  31.    Input:
  32.        Lozenec -> 11,2
  33.        Durvenica -> 4,3
  34.        Mladost1 -> 5,2
  35.        Mladost2 -> 7,8
  36.        collectApartments
  37.        Lozenec&11 -> 2|100000
  38.        Lozenec&2 -> 1|100000
  39.        Durvenica&3 -> 5|80000
  40.        Durvenica&5 -> 15|80000
  41.        Mladost2&13 -> 6|80000
  42.        Mladost1&13 -> 7|79000
  43.        report
  44.    Output:
  45.        Neighborhood: Durvenica
  46.        * Block number: 3 -> 5 apartments for sale. Price for one: 80000
  47.        * Block number: 4 -> 0 apartments for sale. Price for one: None
  48.        Neighborhood: Lozenec
  49.        * Block number: 2 -> 1 apartments for sale. Price for one: 100000
  50.        * Block number: 11 -> 2 apartments for sale. Price for one: 100000
  51.        Neighborhood: Mladost1
  52.        * Block number: 2 -> 0 apartments for sale. Price for one: None
  53.        * Block number: 5 -> 0 apartments for sale. Price for one: None
  54.        Neighborhood: Mladost2
  55.        * Block number: 7 -> 0 apartments for sale. Price for one: None
  56.        * Block number: 8 -> 0 apartments for sale. Price for one: None
  57. """
  58. # Here we have 2 ways to solve the problem first option is with Classes:
  59.  
  60.  
  61. class Block:
  62.     def __init__(self, block_number: int, count_apartments=0, price_one=None):
  63.         self.block_number = block_number
  64.         self.count_apartments = count_apartments
  65.         self.price_one = price_one
  66.  
  67.  
  68. class Neighborhood:
  69.     def __init__(self, name, blocks: []):
  70.         self.name = name
  71.         self.blocks = blocks
  72.  
  73.  
  74. all_neighborhoods = []
  75.  
  76. while True:
  77.     data = input()
  78.     if data == "collectApartments":
  79.         break
  80.     c_neighborhood, all_blocks = data.split(" -> ")
  81.     c_blocks = [Block(block_number=int(block)) for block in all_blocks.split(",")]
  82.     neigh = Neighborhood(name=c_neighborhood, blocks=c_blocks)
  83.  
  84.     if_neighborhood_exist = False
  85.     for neighborhood in all_neighborhoods:
  86.         if neighborhood.name == neigh.name:
  87.             if_neighborhood_exist = True
  88.  
  89.             for checked_block in neigh.blocks:
  90.                 if_block_exist = False
  91.                 for block in neighborhood.blocks:
  92.                     if checked_block.block_number == block.block_number:
  93.                         if_block_exist = True
  94.                 if not if_block_exist:
  95.                     neighborhood.blocks += [Block(block_number=int(checked_block.block_number))]
  96.  
  97.     if not if_neighborhood_exist:
  98.         all_neighborhoods += [neigh]
  99.  
  100.  
  101. all_neighborhoods.sort(key=lambda x: x.name, reverse=False)
  102. for neighborhood in all_neighborhoods:
  103.     neighborhood.blocks.sort(key=lambda x: x.block_number)
  104.  
  105. while True:
  106.     data = input()
  107.     if data == "report":
  108.         break
  109.     neigh_and_block, av_and_pr = data.split(" -> ")
  110.     r_neighborhood, r_bock = neigh_and_block.split("&")
  111.     r_av_ap, r_price = av_and_pr.split("|")
  112.     r_bock = int(r_bock)
  113.  
  114.     for neigh in all_neighborhoods:
  115.         if r_neighborhood == neigh.name:
  116.             for bl in neigh.blocks:
  117.                 if bl.block_number == r_bock:
  118.                     bl.count_apartments = r_av_ap
  119.                     bl.price_one = r_price
  120.                     break
  121.  
  122. for n in all_neighborhoods:
  123.     print(f"Neighborhood: {n.name}")
  124.     for b in n.blocks:
  125.         print(f"* Block number: {b.block_number} -> {b.count_apartments} apartments for sale. Price for one: {b.price_one}")
  126.  
  127.  
  128. # The second way to solve the problem is to use dictionary (which i hate from the depth of my heart):
  129. """
  130. apartments = {}
  131.  
  132. data_list = input().split(' -> ')
  133.  
  134. while not data_list[0] == 'collectApartments':
  135.    neighborhood = data_list[0]
  136.    blocks_nums = list(map(int, data_list[1].split(',')))
  137.  
  138.    if neighborhood not in apartments.keys():
  139.        apartments[neighborhood] = {}
  140.    for num in blocks_nums:
  141.        apartments[neighborhood][num] = {'available_apartments_count': 0, 'price': None}
  142.  
  143.    data_list = input().split(' -> ')
  144.  
  145. data_list = input().split(' -> ')
  146.  
  147. while not data_list[0].strip() == 'report':
  148.    neighborhood, block_num = data_list[0].split('&')
  149.    count_available, price = list(map(int, data_list[1].split('|')))
  150.    block_num = int(block_num)
  151.  
  152.    if neighborhood in apartments.keys() and block_num in apartments[neighborhood].keys():
  153.        apartments[neighborhood][block_num]['available_apartments_count'] = count_available
  154.        apartments[neighborhood][block_num]['price'] = price
  155.  
  156.    data_list = input().split(' -> ')
  157.  
  158. for neighborhood, _ in sorted(apartments.items(), key=lambda kvp: kvp[0]):
  159.    print(f"Neighborhood: {neighborhood}")
  160.    for block_num, info_dict in sorted(apartments[neighborhood].items(), key=lambda kvp: kvp[0]):
  161.        print(
  162.            f"* Block number: {block_num} -> {info_dict['available_apartments_count']} apartments for sale. Price for one: {info_dict['price']}")
  163. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement