update with eol lf
This commit is contained in:
parent
61ac578636
commit
83308d74e5
18
python/17.py
Normal file
18
python/17.py
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
def under_20():
|
||||
words = 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen'
|
||||
return len(words.replace(' ', ''))
|
||||
|
||||
def under_100():
|
||||
ten_words = 'twenty thirty forty fifty sixty seventy eighty ninety'
|
||||
digit_words = 'one two three four five six seven eight nine'
|
||||
return len(ten_words.replace(' ', '')) * 10 + len(digit_words.replace(' ', '')) * 8 + under_20()
|
||||
|
||||
def handreds():
|
||||
words = 'one hundred two hundred three hundred four hundred five hundred six hundred seven hundred eight hundred nine hundred'
|
||||
return len(words.replace(' ', '')) * 100 + len('and') * 99 * 9 + under_100() * 10
|
||||
|
||||
def count():
|
||||
return handreds() + len('onethousand')
|
||||
|
||||
print(count())
|
@ -13,6 +13,5 @@ def file_get():
|
||||
context = f.read().replace('"', '').split(',')
|
||||
return list(filter(lambda x: is_tri_num(x), map(lambda x: word_num(x), context)))
|
||||
|
||||
open('../resource/words')
|
||||
|
||||
print(len(file_get()))
|
||||
|
||||
|
35
python/44.1.py
Normal file
35
python/44.1.py
Normal file
@ -0,0 +1,35 @@
|
||||
import time
|
||||
|
||||
def sum_is_five(m, n):
|
||||
test = 3 *((3 * m - 1) * m + (3 * n - 1) * n)
|
||||
sqrt = int(test ** 0.5) + 1
|
||||
if sqrt * (sqrt - 1) == test:
|
||||
q, r = divmod(sqrt * 2, 6)
|
||||
if not r:
|
||||
return q
|
||||
return 0
|
||||
|
||||
def gen_five(start):
|
||||
five = (3 * start - 1) * start // 2
|
||||
while True:
|
||||
yield start, five
|
||||
five += 3 * start + 1
|
||||
start += 1
|
||||
|
||||
def search():
|
||||
stage = (0, 0, 0)
|
||||
for n, fn in gen_five(1):
|
||||
print(n, fn)
|
||||
for m, fm in gen_five(n + 1):
|
||||
if 3 * m + 1 > fn:
|
||||
break
|
||||
if 0 < stage[0] <= n:
|
||||
return stage
|
||||
x = sum_is_five(m, n)
|
||||
if x:
|
||||
if sum_is_five(m, x):
|
||||
return (n, m, x)
|
||||
if sum_is_five(n, x):
|
||||
stage = (m, n, x)
|
||||
|
||||
print(search())
|
32
python/44.py
32
python/44.py
@ -1,20 +1,20 @@
|
||||
|
||||
def test(x):
|
||||
sq = 12 * x + 1
|
||||
ss = int(sq ** 0.5)
|
||||
if ss * ss != sq:
|
||||
return False
|
||||
if ss % 6 != 5:
|
||||
return False
|
||||
return True
|
||||
sq = 12 * x + 1
|
||||
ss = int(sq ** 0.5)
|
||||
if ss * ss != sq:
|
||||
return False
|
||||
if ss % 6 != 5:
|
||||
return False
|
||||
return True
|
||||
|
||||
def main(limit):
|
||||
for d in xrange(1, limit):
|
||||
for n in xrange(5, limit):
|
||||
m = n + d
|
||||
a = 3 * (m * m + n * n) - m - n
|
||||
b = d * (3 * (m + n) - 1)
|
||||
if test(a) and test(b):
|
||||
print a / 2, b / 2, m, n
|
||||
return
|
||||
for d in range(1, limit):
|
||||
for n in range(5, limit):
|
||||
m = n + d
|
||||
a = 3 * (m * m + n * n) - m - n
|
||||
b = d * (3 * (m + n) - 1)
|
||||
if test(a) and test(b):
|
||||
return b // 2
|
||||
|
||||
main(1300)
|
||||
print(main(1300))
|
||||
|
19
python/45.py
19
python/45.py
@ -1,8 +1,11 @@
|
||||
m = 166
|
||||
while 1:
|
||||
five = m * (3 * m - 1)
|
||||
n = int(five ** 0.5)
|
||||
if five == n * (n + 1) and n % 2 == 1:
|
||||
break
|
||||
m += 1
|
||||
print m, (n + 1) / 2, m * (3 * m - 1) / 2
|
||||
|
||||
def search(m):
|
||||
while True:
|
||||
five = m * (3 * m - 1)
|
||||
n = int(five ** 0.5)
|
||||
if n * (n + 1) == five and n % 2:
|
||||
break
|
||||
m += 1
|
||||
return m * (3 * m - 1) // 2
|
||||
|
||||
print(search(166))
|
||||
|
56
python/49.py
56
python/49.py
@ -1,48 +1,16 @@
|
||||
def alanum(x):
|
||||
lis = []
|
||||
while x != 0:
|
||||
lis.append(x % 10)
|
||||
x /= 10
|
||||
return lis
|
||||
|
||||
def pick(x, lis, out, a = 0):
|
||||
if x == 0:
|
||||
out.append([a, lis])
|
||||
return
|
||||
a *= 10
|
||||
for i in range(len(lis)):
|
||||
tmp = lis[:]
|
||||
tmpa = a + lis[i]
|
||||
tmp.pop(i)
|
||||
pick(x - 1, tmp, out, tmpa)
|
||||
from tools import number_theory
|
||||
|
||||
def isp(x):
|
||||
if x == 2:
|
||||
return True
|
||||
if x <= 1 or x & 1 == 0:
|
||||
return False
|
||||
for i in range(3, int(x ** 0.5) + 1, 2):
|
||||
if x % i == 0:
|
||||
return False
|
||||
return True
|
||||
def same(x, y):
|
||||
return str(sorted(str(x))) == str(sorted(str(y)))
|
||||
|
||||
def main():
|
||||
n = 1489
|
||||
while 1:
|
||||
if isp(n):
|
||||
tmp = alanum(n)
|
||||
ttmp = []
|
||||
pick(4, tmp, ttmp)
|
||||
tt = []
|
||||
for ti in ttmp:
|
||||
tt.append(ti[0])
|
||||
#print n, tmp, tt
|
||||
while tt.count(n) > 0:
|
||||
tt.remove(n)
|
||||
for ii in tt:
|
||||
jj = 2 * ii - n
|
||||
if jj > 0 and isp(ii) and isp(jj) and tt.count(jj):
|
||||
return str(n) + str(ii) + str(jj) #(n, ii, jj)
|
||||
n += 1
|
||||
def search(limit):
|
||||
prime = list(filter(lambda x: x > 1487, number_theory.make_prime(limit)))
|
||||
for i, pi in enumerate(prime):
|
||||
for pj in prime[i + 1:]:
|
||||
if same(pi, pj):
|
||||
pn = 2 * pj - pi
|
||||
if same(pn, pi) and pn in prime:
|
||||
return str(pi) + str(pj) + str(pn)
|
||||
|
||||
print(main())
|
||||
print(search(10000))
|
||||
|
102
python/54.py
102
python/54.py
@ -1,61 +1,49 @@
|
||||
#valuetab = ('High Card', 'One Pair', 'Two Pairs', 'Three of a Kind', 'Straight', 'Flush', 'Full House', 'Four of a Kind', 'Straight Flush', 'Royal Flush')
|
||||
|
||||
trans = {'A':14, 'T':10, 'J':11, 'Q':12, 'K':13}
|
||||
def is_order(num):
|
||||
mini = min(num)
|
||||
return list(range(mini, mini + len(num))) == list(num)
|
||||
|
||||
def calc(x):
|
||||
color = []
|
||||
num = []
|
||||
for i in x:
|
||||
color.append(i[1])
|
||||
if '1' < i[0] <= '9':
|
||||
num.append(ord(i[0]) - ord('0'))
|
||||
else:
|
||||
num.append(trans.get(i[0]))
|
||||
num.sort()
|
||||
num.append(0)
|
||||
step = 1
|
||||
dic = {4:[], 3:[], 2:[], 1:[]}
|
||||
same = 1
|
||||
for i in range(1, len(num)):
|
||||
diff = num[i] - num[i - 1]
|
||||
if diff == 0:
|
||||
same += 1
|
||||
else:
|
||||
dic.get(same).append(num[i - 1])
|
||||
same = 1
|
||||
if diff == 1:
|
||||
step += 1
|
||||
if len(dic.get(4)):
|
||||
return (7, dic.get(4)[0], dic.get(1))
|
||||
if len(dic.get(3)) == len(dic.get(2)) == 1:
|
||||
return (6, dic.get(3)[0], dic.get(2)[0])
|
||||
flag = 0
|
||||
if len(set(color)) == 1:
|
||||
if step == 5:
|
||||
return (8, dic.get(1)[-2])
|
||||
flag = 5
|
||||
if step == 5:
|
||||
return (flag, 4, num[-2])
|
||||
if len(dic.get(3)):
|
||||
dic.get(1).reverse()
|
||||
return (flag, 3, dic.get(3)[0], dic.get(1))
|
||||
if len(dic.get(2)) > 1:
|
||||
dic.get(2).reverse()
|
||||
return (flag, 2, dic.get(2), dic.get(1))
|
||||
dic.get(1).reverse()
|
||||
if len(dic.get(2)):
|
||||
return (flag, 1, dic.get(2)[0], dic.get(1))
|
||||
return (flag, 0, dic.get(1))
|
||||
def anlz_num(num):
|
||||
anlz = {1: [], 2: [], 3: [], 4: []}
|
||||
for n in set(num):
|
||||
anlz[num.count(n)].append(n)
|
||||
if len(anlz[4]):
|
||||
return (8, max(anlz[4]))
|
||||
if len(anlz[3]):
|
||||
if len(anlz[2]):
|
||||
return (7, max(anlz[3]))
|
||||
else:
|
||||
return (4, max(anlz[3]))
|
||||
if len(anlz[2]) == 2:
|
||||
return (3, max(anlz[2]))
|
||||
elif len(anlz[2]):
|
||||
return (2, max(anlz[2]))
|
||||
return (1, max(anlz[1]))
|
||||
|
||||
def main():
|
||||
ff = open('../resource/poker.txt', 'r')
|
||||
out = 0
|
||||
for line in ff.readlines():
|
||||
strlis = line.split(' ')
|
||||
if calc(strlis[0:5]) > calc(strlis[5:]):
|
||||
out += 1
|
||||
ff.close()
|
||||
return out
|
||||
def score(p):
|
||||
follow = set(p[1::3])
|
||||
number = list(sorted(map(lambda x: '23456789TJQKA'.find(x), p[0::3])))
|
||||
if is_order(number):
|
||||
if len(follow) == 1:
|
||||
return (9, max(number))
|
||||
else:
|
||||
return (5, max(number))
|
||||
if len(follow) == 1:
|
||||
return (6, max(number))
|
||||
return anlz_num(number)
|
||||
|
||||
#print calc(['7C','4C','4C','4C','7C'])
|
||||
print(main())
|
||||
def seek_file(fn):
|
||||
with open(fn, 'r') as f:
|
||||
l = f.readline()
|
||||
while l:
|
||||
yield l[:14], l[15:-1]
|
||||
l = f.readline()
|
||||
|
||||
def judge():
|
||||
count = 0
|
||||
for p, q in seek_file('../resource/poker.txt'):
|
||||
if score(p) > score(q):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
print(judge())
|
||||
|
26
python/59.py
26
python/59.py
@ -1,18 +1,12 @@
|
||||
from string import atoi
|
||||
|
||||
fi = open('../lib/cipher1.txt', 'r')
|
||||
st = fi.read()
|
||||
li = st.split(',')
|
||||
for i in xrange(len(li)):
|
||||
li[i] = atoi(li[i])
|
||||
def get_file(fn):
|
||||
with open(fn, 'r') as f:
|
||||
return list(map(lambda x: int(x), f.read().split(',')))
|
||||
|
||||
'''['g', 'o', 'd']'''
|
||||
de = [103, 111, 100]
|
||||
count = 0
|
||||
string = ''
|
||||
for i in xrange(len(li)):
|
||||
count += li[i] ^ de[i % 3]
|
||||
tmp = chr(li[i] ^ de[i % 3])
|
||||
string += tmp
|
||||
print string
|
||||
print count
|
||||
def xor_list(p, q):
|
||||
return list(map(lambda x: x[0] ^ x[1], zip(p * (len(q) // len(p) + 1), q)))
|
||||
|
||||
def decrypt():
|
||||
return sum(xor_list([103, 111, 100], get_file('../resource/cipher1.txt')))
|
||||
|
||||
print(decrypt())
|
||||
|
@ -18,7 +18,6 @@ def tt_ori(x, f, fd, flag):
|
||||
tmp = int(sqrt(flag * x))
|
||||
tmp_ = 0
|
||||
while tmp_ != tmp:
|
||||
#print '*', tmp
|
||||
tmp_ = tmp
|
||||
tmp -= (poly(tmp, f) - flag * x) / poly(tmp, fd)
|
||||
while poly(tmp, f) - flag * x > 0:
|
||||
|
30
python/90.py
Normal file
30
python/90.py
Normal file
@ -0,0 +1,30 @@
|
||||
|
||||
def gen_iter(result, digit, pool, pick):
|
||||
if not digit:
|
||||
result.append(pick)
|
||||
else:
|
||||
for i in range(len(pool) - digit + 1):
|
||||
gen_iter(result, digit - 1, pool[i + 1:], pick + pool[i])
|
||||
|
||||
def gen_pick(digit, pool):
|
||||
result = []
|
||||
gen_iter(result, digit, pool, '')
|
||||
return result
|
||||
|
||||
def judge(state, c1, c2):
|
||||
for p1, p2 in state:
|
||||
if not ((p1 in c1 and p2 in c2) or (p2 in c1 and p1 in c2)):
|
||||
return False
|
||||
return True
|
||||
|
||||
def search():
|
||||
state = [('0', '1'), ('0', '4'), ('0', '6'), ('1', '6'), ('2', '5'), ('3', '6'), ('4', '6'), ('6', '4'), ('8', '1')]
|
||||
tale = []
|
||||
cube = gen_pick(6, '0123456786')
|
||||
for i in range(len(cube)):
|
||||
for j in range(i, len(cube)):
|
||||
if judge(state, cube[i], cube[j]):
|
||||
tale.append((cube[i], cube[j]))
|
||||
return len(tale)
|
||||
|
||||
print(search())
|
Loading…
x
Reference in New Issue
Block a user