Challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Challenge

I have a little challenge for you guys. I don't know how to do it myself, but I'm curious to see what you all can come up with. Write a code that will take a set of data of ANY size (so you don't know how many data points the user will enter) and then find the median. Also note that you have to get the program to rearrange the data into numerical order if it isn't in order already. NOTE: If you don't already know how to find a median anyway, go to bit.ly/HowToMedian Keep in mind that if a user enters a data set with an odd number of points, you have to take the average of the two center points. For example, if the data set is [1, 3, 5, 8] then you would have to take the average of 3 and 5, which is 4.

25th Apr 2018, 1:05 PM
Jackson Meddows
Jackson Meddows - avatar
18 Answers
+ 1
https://imgur.com/a/ngCrlmY as i said, our inputs likely differ, as the code can't return different things for the same input
25th Apr 2018, 1:40 PM
hinanawi
hinanawi - avatar
+ 3
smells a bit like homework, but i did it anyways, here you go. https://code.sololearn.com/c084Pz6d0Qiw/?ref=app
25th Apr 2018, 1:14 PM
hinanawi
hinanawi - avatar
+ 3
https://code.sololearn.com/c7LJuhX86uZi/?ref=app
26th Apr 2018, 12:12 AM
Pedro Demingos
Pedro Demingos - avatar
+ 1
It's not my homework, I've just been thinking on it for about a day now and can't figure it out. I don't even take a programming class lol.
25th Apr 2018, 1:20 PM
Jackson Meddows
Jackson Meddows - avatar
+ 1
Jackson Meddows the first line takes the input and turns it into an array of integers, the second line sorts it in rising order, and the third one checks whether or not the length of the array is odd. if it is, then it just prints out the element at the middle. if not, it prints the average of the two elements at the middle
25th Apr 2018, 1:22 PM
hinanawi
hinanawi - avatar
+ 1
Jackson Meddows the ordered data set is [8 9 9 9 9 10 11 11 11 13 13]. the length is 11, so the median is the 6th element which is 10, the code should output that too edit: for me it outputs 10, your input was probably wrong
25th Apr 2018, 1:26 PM
hinanawi
hinanawi - avatar
+ 1
oh that makes sense. When I tried to read it, I was so overloaded by information and confusion that I didn't process any of it lol. sorry about that!
25th Apr 2018, 2:28 PM
Jackson Meddows
Jackson Meddows - avatar
0
Wow your program is complicated.... i don't understand it at all lmao
25th Apr 2018, 1:20 PM
Jackson Meddows
Jackson Meddows - avatar
0
I'm not sure it works.... I put in the data set [9 13 9 11 9 13 11 9 10 8 11] and the output should have been 10 but it was 9.... i don't know
25th Apr 2018, 1:23 PM
Jackson Meddows
Jackson Meddows - avatar
0
I'm going to run the code again and see what happens.
25th Apr 2018, 1:29 PM
Jackson Meddows
Jackson Meddows - avatar
0
It just outputs 9. Try it yourself
25th Apr 2018, 1:31 PM
Jackson Meddows
Jackson Meddows - avatar
0
Jackson Meddows tried it multiple times now, keeps giving me 10. if you wish i can provide screenshots
25th Apr 2018, 1:33 PM
hinanawi
hinanawi - avatar
0
This is really weird.... idk what's going on
25th Apr 2018, 1:35 PM
Jackson Meddows
Jackson Meddows - avatar
0
@hinanawi I believe you. I don't think you're lying or anything. I just don't understand why we're running the same code with the same input and getting different outputs. Did you check that your input is 9 13 9 11 9 13 11 9 10 8 11?
25th Apr 2018, 1:36 PM
Jackson Meddows
Jackson Meddows - avatar
0
Doesn't it say that multiple inputs have to be on different lines?
25th Apr 2018, 1:44 PM
Jackson Meddows
Jackson Meddows - avatar
0
Jackson Meddows i specified in my code that it takes a single line input, separated by spaces
25th Apr 2018, 1:45 PM
hinanawi
hinanawi - avatar
0
Is it possible to rewrite that code so that it accepts input on separate lines? Just out of curiosity....
25th Apr 2018, 4:40 PM
Jackson Meddows
Jackson Meddows - avatar
0
Jackson Meddows indeed it is. you would input a number, create an array of that size, fill it with a for loop and then perform line 2 onwards
25th Apr 2018, 4:52 PM
hinanawi
hinanawi - avatar