Skip to content

Instantly share code, notes, and snippets.

@blzzua
Last active March 1, 2025 09:51
Show Gist options
  • Save blzzua/b455836730555a4a13e78858be6cb1ff to your computer and use it in GitHub Desktop.
Save blzzua/b455836730555a4a13e78858be6cb1ff to your computer and use it in GitHub Desktop.
codewars python tip and tricks
# ravel + zip_longest
def compound_array(*args):
res = []
while any(arr for arr in args):
for arr in args:
if arr:
res.append(arr.pop(0))
return res
# ravel + zip_shortest
# while all(..):
# detecting a continuously non-decreasing subsequence(first case - 4 elements) in an array arr:
any(a <= b <= c <= d for a, b, c, d in zip(*(arr[diff:] for diff in range(4))))
# detecting a continuously non-decreasing subsequence(common case - N elements) in an array arr:
any(all(a<=b for a,b in zip(subseq,subseq[1:])) for subseq in zip(*(arr[diff:] for diff in range(N))))
def from_dec_to_base(n, to_base):
res = ''
while n > 0:
n, rem = divmod(n, to_base)
res = al[rem] + res
return res or '0'
def from_base_to_dec(n_str, from_base):
res = 0
for i, d in enumerate(n_str[::-1]):
res += al.index(d) * (from_base ** i)
return res
# also libs:
from numpy import base_repr
base_repr(int(number, from_base), to_base)
from gmpy2 import mpz
mpz(number, base=from_base).digits(to_base)
def digit_sum(n):
res = 0
while n > 0:
n, rem = divmod(n, 10)
res += rem
return res
import math
def divisors(n): # generator
large_divisors = []
for i in range(1, math.isqrt(n) + 1):
if n % i == 0:
yield i
if i*i != n:
large_divisors.append(n // i)
for divisor in reversed(large_divisors):
yield divisor
def divisors(n): # generator
res = []
large_divisors = []
for i in range(1, math.isqrt(n) + 1):
if n % i == 0:
res.append(i)
if i*i != n:
large_divisors.append(n // i)
return res + reversed(large_divisors)
list(divisors(12))
[1, 2, 3, 4, 6, 12]
# return middle element. if len even - return two middle elements:
arr = '123456'
return '34'
arr = '1234567'
return '4'
m, d = divmod(len(arr), 2) # m = middle, d = division rest
# variant1
return arr[m - (1 - d) : m + 1]
# variant2
return arr[m - (not d) : m + 1]
# variant3
return arr[m - (1 - d) ] + arr[m] * (1-d)
# variant4
r = 1-d
return arr[m - r] + arr[m] * r
import gmpy2
def factorize(n):
factors = []
while n > 1:
prime = gmpy2.next_prime(1)
while n % prime != 0:
prime = gmpy2.next_prime(prime)
factors.append(int(prime))
n //= prime
return factors
# yet another solution
def factorize(n):
if n <= 1:
return []
factors = []
prime = gmpy2.next_prime(2)
while prime * prime <= n:
if n % prime == 0:
factors.append(int(prime))
n //= prime
else:
prime = gmpy2.next_prime(prime)
else:
if n > 1:
factors.append(int(n))
return factors
def quicksort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
left = [x for x in arr[1:] if x < pivot]
right = [x for x in arr[1:] if x >= pivot]
return quicksort(left) + [pivot] + quicksort(right)
arr = [i for i in range(1,37)] # [1,2,3,...,36]
list(zip(*[iter(arr)] * 4))
[(1, 2, 3, 4),
(5, 6, 7, 8),
(9, 10, 11, 12),
(13, 14, 15, 16),
(17, 18, 19, 20),
(21, 22, 23, 24),
(25, 26, 27, 28),
(29, 30, 31, 32),
(33, 34, 35, 36)]
list(zip(*zip(*[iter(arr)] * 4)))
[(1, 5, 9, 13, 17, 21, 25, 29, 33),
(2, 6, 10, 14, 18, 22, 26, 30, 34),
(3, 7, 11, 15, 19, 23, 27, 31, 35),
(4, 8, 12, 16, 20, 24, 28, 32, 36)]
all subcombinations from list
somelist = [1,2,3,4,5]
[s for s in itertools.chain.from_iterable(itertools.combinations(somelist, j) for j in range(1,len(somelist)+1))]
[(1,), (2,), (3,), (4,), (5,),
(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5),
(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5),
(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5),
(1, 2, 3, 4, 5)]
# regex for find for all cases when the case is changed
re.findall("[A-Z](?=[a-z])|[a-z](?=[A-Z])", string)
re.findall("[A-Z](?=[a-z])|[a-z](?=[A-Z])", 'ZEROoneTWOthreeFOUR')
['O', 'e', 'O', 'e']
"""
'ZEROoneTWOthreeFOUR'
^ ^ ^ ^
"""
import random
r = random.randint(1,50)
diapasons = ['less then 10', 'between 10 and 20', 'between 20 and 30', 'between 30 and 40', 'more than 40']
print(r,'is', diapasons[(r>10)+(r>20)+(r>30)+(r>40)])
# using dict last_true with condition as key. last added True - will be saved, and we can get it as last_true[True]:
last_true = { (r<10): 'less then 10',
(r>=10): 'between 10 and 20',
(r>=20): 'between 20 and 30',
(r>=30): 'between 30 and 40',
(r>40): 'more than 40' }
print(r,'is', last_true[True])
# tilde-operator for range expansion
# for instance n=10
range(10) -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# when we need for 1..10: (add 10+1 as n)
[*range(1,10+1)] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#but we can add -~
[*range(1,-~10)] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
how its work:
~x is equivalent to (-x) - 1. ~10 -> -11
-~x = x+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment