Please help with Python Idle problem! I really don\'t know how to do this it\'s
ID: 3668868 • Letter: P
Question
Please help with Python Idle problem! I really don't know how to do this it's really confusing and I like using chegg as a referal to check if i'm doing it right since it takes a while for people to answer questions like these anyways. I'm using python Idle (python GUI) 3.4.0 I will be posting screenshots of the guidlines. Thank you so much :) Here is the median code. Even just explaining it will help me :) Note: is_memberR will need a wrapper function pls help anything will help at this point.
def median(alist):
copylist = alist[:]
copylist.sort()
if len(copylist)%2 == 0:
rightmid = len(copylist)//2
leftmid = rightmid - 1
median = (copylist[leftmid] + copylist[rightmid])//2
else:
mid = len(copylist)//2
median = copylist[mid]
return median
Explanation / Answer
Hi, It is quite simple.
I am commenting each line, just read my comment
// this a function that takes a list
def median(alist):
copylist = alist[:] // this line copy original list into a new list- 'copylist'
// we are making copy because we do not want to modify original one
copylist.sort() // this line sore the newly created list - copylist
if len(copylist)%2 == 0: // checking that list has even or odd number of elements,
// so, if length will be even then median will be average of two elements
// ex- 1,2,3,4 , median = (2+3)/2, so in next two line on code
// we are finding two indexes, say 'rightmid' and 'leftmid'
// in my example, lestmid will be index of 2 and rightmid will be index of 3
rightmid = len(copylist)/2 // finding index of median element that is right side
leftmid = rightmid - 1 // index of leftside element
median = (copylist[leftmid] + copylist[rightmid])/2 // calculating median
else:
mid = len(copylist)/2 // and if length of list is odd then only one element will participate
// in median. ex- 1,2,3,4,5 -- here median is 3 (only one element)
median = copylist[mid] // calculating median in case of odd lenght
return median // returning median
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.