if len(lst) % 2 == 0:
sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]
return sum / 2
else:
return lst[int(len(lst)/2)]
Hello. The if len(list) % 2 == 0
checks to see if a number is divisible, and does so by using the modulo operator (%
). Essentially, it returns the remainder of a division equation:
5 % 3 returns 2. 3 goes into 5 once, with 2 left over. The 2 is the remainder
Other examples:
7 % 4 returns 3
6 % 3 returns 0
The reason behind this is to check whether the list is and even or and odd length.
I hope this helps!
3 Likes
I was more wondering about the operations that followed, specifically using int in the algorithm, but after a long time, I think I sorted it out and understand now. Thank you so much nevertheless!
1 Like