Why answer is 4.5 (I was expecting 5) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why answer is 4.5 (I was expecting 5)

https://code.sololearn.com/cs1TEXrLkRWF/?ref=app So It is like this - [4,5,5,4] It took 2 index -5 Then it took 3 index - 5 10/2 = 5 so Output should be 5 instead of 4.5 Please Explain it. Thank You in Advance.

8th Sep 2021, 2:18 PM
Balraj Singh
4 Answers
+ 2
You sorted the list I guess?
8th Sep 2021, 2:23 PM
Tim
Tim - avatar
+ 2
Balraj Singh no,your code first took the a first index which is 4, and a second index which is 5 , it added that and divided it by 2. Which is ofcourse 4.5 the sorted list was [4,4,5,5] . in your code first iteration was skipped,then the second 4 becomes the first index,and 5 becomes the second index
8th Sep 2021, 2:31 PM
raynard
raynard - avatar
+ 1
That's your list: (4,5,5,4) Sort it => (4,4,5,5) Median = (4 + 5) / 2 = 4.5 There is many boilerplate code in your version. For what is this for-loop? I was a little bit playing a round in your code. Have a look at this: https://code.sololearn.com/ch39p12lNcaP/?ref=app
8th Sep 2021, 3:02 PM
Coding Cat
Coding Cat - avatar
0
if index == 0: print('Frist value') # frist_value
8th Sep 2021, 2:43 PM
SoloProg
SoloProg - avatar