Let M be the median of the numbers in this file . What is M , rounded to the nearest integer?
Click here to open the file.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
My Solution. Didn't even need to read a file, I just copy pasted the contents since it was smallish s = """212 831 115 125 460 949 538 557 625 327 761 402 760 823 301 95 621 778 400 848 321 121 327 126 361 720 946 15 606 158 294 114 988 237 19 99 357 65 830 16 579 45 527 117 677 83 696 661 270 619 266 795 721 4 835 325 467 59 841 277 798 320 680 484 390 345 203 930 773 52 15 814 683 865 773 711 245 484 742 86 80 406 405 131 602 937 896 498 70 790 304 994 628 282 610 526 120 672 283 579 360 88 16 222 224 579 217 195 550 143 626 995 197 765 869 223 287 520 858 950 795 93 526 359 17 385 170 484 259 879 690 762 101 820 179 403 911 203 628 838 886 175 889 272 283 785 595 711 578 397 166 15 247 187 196 819 33 230 881 376 559 920 687 690 340 894 738 717 215 247 687 910 646 296 214 213 207 124 490 86 543 645 750 342 852 769 643 251 626 545 395 339 550 272 830 573 8 244 319 818"""
arr = []
for a in s.split('\n'):
arr.append(int(a))
arr.sort()
print (arr[(len(arr)/2)] + arr[(len(arr)/2 -1)])/2.0
The following Python code reads the numbers from the file into a list, sorts the list, and prints the median (rounded to the nearest integer):
# Read numbers from file
file = open("cs_median.txt", "r")
numbers = []
for line in file.read().split("\n"):
if (line.isnumeric()):
numbers.append(int(line))
file.close()
# Sort numbers
numbers.sort()
# Print the median
print(round((numbers[len(numbers) // 2 - 1] + numbers[len(numbers) // 2]) / 2))
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hafllka;
import java.util.Scanner;
/**
@author Best */ public class Hafllka {
/**
static double median(int data[]){ double median=0.0; data = sorting(data); if(data.length % 2 == 1) median = data[data.length/2]; else median = ((double)(data[data.length/2] + data[(data.length/2)-1])) / 2; return median; }
public static void main(String[] args) { Scanner masukan = new Scanner(System.in); header("Program Pencari Nilai Median"); int data[] = {212, 831, 115, 125, 460, 949, 538, 557, 625, 327, 761, 402, 760, 823, 301, 95, 621, 778, 400, 848, 321, 121, 327, 126, 361, 720, 946, 15, 606, 158, 294, 114, 988, 237, 19, 99, 357, 65, 830, 16, 579, 45, 527, 117, 677, 83, 696, 661, 270, 619, 266, 795, 721, 4, 835, 325, 467, 59, 841, 277, 798, 320, 680, 484, 390, 345, 203, 930, 773, 52, 15, 814, 683, 865, 773, 711, 245, 484, 742, 86, 80, 406, 405, 131, 602, 937, 896, 498, 70, 790, 304, 994, 628, 282, 610, 526, 120, 672, 283, 579, 360, 88, 16, 222, 224, 579, 217, 195, 550, 143, 626, 995, 197, 765, 869, 223, 287, 520, 858, 950, 795, 93, 526, 359, 17, 385, 170, 484, 259, 879, 690, 762, 101, 820, 179, 403, 911, 203, 628, 838, 886, 175, 889, 272, 283, 785, 595, 711, 578, 397, 166, 15, 247, 187, 196, 819, 33, 230, 881, 376, 559, 920, 687, 690, 340, 894, 738, 717, 215, 247, 687, 910, 646, 296, 214, 213, 207, 124, 490, 86, 543, 645, 750, 342, 852, 769, 643, 251, 626, 545, 395, 339, 550, 272, 830, 573, 8, 244, 319, 818};
System.out.println("Nilai median = "+median(data));
}
}
Python 2 solution:
data = """212
831
115
125
460
949
538
557
625
327
761
402
760
823
301
95
621
778
400
848
321
121
327
126
361
720
946
15
606
158
294
114
988
237
19
99
357
65
830
16
579
45
527
117
677
83
696
661
270
619
266
795
721
4
835
325
467
59
841
277
798
320
680
484
390
345
203
930
773
52
15
814
683
865
773
711
245
484
742
86
80
406
405
131
602
937
896
498
70
790
304
994
628
282
610
526
120
672
283
579
360
88
16
222
224
579
217
195
550
143
626
995
197
765
869
223
287
520
858
950
795
93
526
359
17
385
170
484
259
879
690
762
101
820
179
403
911
203
628
838
886
175
889
272
283
785
595
711
578
397
166
15
247
187
196
819
33
230
881
376
559
920
687
690
340
894
738
717
215
247
687
910
646
296
214
213
207
124
490
86
543
645
750
342
852
769
643
251
626
545
395
339
550
272
830
573
8
244
319
818"""
x = map(int, data.split())
while len(x) > 2: # lowers x to two middle numbers
x.pop(x.index(max(x)))
x.pop(x.index(min(x)))
print sum(x)/2 # >>> 464
Main idea : The median of an even number of data is defined as the average of the two middlemost ones after sorted. We notice that there are 200 numbers, so we need to sort the numbers and get the average of the 100th and 101st numbers. We can round the result later manually.
Pseudocode :
data <- the list of numbers
sort data in-place
print ((100th element of data + 101st element of data) / 2) to output
Python 2 code :
data = """212
831
115
...
319
818"""
data = data.split("\n")
data = map(lambda x: int(x), data)
data.sort()
print (data[99] + data[100]) / 2.0
Why write
lambda x: int(x)
when you could just write
int
...?
Because I forgot
int
is a function that I can pass to
map
. I have too many
lambda
s.
Also today I learned
`backticks`
can be used for code spans (putting code in-line), from the link provided in the formatting guide (but not int the guide itself). Perhaps the guide should put this?
.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
int main(void)
{
std::ifstream fil("br1.txt");
std::vector<int> vec;
for ( int x; fil >> x; )
vec.push_back(x);
std::sort(vec.begin(), vec.end());
std::vector<int>::size_type len = vec.size();
if ( len % 2 )
std::cout << "median (" << len << "): " << vec[len/2] << std::endl;
else if ( len )
{
int m1 = vec[len/2-1];
int m2 = vec[len/2];
std::cout << "median (" << len << "): " << m1 << "," << m2 << " ==> "
<< ((m1 + m2 + 1) / 2) << std::endl;
}
return 0;
}
self explanatory
def main():
fin = open('in.txt', 'r')
L = [int(i) for i in fin.readlines()]
L.sort()
return int((L[99]+L[100])/2.0+0.5)
main()
As somebody else observed, you could have just written
open('in.txt')
because
open
's default mode is
r
. I think writing the
r
explicitly helps readability a little, though, so I think that's fine.
On the other hand, you don't need to write
fin.readlines()
; you can just iterate through
fin
, which iterates over its own lines:
L = [int(i) for i in fin]
Not only is this simpler to type, it also may save you some memory or time (though not for this problem) because it only reads through the file as you request each element of the iteration, whereas (I think) calling
fin.readlines()
instantly reads the entire file into memory as a list. If you're running a decent Unix-like environment, you can compare running these two statements:
$ yes | head -n 100000000 | python -c 'import sys
for ln in sys.stdin: print ln'
which starts printing
y
s immediately, and
$ yes | head -n 100000000 | python -c 'import sys
for ln in sys.stdin.readlines(): print ln'
which doesn't do anything at the start.
Problem Loading...
Note Loading...
Set Loading...
Examining the file reveals it has a bunch of numbers, one per line. How many?
200, to be precise. So we want to sort the file numerically, get the 100th and 101st lines, and average them: