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_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]
@pythonhacker
pythonhacker / binary_search_demo.py
Created January 6, 2026 12:24
Demo of binary search using bisect
>>> l = [10, 15, 18, 20, 21, 21, 21, 24, 24]
>>> binary_search(l, 18)
2
>>> binary_search(l, 21)
4
>>> binary_search(l, 24)
7
>>> binary_search(l, 100)
-1
@pythonhacker
pythonhacker / binary_search.py
Created January 6, 2026 12:17
Binary search using bisect module
import bisect
def binary_search(a, x, lo=0, hi=None):
""" Search for item x in a using bisect binary search """
if hi is None:
hi = len(a)
pos = bisect.bisect_left(a, x, lo, hi)
if pos != hi and a[pos] == x:
return pos