Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- words = input().split(' ')
- def valid_index(i: str, lst: list):
- if len(i) == 1 and i.isdigit():
- return 0 <= int(i) < len(lst)
- while True:
- command = input().split(' ')
- if 'Stop' in command:
- break
- else:
- cmd = command[0]
- if cmd == 'Delete':
- idx = str(int(command[1]) + 1)
- if valid_index(idx, words):
- words.remove(words[int(idx)])
- elif cmd == 'Swap':
- word_1 = command[1]
- word_2 = command[2]
- if word_1 in words and word_2 in words:
- idx_1 = words.index(word_1)
- idx_2 = words.index(word_2)
- words[idx_1], words[idx_2] = words[idx_2], words[idx_1]
- elif cmd == 'Put':
- word = command[1]
- idx = str(int(command[2]) - 1)
- if valid_index(idx, words):
- if int(idx) == len(words) - 1:
- words.append(word)
- else:
- words.insert(int(idx), word)
- elif cmd == 'Sort':
- words.sort()
- words = words[::-1]
- elif cmd == 'Replace':
- word_1 = command[1]
- word_2 = command[2]
- if word_2 in words:
- idx = words.index(word_2)
- words[idx] = word_1
- print(' '.join(words))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement