-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapelements.java
More file actions
40 lines (35 loc) · 1.48 KB
/
swapelements.java
File metadata and controls
40 lines (35 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
(c) Sergio Morales
CodeEval Challenge: Swap Elements
Date Solved: 12/20/13
*/
import java.io.*;
public class Main {
public static void main (String[] args) throws java.io.IOException {
File file = new File(args[0]);
BufferedReader in = new BufferedReader(new FileReader(file));
String line = "";
while (( line = in.readLine() ) != null) {
if (line.length() > 0) {
String[] numArray = line.substring(0, line.indexOf(":")).split(" ");
String[] swapArray = line.substring(line.indexOf(":")+1).split(",");
for(int i = 0; i < numArray.length; ++i)
System.out.print(numArray[i]+" ");
System.out.println();
// Print num length
//System.out.println(numArray.length);
for(int i = 0; i < swapArray.length; ++i) {
String[] swaps = swapArray[i].split("-");
Integer firstswap = Integer.parseInt(swaps[0].substring(1));
Integer secondswap = Integer.parseInt(swaps[1]);
String temp = numArray[firstswap];
numArray[firstswap] = numArray[secondswap];
numArray[secondswap] = temp;
}
for(int i = 0; i < numArray.length; ++i)
System.out.print(numArray[i]+" ");
System.out.println("\n");
}
}
}
}