Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Think my math is wrong?
#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


Messages In This Thread
Think my math is wrong? - by edwdas - Nov-05-2019, 02:40 PM
RE: Think my math is wrong? - by ichabod801 - Nov-05-2019, 02:48 PM
RE: Think my math is wrong? - by buran - Nov-05-2019, 02:56 PM
RE: Think my math is wrong? - by edwdas - Nov-05-2019, 02:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,802 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  math.log versus math.log10 stevendaprano 10 2,609 May-23-2022, 08:59 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,923 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  python gives wrong string length and wrong character thienson30 2 3,105 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