Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Purpose In this project you will need to design and implement recursive and iterative algorithms for determining list inclusion using binary search. It is not a long or complicated project, but it requires careful design Binary search In computer science, a binary search algorithm finds the position of a target value within a sorted list. The binary search algorithm begins by comparing the target value to the value of the middle element of the sorted list. If the target value is equal to the middle element's value, then the position is returned and the search is finished. If the target value is less than the middle element's value, then the search continues on the lower half of the list; or if the target value is greater than the middle element's value, then the search continues on the upper half of the list. This process continues, eliminating half of the elements, and comparing the target value to the value of the middle element of the remaining elements - until the target value is either found (and its associated element position is returned), or until the entire list has been searched (and a not found value is returned). Binary search algorithms are important, as they have much shorter running times than linear search algorithms, which may need to search the entire list. Example A list: alist [4, 3, 8, 9, 1, 11, 6] Sorted list : alist [1, 3, 4, 6, 8, 9, 11] Target value: x 4 Compare x to 6; x is smaller, so repeat with [1, 3, 4] Compare x to 3; x is larger, so repeat with [4] Compare x to 4; x -- 4, so the position is returned Requirements Implement functions is_memberR and is_memberl, with two parameters, alist, a sorted list of integers, and target, an integer. All integers will have a value greater than or equal to 0. is_memberR and is_memberl will return True if target is an element of the the list, and False otherwise. Your implementations must implement the binary search algorithm described above, with the exception that we do not need to know the index for the matched element in the list, we simply need to know True or False. When function is memberR recursively invokes itself, the list it passes on the recursive call should be a slice of the original list. Test your functions using the following (change X to R or I) # even number of items in list-False >>>is_memberX([1, 3, 5, 7], 4) False >>>is_memberX([23, 24, 25, 26, 27], 5) False # odd number of items in list-False is memberX(IQ, 1, 4, 5, 6, 81. 4) # even number of items in list-True

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote