-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.py
More file actions
258 lines (171 loc) · 6.13 KB
/
select.py
File metadata and controls
258 lines (171 loc) · 6.13 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
######################################################################################
#
#
#
# Assignment 1
#
# Description: This program reads in one file giving values
# for m, n, and k. Then the program reads in m text
# files each with n binary numbers, then outputs the kth
# smallest value among all the numbers in all the files
# in m^2log^2(n) time.
#
#######################################################################################
#############################################################################################################
#
# Name: binary_search_max()
#
# Description: This function calculates how many numbers are
# smaller than or equal to an inputed number (value) in an already sorted
# array
#
# Preconditions: Each row of the 2d array is sorted
#
# parameters: m, is the soecific row you are looking at
# first is where in the array you start
# last is where in the array you stop
# halfway is the midpoint of the section of the array you are looking for
# Data is the 2d array
# value is the inputed number you are counting how many numbers are smaller than it
#
###############################################################################################################
def binary_search_max (n, m, first, last, halfway, Data, value):
halfway = int((last + first)/2);
#print(str(halfway) + ", " + str(Data[m][halfway]));
if (Data[m][halfway] == value):
while (halfway <= n and Data[m][halfway] == value):
halfway = halfway + 1;
return halfway;
if (not(first > last)):
if (Data[m][halfway] > value): #value is in first half
last = halfway - 1;
#halfway = (first + last)/2;
return binary_search_max(n, m, first, last, halfway, Data, value);
if (Data[m][halfway] < value): #value is in last half
first = halfway + 1;
#halfway = (first + last)/2;
return binary_search_max(n, m, first, last, halfway, Data, value);
if (Data[m][halfway] < value):
return halfway+1;
else:
return halfway;
def binary_search_min (n, m, first, last, halfway, Data, value):
halfway = int((last + first)/2);
#print(str(halfway) + ", " + str(Data[m][halfway]));
if (Data[m][halfway] == value):
#print("value found");
while (halfway > 0 and Data[m][halfway - 1] == value):
halfway = halfway - 1;
return halfway;
if (not(first > last)):
if (Data[m][halfway] > value): #value is in first half
last = halfway - 1;
#halfway = (first + last)/2;
return binary_search_min(n, m, first, last, halfway, Data, value);
if (Data[m][halfway] < value): #value is in last half
first = halfway + 1;
#halfway = (first + last)/2;
return binary_search_min(n, m, first, last, halfway, Data, value);
#print("value not found");
if (Data[m][halfway] < value):
return halfway+1;
else:
return halfway;
# def binary_search_min(m, first, last, halfway, Data, value):
# halfway = (last + first)/2;
# if (Data[m][halfway] == value):
# while (Data[m][halfway] == value and halfway > 0):
# halfway = halfway - 1;
# return halfway;
# if (halfway != last):
# if (Data[m][halfway] > value): #value is in first half
# last = halfway-1;
# #halfway = (first + last)/2;
# return binary_search_min(m, first, last, halfway, Data, value);
# if (Data[m][halfway] < value): #value is in last half
# first = halfway + 1;
# #halfway = (first + last)/2;
# return binary_search_min(m, first, last, halfway, Data, value);
# if (Data[m][halfway] < value):
# return halfway+1;
# else:
# return halfway;
def get_kval(m, n, k, Data, value):
k_min = 0;
k_max = 0;
#print("value: " + str(value));
for i in range(m):
k_min = k_min + binary_search_min (n, i, 0, n, n/2, Data, value);
k_max = k_max + binary_search_max (n, i, 0, n, n/2, Data, value);
#print("k_min: " + str(k_min));
#print("k_max: " + str(k_max));
#print("value: " + str(value) + ", k_min: " + str(k_min) + ", k_max: " + str(k_max));
if(k_min > k):
return k_min+1;
elif(k_max < k):
return k_max;
else:
return k;
def kth_in_arr(m, m_value, n, k, Data, first, last, halfway):
halfway = int((first+last)/2);
#print("m: " + str(m) + " halfway: " + str(halfway));
k_value = get_kval(m, n, k, Data, Data[m_value][halfway]);
#if(halfway >= 0 and halfway <= n):
#print(str(Data[m_value][halfway]) + ", k_value: " + str(k_value));
if (k_value == k):
return Data[m_value][halfway]; #write kval to file
#print("first: " + str(first) + ", last: " + str(last));
if (not(first > last)):
if(k_value < k):
first = halfway + 1;
return kth_in_arr(m, m_value, n, k, Data, first, last, halfway);
elif (k_value > k):
last = halfway - 1;
return kth_in_arr(m, m_value, n, k, Data, first, last, halfway);
else:
return -1; #index of kth smallest element is not in array
def find_kth(m, n, k, Data):
for i in range(m): #walk through each row
kth_smallest = kth_in_arr(m, i, n, k, Data, 0, n, n/2);
if(kth_smallest >= 0): #if the row contains the kth smallest element
return kth_smallest; #you are done
def build_Data():
input_info = open("input.txt", "r");
text = input_info.readline();
array = text.split(',');
m = int(array[0]);
n = int(array[1]);
k = int(array[2]);
input_info.close();
print(array);
Data = [[ ' ' for i in range(n) ] for j in range(m)];
for i in range(1, m+1):
input_data = open(str(i) + ".dat", "rb");
for j in range(n):
bin_value = input_data.read(4);
dec_value = int.from_bytes(bin_value, byteorder='big');
#print(dec_value);
Data[i-1][j] = dec_value;
input_data.close();
print(Data);
kth_smallest_num = find_kth(m, n-1, k, Data);
print(kth_smallest_num);
output_kth = open("output.txt", "w");
#output_kth.write("Hello There!");
output_kth.write(str(kth_smallest_num));
output_kth.close();
#value = int(new_text);
#Data = new_text.read(1);
#print(Data);
def main ():
build_Data();
# m = 3;
# n = 10;
# k = 29;
# Data = [[1, 1, 2, 2, 2, 3, 3, 3, 4, 4],
# [1, 1, 1, 1, 1, 2, 2, 4, 4, 5],
# [1, 3, 3, 3, 3, 3, 3, 3, 3, 6]];
#Data = [[' ' for i in range(m) ] for j in range(n)];
#kth_snallest_num = find_kth(m, n-1, k, Data);
#print(kth_snallest_num);
main();