I am trying to understand the use of bisect function in the python's bisect module and how to use it with tuples. I have seen that it takes a list and a value to find the index to place the value. In my case the value is a tuple. This is from leetcode 981 https://leetcode.com/problems/time-based-key-value-store/. A is a list of tuple and the tuple will contain (int,string) as its data.
class TimeMap(object):
def __init__(self):
self.M = collections.defaultdict(list)
def set(self, key, value, timestamp):
self.M[key].append((timestamp, value))
def get(self, key, timestamp):
A = self.M.get(key, None)
if A is None: return ""
i = bisect.bisect(A, (timestamp, chr(127)))
return A[i-1][1] if i else ""
I understand what they are trying to do for solving the question . I just want to understand why use chr(127). I tried using None but it gives an error. I am guessing that it will always take the first int value in the tuple because the chr(127) will always be unqualified somehow for bisect to accept. But I am not able to find the correct reason for this. also IDLE shows chr(127) as \x7f and found out its a non ascii character.