Median


#1

This is the code I am using.

def median(lst):
ans = sorted(lst, key = int)
wer = ""
while len(ans) != 1:
if len(ans) == 2:
break
ans.pop(0)
ans.pop(len(ans) - 1)
for n in ans:
wer += str(n)
return int(wer)

The error message returned is Oops, try again. median([4, 5, 5, 4]) returned 45 instead of 4.5
I know this probably has a simple answer but I can't seem to find it out. Please help if you can.


#2

You should post your code in preformatted text (Ctrl + Shift + C) like this to preserve all the indentations for others to understand your code better.

def median(lst):
	ans = sorted(lst, key = int)
	wer = ""
	while len(ans) != 1:
		if len(ans) == 2:
			break
		ans.pop(0)
		ans.pop(len(ans) - 1)
		for n in ans:
			wer += str(n)
	return int(wer)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.