-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSigmaCodeTest.java
More file actions
133 lines (100 loc) · 3.03 KB
/
TwoSigmaCodeTest.java
File metadata and controls
133 lines (100 loc) · 3.03 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
public class TwoSigmaCodeTest {
//problem one
public static void main (String[] args) throws Exception{
// problem #1
/*
System.out.println(sortByAlphaAndNum(""));
System.out.println(sortByAlphaAndNum("car truck bus"));
System.out.println(sortByAlphaAndNum("8 4 6 1 -2 9 5"));
System.out.println(sortByAlphaAndNum("car truck 8 4 bus 6 1"));
System.out.println(sortByAlphaAndNum(" "));
*/
//problem #2
wrapper_syncDataCenter();
}
public static String sortByAlphaAndNum(String line){
String[] list = line.split(" ");
boolean[] isNum = new boolean[list.length];
LinkedList<String> alpha = new LinkedList<String>();
LinkedList<Integer> integer = new LinkedList<Integer>();
for(int i =0; i<list.length; i++){
if (checkIfNumber(list[i])){
isNum[i] = true;
integer.add(Integer.parseInt(list[i]));
}else{
isNum[i] = false;
alpha.add(list[i]);
}
}
Collections.sort(alpha);
Collections.sort(integer);
String result = "";
for(int i =0; i<isNum.length; i++){
if(isNum[i]){
result+=integer.pollFirst()+" ";
}else{
result+=alpha.pollFirst()+" ";
}
}
if("".equals(result)) return "";
return result.substring(0,result.length()-1);
}
public static boolean checkIfNumber(String in) {
try {
Integer.parseInt(in);
} catch (NumberFormatException ex) {
return false;
}
return true;
}
//problem 2
public static void wrapper_syncDataCenter() throws Exception{
FileInputStream fstream = new FileInputStream("/Users/Terrence/Downloads/input.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(in));
String number = bufferRead.readLine();
if(Integer.parseInt(number) == 0) {
bufferRead.close();
return;
}
String line1 = bufferRead.readLine();
HashMap<String, String> map = new HashMap<String, String>();
String[] list1 = line1.split(" ");
for(int i =0; i<list1.length; i++){
map.put(list1[i], ""+1);
}
int cur =1;
while(cur<Integer.parseInt(number)){
cur++;
String line = bufferRead.readLine();
syncDataCenter(cur, line, map);
}
bufferRead.close();
}
public static void syncDataCenter(int center_id, String sets, HashMap<String, String> map){
String[] list = sets.split(" ");
HashSet<String> local_set= new HashSet<String>();
for(int i =0; i<list.length; i++){
if(!map.containsKey(list[i])){
map.put(list[i], ""+center_id);
for(int j =1; j<center_id; j++){
System.out.println(list[i]+" "+center_id+" "+j);
}
}
local_set.add(list[i]);
}
for(String str : map.keySet()){
if(!local_set.contains(str)){
System.out.println(str + " "+ map.get(str)+" "+center_id);
}
}
}
}