Skip to content

Instantly share code, notes, and snippets.

View codecakes's full-sized avatar
💭
I may be slow to respond.

codecakes codecakes

💭
I may be slow to respond.
View GitHub Profile
@codecakes
codecakes / numBits.py
Created October 2, 2019 18:50
Count number of bits in a positively signed number.
def countBits(num):
'''Count number of bits in a positively signed number.'''
count = 0
for i in iter(lambda: fn(num), 0):
num = i
count += 1
return count + 1
@codecakes
codecakes / str_subsequences.py
Last active October 13, 2019 16:31
Find all possible substrings
# Slow
sub = lambda astring: [''.join([astring[idx] for idx, each in enumerate("{0:0b}".format(i)) if each == '1']) for i in range(pow(2, len(astring)))]
# Better recursive
def subsequences(chars, n, pt=0, res=''):
'''Print all subsequences of a string.'''
# print(pt, n)
if pt == n:
print(repr(res))
return
@codecakes
codecakes / xor1toN.py
Created October 3, 2019 10:54
XOR of all numbers from 1 to N.
def xorN(n):
'''Xor of all numbers from 1 to n.'''
rem = n%4
if rem ==0:
return n
xord = functools.reduce(lambda x,y: x^y, range(1, rem+1))
return xord if xord < 2 else n+1
@codecakes
codecakes / maxAndOr.py
Last active October 3, 2019 12:10
Find Max AND and OR values and their sum
import math
binaryLen = lambda n: math.floor(math.log2(n)) + 1
def findNextMx(mx, ar , exclude_list):
'''Find the next max of same bin len as mx.'''
logmx = binaryLen(mx)
nextmx = float('-inf')
for n in ar:
if n not in exclude_list and n > nextmx:
@codecakes
codecakes / longest_span_bit_sum.py
Created October 8, 2019 16:25
Longest Span with same Sum in two Binary arrays
def largestSpan(a_bin, b_bin):
a_bin_ln = len(a_bin)
b_bin_ln = len(b_bin)
max_ij = max(a_bin_ln, b_bin_ln)
a_bin = a_bin.zfill(max_ij)
b_bin = b_bin.zfill(max_ij)
a_bin, b_bin = list(map(int, a_bin)), list(map(int, b_bin))
maxLen = diff = 0
start = 0
cutoff = {}
@codecakes
codecakes / binary_factorization.py
Last active October 13, 2019 15:44
represent a number in terms of its binary powers of 2 which can be set to 1s and 0s
import math
fn = lambda num: math.floor(math.log2(num))
def binary_factorization(num):
x = fn(num)
pw = 2**x
rem = num%pw
return pw, (fact2(rem) if rem else rem)
@codecakes
codecakes / max_cut.py
Last active October 13, 2019 15:43
maximum number of divisions of N or max num of cuts that add up to make N
import functools
@functools.lru_cache(maxsize = 100_000, typed=True)
def maxCut(n, a,b,c):
if n <0: return -1
if n == 0: return 0
res = max([
maxCut(n-a, a,b,c),
maxCut(n-b, a,b,c),
maxCut(n-c, a,b,c)])
@codecakes
codecakes / power_of_num_mod.py
Last active October 14, 2019 09:15
power of (number**reverse of number)
# See: https://practice.geeksforgeeks.org/problems/power-of-numbers/1/?ref=self
import functools
_MODNUM = 1000000007
# runs fine w/o lru_cache.
functools.lru_cache(maxsize=_MODNUM, typed=True)
def power(N,R):
if R == 0:
def printNos(N, start = 1, result=''): def powerSet(s, pos = 0, ln = 0, res=None):
if N == 0: '''
print(result.strip()) :param s: given string s
return :return: list containing power set of s.
result += "{0} ".format(start) '''
return printNos(N-1, start+1, result)
@codecakes
codecakes / str_subsequences_recursion.py
Created October 14, 2019 12:26
Power Set Using Recursion
# See: https://practice.geeksforgeeks.org/problems/power-set-using-recursion/1/?ref=self
def powerSet(s, pos = 0, ln = 0, res=None):
'''
:param s: given string s
:return: list containing power set of s.
'''
res = res or ''
ln = ln or len(s)
if pos == ln: