This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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: |