2 posts were split to a new topic: Why is my sum variable text white? [solved]
3 posts were merged into an existing topic: List indexes cannot have floats [solved]
7 posts were split to a new topic: How does integer casting work? int() [solved]
2 posts were split to a new topic: Challenge lists - why doesn’t this work [solved]
3 posts were split to a new topic: Lists challenge - code problem [solved]
4 posts were merged into an existing 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.
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
if len(lst) % 2 == 0:
return lst[(len(lst) // 2)] + lst[(len(lst) // 2) -1] // 2
#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))
Please, if you will, explain the logic here.
In the index2 variable I’m storing the index to the “left” of the “middle” of the list.
I first find the length of the list with len(), then divide that by 2 to get the position/index to the “right” of the “middle.” I then subtract 1 to move to the left of the middle since we found the other position in the variable “index1.”
Python returns answers from division as floats, I believe, and since I can’t pass through a float into a list as an index, I have to wrap it in the int() function to make it an integer.
I hope this makes sense! It took me a little bit to figure this one out!