Advertisement
simeonshopov

Easter Shopping

Jan 28th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. shops = input().split(' ')
  2. com_count = int(input())
  3.  
  4.  
  5. def valid_index(i: str, lst: list):
  6.     if len(i) == 1 and i.isdigit():
  7.         return 0 <= int(i) < len(lst)
  8.  
  9.  
  10. for command in range(com_count):
  11.     tokens = input().split(' ')
  12.     cmd = tokens[0]
  13.     if cmd == 'Include':
  14.         shop = tokens[1]
  15.         shops.append(shop)
  16.     elif cmd == 'Visit':
  17.         count = int(tokens[2])
  18.         if count <= len(shops):
  19.             if tokens[1] == 'first':
  20.                 shops = [shops[x] for x in range(len(shops)) if x > count - 1]
  21.             elif tokens[1] == 'last':
  22.                 shops = [shops[x] for x in range(len(shops) - 1, -1, -1) if x < len(shops) - count][::-1]
  23.     elif cmd == 'Prefer':
  24.         if tokens[1].isdigit() and tokens[2].isdigit():
  25.             if valid_index(tokens[1], shops) and valid_index(tokens[2], shops):
  26.                 if int(tokens[1]) != int(tokens[2]):
  27.                     idx_1 = int(tokens[1])
  28.                     idx_2 = int(tokens[2])
  29.                     shops[idx_1], shops[idx_2] = shops[idx_2], shops[idx_1]
  30.     elif cmd == 'Place':
  31.         shop = tokens[1]
  32.         if tokens[2].isdigit():
  33.             idx = str(int(tokens[2]) + 1)
  34.             if valid_index(idx, shops):
  35.                 shops.insert(int(idx), shop)
  36.  
  37.  
  38. print(f'Shops left:\n{" ".join(shops)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement