Skip to content

Commit 453ae5c

Browse files
authored
Create SwapElements.java
1 parent 8b66a4b commit 453ae5c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Easy-Level/SwapElements.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//You are given a list of numbers which is supplemented with positions that have to be swapped.
2+
3+
import java.io.*;
4+
import java.util.*;
5+
public class Main {
6+
public static void main (String[] args) throws IOException {
7+
File file = new File(args[0]);
8+
BufferedReader br = new BufferedReader(new FileReader(file));
9+
String line;
10+
while ((line = br.readLine()) != null) {
11+
String[] a = line.split(":");
12+
String[] b = a[0].split(" ");
13+
String[] c = a[1].replace(" ", "").split(",");
14+
for (String s: new Main().swap(b, c)) {
15+
System.out.print(s + " ");
16+
}
17+
System.out.println();
18+
}
19+
br.close();
20+
}
21+
22+
String[] swap(String[] s, String[] h) {
23+
String g;
24+
for (int i = 0; i < h.length; i++) {
25+
g = h[i];
26+
String[] d = g.split("-");
27+
int a = Integer.parseInt(d[0]);
28+
int b = Integer.parseInt(d[1]);
29+
String c = s[a];
30+
String e = s[b];
31+
s[a] = e;
32+
s[b] = c;
33+
}
34+
return s;
35+
}
36+
}

0 commit comments

Comments
 (0)