Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Think my math is wrong?
#1
So I'm trying to calculate the median from inputs (int) that a user submits WITHOUT using a statistics module (just want to see how it can be done)

Current function to find the median looks like this

def median():
    srt = sorted(lst)
    mid = len(lst)//2
    if len(lst) % 2:
            return srt[mid]
    else:
        med = int((srt[mid] + srt[mid-1])) // 2
        return med
And the whole script looks like this

lst = []
def median():
    srt = sorted(lst)
    mid = len(lst)//2
    if len(lst) % 2:
            return srt[mid]
    else:
        med = int((srt[mid] + srt[mid-1])) // 2
        return med


while True:
    l = (input("Enter a number "))
    if l == "":
        print(median())
        break
    lst.append(l)
Reply
#2
You need to convert them to ints as they are appended to lst. If you don't, they won't sort correctly, because strings sort alphabetically. That is, when sorting strings, '2' is higher than '10', and indeed higher than '1234567890'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
// is floor division:
>>> 5//2
2
so if there are odd number of elements you want to return srt[mid+1] (on line 6)

With even number of elements median should be the mean of the middle two numbers (i.e. the true division of the middle 2 elements)- mid and mid+1, not mid-1
med = (srt[mid] + srt[mid+1]) / 2

Also because of using floor division you don't need to explicitly convert to int on line 8 (but this is just a note)

that said, you better add argument to median function, instead of using global variable lst
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
How would one do this?

tried slipping in a cheeky
 l = [int(lst) for lst in l] 
directly above the lst.append but that seemed to cause more issues

(Nov-05-2019, 02:56 PM)buran Wrote: With even number of elements median should be the mean of the middle two numbers (i.e. the true division of the middle 2 elements)- mid and mid+1, not mid-1
med = (srt[mid] + srt[mid+1]) / 2

Also because of using floor division you don't need to explicitly convert to int on line 8 (but this is just a note)

that said, you better add argument to median function, instead of using global variable lst

With these amendments i get the error "int expected str received" in turn it kills itself. Also declaring the "l = input()" to be int it also kills itself with "ValueError: invalid literal for int() with base 10: ''"


furthermore, with the script it its original state and changing the -1 to +1 were told too the input is 8 8 8 8 by the user my median is apparently 44?

Actually.

Applying the suggested math changes and by simplying changing the append to lst.append(int(l))

it bloody worked, so cheers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,383 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  math.log versus math.log10 stevendaprano 10 2,301 May-23-2022, 08:59 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,704 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  python gives wrong string length and wrong character thienson30 2 2,940 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020