-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.py
More file actions
190 lines (154 loc) · 4.16 KB
/
lib.py
File metadata and controls
190 lines (154 loc) · 4.16 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import operator, math
class memoize:
def __init__(self, f):
self.f = f
self.memo = {}
def __call__(self, *args):
if not args in self.memo:
self.memo[args] = self.f(*args)
return self.memo[args]
@memoize
def factorial(n):
return reduce(operator.mul, range(2,n+1), 1)
def choose(n, k):
return reduce(operator.mul, range(n-k+1, n+1), 1) / factorial(k)
def triangular(p):
return (p * (p+1)) / 2
def num_divisors(p):
num = 0
square_root = int(math.ceil(math.sqrt(p)))
for x in xrange(1, square_root):
if p % x == 0:
num += 2
if square_root * square_root == p:
num += 1
return num
def divisors(p):
div = set()
square_root = int(math.ceil(math.sqrt(p)))
for x in xrange(1, square_root+1):
if p % x == 0:
div.add(p/x)
div.add(x)
if p > 1: div.remove(p)
return div
def word_score(word):
return sum( (ord(x) - ord('A')+1) for x in word)
def get_primes(limit):
numbers = [True] * limit
numbers[0] = False
numbers[1] = False
for sieve in range(2, limit):
if not numbers[sieve]:
# find next prime
continue
yield sieve
for x in range(sieve+sieve, limit, sieve):
numbers[x] = False
_primes = set()
_non_primes = set()
_prime_limit = 1000000
def is_prime(x):
if len(_primes) == 0:
_primes.update(get_primes(_prime_limit))
if x in _primes:
return True
if x in _non_primes or x < _prime_limit:
return False
if x % 3 == 0 or x % 2 == 0:
_non_primes.add(x)
return False
for d in range(6, int(math.sqrt(x)), 6):
if x % (d+1) == 0 or x % (d-1) == 0:
_non_primes.add(x)
return False
_primes.add(x)
return True
def gcd(a, b):
if a > b:
a, b = b, a
while b != 0:
a, b = b, a % b
return a
def lcd(a, b):
return a * b / gcd(a, b)
def permute(seq, pred=cmp):
"""Like C++ std::next_permutation() but implemented as
generator. Yields copies of seq."""
def reverse(seq, start, end):
# seq = seq[:start] + reversed(seq[start:end]) + \
# seq[end:]
end -= 1
if end <= start:
return
while True:
seq[start], seq[end] = seq[end], seq[start]
if start == end or start+1 == end:
return
start += 1
end -= 1
if not seq:
raise StopIteration
try:
seq[0]
except TypeError:
raise TypeError("seq must allow random access.")
first = 0
last = len(seq)
seq = seq[:]
# Yield input sequence as the STL version is often
# used inside do {} while.
yield seq
if last == 1:
raise StopIteration
while True:
next = last - 1
while True:
# Step 1.
next1 = next
next -= 1
if pred(seq[next], seq[next1]) < 0:
# Step 2.
mid = last - 1
while not (pred(seq[next], seq[mid]) < 0):
mid -= 1
seq[next], seq[mid] = seq[mid], seq[next]
# Step 3.
reverse(seq, next1, last)
# Change to yield references to get rid of
# (at worst) |seq|! copy operations.
yield seq[:]
break
if next == first:
raise StopIteration
raise StopIteration
def is_square(n):
sq = int(math.sqrt(n))
return sq * sq == n
_fprimes = [2]
@memoize
def factors(n, first):
global _fprimes
if _fprimes[-1] < n:
_fprimes = list(get_primes(n*2))
factors = []
for p in _fprimes:
while n % p == 0:
factors.append(p)
n /= p
if n == 1:
break
return factors
def zipWith(f, a, b):
return map(f, zip(a,b))
def compose(f, g):
def fg(*a, **kw):
return f(g(*a, **kw))
return fg
def trace(f):
def inner(*a, **kw):
print '> calling', f.func_name, 'with ', a, kw
r = f(*a, **kw)
print '> returning', f.func_name, r
return r
return inner