Skip to content

Instantly share code, notes, and snippets.

View pythonhacker's full-sized avatar
💭
Full Reset

Anand B Pillai pythonhacker

💭
Full Reset
View GitHub Profile
@pythonhacker
pythonhacker / iq_range_dict.py
Created January 6, 2026 13:25
IQ function rewritten using the custom range dictionary
def IQ(score):
""" IQ function using range dictionary """
scores_class_dict={0: 'intellectually disabled',
70: 'below average',
85: 'average',
115: 'bright',
130: 'gifted',
145: 'highly gifted',
160: 'exceptionally gifted',
@pythonhacker
pythonhacker / range_dict.py
Last active January 8, 2026 09:57
A custom range dictionary using bisect
class RangeDict(dict):
""" A dictionary mapping a key to a range of integers """
def __getitem__(self, key):
sorted_keys = sorted(self.keys())
idx = bisect.bisect_right(sorted_keys, key)
# Mapped key is one down from index but we don't
# want to cycle using negative indices
if idx > 0: idx -= 1
mapped_key = sorted_keys[idx]
@pythonhacker
pythonhacker / iq_bisect.py
Created January 6, 2026 13:00
IQ function using bisect module
def IQ(score):
ranges = (70, 85, 115, 130, 145, 160, 180)
rating = ("intellectually disabled",
"below average",
"average",
"bright",
"gifted",
"highly gifted",
"exceptionally gifted",
"profoundly gifted/genius")
@pythonhacker
pythonhacker / iq_demo.py
Created January 6, 2026 12:56
IQ function demo
>>> IQ(65)
'intellectually disabled'
>>> IQ(100)
'average'
>>> IQ(125)
'bright'
>>> IQ(200)
'profoundly gifted/genius'
@pythonhacker
pythonhacker / IQ.py
Created January 6, 2026 12:55
An IQ function written from first principles
def IQ(score):
""" IQ score -> rating """
if score < 70:
return "intellectually disabled"
elif score in range(70, 85):
return "below average"
elif score in range(85, 115):
return "average"
elif score in range(115, 130):
@pythonhacker
pythonhacker / occurences_demo.py
Created January 6, 2026 12:50
Demo of the occurrences function using bisect
>>> l=[10, 12, 15, 17, 18, 20, 20, 21, 21, 21, 24, 24]
>>> occurs(l, 18)
range(4, 5)
>>> occurs(l, 20)
range(5, 7)
>>> list(occurs(l, 21))
[7, 8, 9]
@pythonhacker
pythonhacker / occurences.py
Created January 6, 2026 12:47
Return the occurences of an element in a sorted list using bisect
def occurs(a, x):
""" Return all occurrences of x in a """
start = search(a, x)
end = search_right(a, x)
if start != -1 and end != -1:
return range(start, end, 1)
@pythonhacker
pythonhacker / list_search_demo.py
Last active January 6, 2026 12:43
Generic list search demo using bisect
>>> l=[10, 12, 15, 17, 18, 20, 20, 21, 21, 21, 24, 24]
>>> search(l, 18)
4
>>> search(l, 20)
5
>>> search(l, 21)
7
>>> search_right(l, 21)
10
>>> search_right(l, 24)
@pythonhacker
pythonhacker / list_search.py
Created January 6, 2026 12:39
Generic sorted list search and locate using bisect
def search(a, x):
""" Locate the index of the leftmost value exactly equal to x """
i = bisect.bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
return -1
def search_right(a, x):
""" Locate the index of the right most value exactly equal to x """
@pythonhacker
pythonhacker / insort_demo.py
Last active January 6, 2026 12:38
Bisect insort functions demo
>>> bisect.insort_left(l, 12)
>>> l
[10, 12, 15, 18, 20, 21, 21, 21, 24, 24]
>>> bisect.insort_right(l, 20)
>>> l
[10, 12, 15, 18, 20, 20, 21, 21, 21, 24, 24]
>>> bisect.insort_left(l, 17)
>>> l
[10, 12, 15, 17, 18, 20, 20, 21, 21, 21, 24, 24]