EPIJudge icon indicating copy to clipboard operation
EPIJudge copied to clipboard

search_student fucntion on page 147 returns wrong result.

Open gan3i opened this issue 4 years ago • 2 comments

import collections
from typing import Tuple, List , Callable
import bisect

Student = collections.namedtuple("Student",('gpa','name'))

def comp_gpa(student : Student ) -> Tuple[float, str]:
    return (-student.gpa, student.name)

def search_student(students : List[Student], target : Student,
                comp_gpa : Callable[[Student], Tuple[float, str]]):

    i = bisect.bisect_left([comp_gpa(s) for s in students], comp_gpa(target))

    return 0 <= i < len(students) and students[i] == target

students = [Student(gpa,name) for gpa, name in zip([1,2,3,4],"abcd")]
print(search_student(students,Student(1, 'a'),comp_gpa))

it should print True but it prints False because, comp_gpa changes student_gpa to negative and that results in bisect_left returning the wrong index.

gan3i avatar Dec 26 '20 17:12 gan3i

@gan3i what does search_student function is even supposed to do?

mtorabirad avatar Apr 18 '21 12:04 mtorabirad

@mtorabirad search_student is supposed to check if the target(combination of gpa and name) is in the students list and return true incase it is present otherwise return false.

gan3i avatar Apr 18 '21 13:04 gan3i