2 posts were split to a new topic: List indexes cannot have floats [solved]
I solved this by making the two scenarios into variables; even and odd. Each variable finds their respective midpoints, which makes them easy to average and call in the control flow. There’s probably a more elegant way to solve this, though, haha.
def middle_element(lst):
even = int(len(lst)/2) - 1
odd = int(len(lst)/2)
if len(lst) % 2 != 0:
return lst[odd]
else:
return (lst[even]+lst[odd])/2
Rather than doing floating point arithmetic and rounding later, stick with integer operations:
# don't:
int(a / b)
# do:
a // b
Ah! Wasn’t aware of that operator. Thanks!
Would be worth the ten minutes or so it will take to research it further while it is fresh in your mind.
floor division
It’s important to note that floats have floors, too, and if one or both operands are a float, the result will be a float.
10.3 // 2 => 5.0
Only when both operands are integer, will the quotient be an integer.
I couldn’t remember how to fool python into accounting for odd or even (that was a few excercises ago), so I hit the “see solution” button. It wrote this:
#Write your function here
def middle_element(lst):
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)]
#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))
and when I hit solve it printed out: -7.0
I thought the program was supposed to show the middle 2 elements when even. -7.0 isn’t even the middle 2 added or subtracted from each other. So what the H? Did I fully misunderstand the exercise, or was the solution wrong?
Hello, @bit2995930909, and welcome to the forums.
It would seem you misunderstood. The exercise directs us to return the average of the two middle values when there are an even number of values.
We don’t attempt to fool Python. We just write code. We write it, and the computer executes what we wrote.
Ok, I missed the avg line. Thank you. Though I do feel like I’m trying to fool something that can add but doesn’t know what an odd number is. That is a joke. I mean, my joke, not the program.
BTW, using floats for doing integer math is very strange. You’re using a more complicated type to represent something that your simple (and much nicer) type is perfectly able to do. If you do integer division you end up with an integer and discard the remainder.
The only time floats should be involved in anything you do, even as an intermediary step, is if you’re approximating something.
(A common mistake is to think that floats are for representing decimal values and that the numbers will work out nicely like those seen in grade school. That’s not what float is or does.)
can someone tell me why this returns -9?
#Write your function here
def middle_element(lst):
if len(lst) % 2 == 0:
return lst[(len(lst) // 2)] + lst[(len(lst) // 2) -1] // 2
else:
return lst[int(len(lst)/2)]
#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))