Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class HOMEWORK_PROGRAMMING02 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] input = scan.nextLine().split(" ");
- int n = scan.nextInt();
- scan.nextLine();
- for (int i = 0; i < n; i++) {
- String command = scan.nextLine();
- if (command.toLowerCase().contains("reverse")) {
- input = ReverseArray(input);
- }
- if (command.toLowerCase().contains("replace")) {
- input = ReplaceArray(input, command);
- }
- if (command.toLowerCase().contains("distinct")) {
- input = DistinctArray(input);
- }
- }
- System.out.println(Arrays.toString(input));
- }
- private static String[] DistinctArray(String[] input) {
- int counter = 0;
- for(int i =0;i<input.length;i++){
- if(input[i]==null){
- continue;
- }
- for(int j=i+1;j<input.length;j++){
- if(input[i].equals(input[j])){
- input[j] = null;
- counter++;
- }
- }
- }
- String[] new_array = new String[input.length - counter];
- int index_counter = 0;
- for (String s : input) {
- if (!(s ==null)) {
- new_array[index_counter] = s;
- index_counter++;
- }
- }
- return new_array;
- }
- public static String[] ReverseArray(String[] input) {
- for (int j = 0; j < input.length / 2; j++) {
- String temp = input[j];
- input[j] = input[input.length - 1 - j];
- input[input.length - 1 - j] = temp;
- }
- return input;
- }
- public static String[] ReplaceArray(String[] input, String command) {
- String[] replaceCommand = command.split(" ");
- input[Integer.parseInt(replaceCommand[1])] = replaceCommand[2];
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement