Skip to content

Instantly share code, notes, and snippets.

View narendranathjoshi's full-sized avatar

Narendra Nath Joshi narendranathjoshi

View GitHub Profile
@narendranathjoshi
narendranathjoshi / count_rows_in_all_tables.sql
Created April 9, 2020 02:19
Count Rows in All Tables in PostgreSQL
SELECT schemaname,relname,n_live_tup
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC;
@narendranathjoshi
narendranathjoshi / quicksort.py
Created June 23, 2016 07:15
Quick Sort One Liner in Python
qsort = lambda l : [x for x in l[1:] if x < l[0]] + [l[0]] + [x for x in l[1:] if x >= l[0]]
#usage
qsort([7,5,2,8])
@narendranathjoshi
narendranathjoshi / weighted_choice.py
Last active June 23, 2016 07:15
Weighted Choice in Python
def weighted_choice(choices):
total = sum(w for c, w in choices)
r = random.uniform(0, total)
upto = 0
for c, w in choices:
if upto + w >= r:
return c
upto += w
assert False, "Shouldn't get here"
@narendranathjoshi
narendranathjoshi / iterctr.py
Last active April 10, 2020 19:24
Print every nth element when iterating over large lists - Python 3
def iterctr(items, n):
ctr = 0
for item in items:
ctr += 1
if ctr % n == 0:
print(ctr)
yield item