Advertisement
simeonshopov

The Final Quest 83/100

Jan 28th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. words = input().split(' ')
  2.  
  3.  
  4. def valid_index(i: str, lst: list):
  5.     if len(i) == 1 and i.isdigit():
  6.         return 0 <= int(i) < len(lst)
  7.  
  8.  
  9. while True:
  10.     command = input().split(' ')
  11.     if 'Stop' in command:
  12.         break
  13.     else:
  14.         cmd = command[0]
  15.         if cmd == 'Delete':
  16.             idx = str(int(command[1]) + 1)
  17.             if valid_index(idx, words):
  18.                 words.remove(words[int(idx)])
  19.         elif cmd == 'Swap':
  20.             word_1 = command[1]
  21.             word_2 = command[2]
  22.             if word_1 in words and word_2 in words:
  23.                 idx_1 = words.index(word_1)
  24.                 idx_2 = words.index(word_2)
  25.                 words[idx_1], words[idx_2] = words[idx_2], words[idx_1]
  26.         elif cmd == 'Put':
  27.             word = command[1]
  28.             idx = str(int(command[2]) - 1)
  29.             if valid_index(idx, words):
  30.                 if int(idx) == len(words) - 1:
  31.                     words.append(word)
  32.                 else:
  33.                     words.insert(int(idx), word)
  34.         elif cmd == 'Sort':
  35.             words.sort()
  36.             words = words[::-1]
  37.         elif cmd == 'Replace':
  38.             word_1 = command[1]
  39.             word_2 = command[2]
  40.             if word_2 in words:
  41.                 idx = words.index(word_2)
  42.                 words[idx] = word_1
  43.  
  44. print(' '.join(words))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement