SimeonTs

SUPyF2 P.-Mid-Exam/16 April 2019 - 03. Easter Shopping

Oct 29th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.27 KB | None | 0 0
  1. """
  2. Technology Fundamentals Retake Mid Exam - 16 April 2019
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1610#2
  4.  
  5. SUPyF2 P.-Mid-Exam/16 April 2019 - 03. Easter Shopping
  6.  
  7. Problem:
  8. You have decided to go on an Easter shopping spree to take advantage of the promotions.
  9. Create a program that helps you keep track of the shops that you want to visit.
  10. You will receive the list of shops you have planned on checking out on a single line,
  11. separated by a single space in the following format:
  12. "{shop1} {shop2} {shop3}… {shopn}"
  13. Then you will receive a number – n - a count of commands you need to execute over your list.
  14. There are four possible commands:
  15. • "Include {shop}":
  16. o   Add the shop at the end of your list.
  17. • "Visit {first/last} {numberOfShops}"
  18. o   Remove either the "first" or the "last" number of shops from your list, depending on the input.
  19. If you have less shops on your list than the given number, skip this command.
  20. • "Prefer {shopIndex1} {shopIndex2}":
  21. o   If both of the shop indexes exist in your list, take the shops that are on them and change their places.
  22. • "Place {shop} {shopIndex}"
  23. o   Insert the shop after the given index, only if the resulted index exists.
  24. In the end print the manipulated list in the following format:
  25. "Shops left:
  26. {shop1} {shop2}… {shopn}"
  27. Input / Constraints
  28. • On the 1st line, you will receive the starting list with the names of the shops separated by a single space.
  29. • On the 2nd line, you will receive the number of commands - n – an integer in range [1…100]
  30. • On the next n lines you will be receiving commands in the format described above.
  31. Output
  32. • Print the list after the manipulations in the format described above.
  33. Examples:
  34. Input:
  35. Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore
  36. 5
  37. Include HM
  38. Visit first 2
  39. Visit last 1
  40. Prefer 3 1
  41. Place Library 2
  42.  
  43. Output:
  44. Shops left:
  45. ThriftShop ToyStore Groceries Library Armani PeakStore
  46.  
  47. Comments:
  48. First we receive the "Include" and the name of the store and we add the store to our list.
  49. The list should look like this: Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore HM
  50. After, we receive the "Visit" command and "first", which means we have to visit the first 2 stores,
  51. so we remove them from our list and the collection should look like this:
  52. ThriftShop Armani Groceries ToyStore PeakStore HM. After that, we receive the "Visit" command again,
  53. but this time we need to visit the "last" 1 store, so we remove it and the collection should look like this:
  54. ThriftShop Armani Groceries ToyStore PeakStore. After that we receive the "Prefer" command,
  55. which means we need to find the shop on the first given index – 3 and change it with the one that is on index – 1,
  56. and the collection should look like this: ThriftShop ToyStore Groceries Armani PeakStore.
  57. At last, we receive the "Place" command and we need to insert the shop at the next index after 2.
  58. And our final list looks like this:
  59. ThriftShop ToyStore Groceries Library Armani PeakStore
  60.  
  61. Input:
  62. Boutique Flowers CandyStore ThriftShop Versace Groceries ToyStore PeakStore
  63. 6
  64. Visit first 9
  65. Visit last 4
  66. Prefer 3 8
  67. Prefer 0 1
  68. Place Store 7
  69. Place ShoeAquarium 2
  70.  
  71. Output:
  72. Shops left:
  73. Flowers Boutique CandyStore ShoeAquarium ThriftShop
  74. """
  75. shops = input().split()
  76.  
  77. for i in range(int(input())):
  78.     command = input().split()
  79.  
  80.     if command[0] == "Include":
  81.         new_shop = command[1]
  82.         shops.append(new_shop)
  83.  
  84.     elif command[0] == "Visit":
  85.         first_or_last, number_of_shops = command[1], int(command[2])
  86.         if number_of_shops <= len(shops):
  87.             if first_or_last == "first":
  88.                 shops = shops[number_of_shops:]
  89.             elif first_or_last == "last":
  90.                 shops = shops[:-number_of_shops]
  91.  
  92.     elif command[0] == "Prefer":
  93.         shop_index_one, shop_index_two = int(command[1]), int(command[2])
  94.         if 0 <= shop_index_one < len(shops) and 0 <= shop_index_two < len(shops):
  95.             shops[shop_index_one], shops[shop_index_two] = shops[shop_index_two], shops[shop_index_one]
  96.  
  97.     elif command[0] == "Place":
  98.         shop, shop_index = command[1], int(command[2]) + 1
  99.         if 0 <= shop_index < len(shops):
  100.             shops.insert(shop_index, shop)
  101.  
  102. print(f"Shops left:\n{' '.join(shops)}")
Add Comment
Please, Sign In to add comment