PyScripter でプログラムを実行すると、予想される の中央値が得られ4.5
ます。しかし、ここと同じ Ideoneまたはcodeacademyで同じコードを実行すると、が返されます4
。なぜ異なる結果が得られるのか考えていますか? ありがとう。
#Create a function that returns the median of a list of numbers
def median(list_of_numbers):
#Make a copy of list_of_numbers and sort the new list
lst = list_of_numbers
lst = sorted(lst)
#Get the length of list and use it to index through the list
lst_length = len(lst)
index = int(lst_length/2)
#If length if even, average the two middle numbers
if lst_length%2==0:
a= lst[index-1]
b = lst[index]
result = (a+b)/2
#If length is odd, return the middle number
else:
result = lst[index]
return result
print (median([4, 5, 5, 4]))
私の PyScripter のバージョン: * Python 3.3.5 (v3.3.5:62cf4e77f785、2014 年 3 月 9 日、10:37:12) [MSC v.1600 32 ビット (Intel)] on win32。*