Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search_student fucntion on page 147 returns wrong result. #205

Open
gan3i opened this issue Dec 26, 2020 · 2 comments
Open

search_student fucntion on page 147 returns wrong result. #205

gan3i opened this issue Dec 26, 2020 · 2 comments

Comments

@gan3i
Copy link

gan3i commented Dec 26, 2020

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.

@mtorabirad
Copy link

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

@gan3i
Copy link
Author

gan3i commented Apr 18, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants