-
Notifications
You must be signed in to change notification settings - Fork 0
/
collabedit.txt
210 lines (143 loc) · 4.86 KB
/
collabedit.txt
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Hello from ThousandEyes */
/* Hello from Nelson */
/*
1
|\
2 3__
| |\ \
4 5 6 7
DFS: 1, 2, 4, 3, 5, 6, 7
BFS: 1,2,3,4,5,6,7
*/
public class Node {
private int content;
private List<Node> children;
public Node(int content, List<Node> children) {
this.content = content;
this.children = children;
}
public void dfs() {
System.out.print("DFS: ");
dfs_step(this, new HashSet());
}
private void dfs_step(Node node, Set<Node> visitedNodes) {
System.out.println(content + ",");
visitedNodes.add(this)
for(Node node : children) {
if (!visitedNode.contains(node)) {
dfs_step(node);
}
}
}
public void bfs() {
System.out.print("BFS: ");
List<Node> queue = new LinkedList<Node>();
queue.add(this);
for (Node node : queue) {
System.out.print(content + ",");
queue.remove(node);
queue.addAll(node.children);
}
}
equals
hashcode
}
tb_temperatures
country_id | region_id | temperature
US | CA | 30
US | NY | 20
US | TX | 35
PT | LIS | 25
Q1: a query that outputs the country,region,temp with the highest temperature
select country_id, region_id, temperature from tb_temperatures order by temperature limit 1
Q2: a query that outputs the region,temp with highest temperature for each country
select country_id, region_id, temperature from tb_temperatures tb_outer tb_outer.temperatur =
(select max(temperature) from tb_temperatures where country_id = tb_outer.country_id group by country_id)
/*
http://d3js.org/
http://jsfiddle.net/RubuJ/
Draw a scatter plot using d3
- use svg <circle>s for the points
- use d3 data-binding (enter/exit)
Here's some data:
[[1,3],[2,5],[3,6],[4,1],[7,4],[9,5]]
*/
public class Node {
public int type;
public double value;
public List<Node> neighbors;
}
public class Graph {
protected Node root;
public Map<Integer, Double> computeAverages() {
Map<Integer, Double> sums = new HashMap<Integer, Double>();
Map<int, int> counts = new HashMap<int, int>();
dfs_step(root, answer, counts, new HashSet<Node>());
Map<int, double> answer = new HashMap<int, double>();
for (Map.Entry<int, double> sum : sums) {
double average = sum.getValue() / counts.get(sum.getKey());
answer.put(sum.getKey(), average);
}
return answer;
}
private dfs_step(Node node, Map<int, double> currentSum, Map<int, int> countOfNodesPerType, Set<Node> visitedNodes) {
for (Node node : node.neighbours) {
if (!visitedNodes.contains(visitedNodes)) {
Integer countOfNodes = countOfNodesPerType.get(node.type);
Double currentSum = currentSum.get(node.type);
if (countOfNodes == null) {
countOfNodes = new Integer(1);
} else {
countOfNodes ++;
}
if (currentSum == null) {
currentSum = 0d;
}
double newSum = currentSum + node.value;
currentSum.put(node.type, newSum)
countOfNodesPerType.put(node.type, countOfNodes);
visitedNodes.add(node);
dfs_step(node, currentSum, countOfNodesPerType, visitedNodes);
}
}
}
}
public class BlockingQueue<T> {
private LinkedList<T> queue = new LinkedList<T>();
private int maxSize;
public BlockingQueue(int maxSize) {
this.maxSize = maxSize;
}
public T get() {
while (true) {
synchronized(queue) {
if (!queue.empty()) {
T object = queue.removeFirst();
// notify all producers waiting
queue.notifyAll();
return object;
}
try {
queue.wait();
} catch (InterrupetedException e) {
}
}
}
}
public void put(T obj) {
while(true) {
synchronized(queue) {
if (queue.size() == maxSize) {
try {
queue.wait();
} catch (InterrupedException e) {
}
} else {
queue.add(obj);
// notify all consumers waiting
queue.notifyAll();
}
}
}
}
}