Skip to content

Commit 8f60f30

Browse files
committed
Create swapelements.java
1 parent 0c1ae48 commit 8f60f30

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

swapelements.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
(c) Sergio Morales
3+
CodeEval Challenge: Swap Elements
4+
Date Solved: 12/20/13
5+
*/
6+
7+
import java.io.*;
8+
9+
public class Main {
10+
public static void main (String[] args) throws java.io.IOException {
11+
File file = new File(args[0]);
12+
BufferedReader in = new BufferedReader(new FileReader(file));
13+
14+
String line = "";
15+
while (( line = in.readLine() ) != null) {
16+
if (line.length() > 0) {
17+
String[] numArray = line.substring(0, line.indexOf(":")).split(" ");
18+
String[] swapArray = line.substring(line.indexOf(":")+1).split(",");
19+
20+
for(int i = 0; i < numArray.length; ++i)
21+
System.out.print(numArray[i]+" ");
22+
System.out.println();
23+
// Print num length
24+
//System.out.println(numArray.length);
25+
for(int i = 0; i < swapArray.length; ++i) {
26+
String[] swaps = swapArray[i].split("-");
27+
Integer firstswap = Integer.parseInt(swaps[0].substring(1));
28+
Integer secondswap = Integer.parseInt(swaps[1]);
29+
String temp = numArray[firstswap];
30+
numArray[firstswap] = numArray[secondswap];
31+
numArray[secondswap] = temp;
32+
}
33+
34+
for(int i = 0; i < numArray.length; ++i)
35+
System.out.print(numArray[i]+" ");
36+
System.out.println("\n");
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)