ClavinJune

pytohn utils

Oct 12th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. def is_anagram(a: str, b:str):
  2.   if len(a) != len(b): return False
  3.   return ''.join(sorted(list(a))) == ''.join(sorted(list(b)))
  4.  
  5. def is_palindrome(a: str, b:str):
  6.   return a[::-1] == b
  7.  
  8. def get_pow(a: int, b: int) -> int:
  9.   if b == 1: return a
  10.   if b == 0: return 1
  11.  
  12.   if b % 2 == 0:
  13.     x = get_pow(a, b/2)
  14.     return x * x
  15.   return a * get_pow(a, b-1)
  16.  
  17. def get_pow_modulo(a: int, b: int, c: int) -> int:
  18.   if b == 1: return a
  19.   if b == 0: return 1
  20.  
  21.   if b % 2 == 0:
  22.     x = get_pow(a, b/2, c)
  23.     return ((x%c) * (x%c))%c
  24.   return ((a%c) * (get_pow(a, b-1)%c))%c
  25.  
  26. def get_gcd(a: int, b: int) -> int:
  27.   if a == 0: return b
  28.   if b == 0: return a
  29.   return get_gcd(b, a%b)
  30.  
  31. def get_lcm(a: int, b: int) -> int:
  32.   return a*b // get_gcd(a, b)
  33.  
  34. def get_factorial(a: int) -> int:
  35.   if a < 2: return 1
  36.   return get_factorial(a-1) * a
  37.  
  38. def get_combination(n: int, r: int) -> int:
  39.   n_r = n-r
  40.   divisor = max(n_r, r)
  41.   r = get_factorial(min(n_r, r))
  42.   total = 1
  43.   for i in range(divisor+1, n+1):
  44.     gcd = get_gcd(i, r)
  45.     total *= i//gcd
  46.     if r == 1: continue
  47.     r //= gcd
  48.   return total
  49.  
  50. def get_permutation(n: int, r: int) -> int:
  51.   total = 1
  52.   for i in range(n-r+1, n+1):
  53.     total *= i
  54.   return total
  55.  
  56. def get_substring_index(haystack: str, needle: str, prime: int=101) -> int:
  57.   # using rabin-karp
  58.   H = len(haystack)
  59.   N = len(needle)
  60.   h = 0 # hash value for haystack
  61.   n = 0 # hash value for needle
  62.   d = 256 # number of characters in the input alphabet
  63.  
  64.   x = get_pow_modulo(256, N-1, prime)
  65.  
  66.   for i in range(N):
  67.     n =
  68.     h =
  69.  
  70. # get_substring_index("ASD", "asd"  )
Add Comment
Please, Sign In to add comment