FAQ: Code Challenge: Lists - Middle Item

8 posts were split to a new topic: Is middle different than median?

2 posts were split to a new topic: Flaming troll

3 posts were split to a new topic: Solution suggestions for Middle item challenge

2 posts were split to a new topic: List indexes cannot have floats [solved]

2 posts were merged into an existing topic: List indexes cannot have floats [solved]

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 merged into an existing topic: List indexes cannot have floats [solved]

2 posts were split to a new topic: Challenge lists - why doesn’t this work [solved]

2 posts were merged into an existing topic: List indexes cannot have floats [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
1 Like

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.

2 Likes

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.

image

We don’t attempt to fool Python. We just write code. We write it, and the computer executes what we wrote. :wink:

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.

1 Like