SHARE
TWEET

Untitled

a guest Nov 8th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import operator
  2. from math import sqrt
  3. from functools import reduce
  4. from statistics import median
  5.  
  6. def cropND(img, bounding):
  7.     start = tuple(map(lambda a, da: a//2-da//2, img.shape, bounding))
  8.     end = tuple(map(operator.add, start, bounding))
  9.     slices = tuple(map(slice, start, end))
  10.     return img[slices]
  11.  
  12. ##############################################################
  13. ### cartesian product of lists ##################################
  14. ##############################################################
  15.  
  16. def appendEs2Sequences(sequences,es):
  17.     result=[]
  18.     if not sequences:
  19.         for e in es:
  20.             result.append([e])
  21.     else:
  22.         for e in es:
  23.             result+=[seq+[e] for seq in sequences]
  24.     return result
  25.  
  26.  
  27. def cartesianproduct(lists):
  28.     """
  29.     given a list of lists,
  30.     returns all the possible combinations taking one element from each list
  31.     The list does not have to be of equal length
  32.     """
  33.     return reduce(appendEs2Sequences,lists,[])
  34.  
  35. ##############################################################
  36. ### prime factors of a natural ##################################
  37. ##############################################################
  38.  
  39. def primefactors(n):
  40.     '''lists prime factors, from greatest to smallest'''
  41.     i = 2
  42.     while i<=sqrt(n):
  43.         if n%i==0:
  44.             l = primefactors(n/i)
  45.             l.append(i)
  46.             return l
  47.         i+=1
  48.     return [n]      # n is prime
  49.  
  50.  
  51. ##############################################################
  52. ### factorization of a natural ##################################
  53. ##############################################################
  54.  
  55. def factorGenerator(n):
  56.     p = primefactors(n)
  57.     factors={}
  58.     for p1 in p:
  59.         try:
  60.             factors[p1]+=1
  61.         except KeyError:
  62.             factors[p1]=1
  63.     return factors
  64.  
  65. def divisors(n):
  66.     factors = factorGenerator(n)
  67.     divisors=[]
  68.     listexponents=[map(lambda x:k**x,range(0,factors[k]+1)) for k in factors.keys()]
  69.     listfactors=cartesianproduct(listexponents)
  70.     for f in listfactors:
  71.         divisors.append(reduce(lambda x, y: x*y, f, 1))
  72.     divisors = list(set(divisors))
  73.     divisors.sort()
  74.     return divisors
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top