Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Collectors;
- class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
- List<String> inputWords = Arrays.stream(read.readLine().split("\\s+")).collect(Collectors.toList());
- String inputCom;
- while (!"Stop".equals(inputCom = read.readLine())) {
- String[] com = inputCom.split("\\s+");
- switch (com[0]) {
- case "Delete":
- int index = Integer.parseInt(com[1]) + 1;
- if (index >= 0 && index <= inputWords.size() - 1) {
- inputWords.remove(index);
- }
- break;
- case "Swap":
- if (inputWords.contains(com[1]) && inputWords.contains(com[2])) {
- int indexFirst = inputWords.indexOf(com[1]);
- int indexSecond = inputWords.indexOf(com[2]);
- String firstWord = inputWords.get(indexFirst);
- String secondWord = inputWords.get(indexSecond);
- inputWords.set(indexSecond, firstWord);
- inputWords.set(indexFirst, secondWord);
- }
- break;
- case "Put":
- int indexWord = inputWords.indexOf(com[1]);
- int indexPut = Integer.parseInt(com[2]) - 1;
- if (indexWord != -1) {
- inputWords.add(indexWord - 1, com[1]);
- } else if (indexPut >= 0 && indexPut < inputWords.size()) {
- inputWords.add(indexPut, com[1]);
- }
- break;
- case "Sort":
- inputWords = inputWords.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
- break;
- case "Replace":
- if (inputWords.contains(com[2])) {
- int indexSecondWord = inputWords.indexOf(com[2]);
- inputWords.set(indexSecondWord, com[1]);
- }
- break;
- }
- }
- inputWords.forEach(e -> System.out.print(e + " "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement