Skip to content

Commit 9d120c5

Browse files
committed
Finished tasks
1 parent 2f31d16 commit 9d120c5

35 files changed

+1082
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

1-egg_crush.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def height(n,m):
2+
if m == 0 or n == 0:
3+
return 0
4+
if n>m:
5+
n = m
6+
bc = m
7+
cnt = bc
8+
for i in range(2, n+1):
9+
bc = bc * (m-i+1) // i
10+
cnt = (cnt + (bc % 998244353)) % 998244353
11+
return cnt
12+
13+
# print(height(80000, 100000))
14+
print(height(3000, 2**200))

2-ascii85.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import base64
2+
3+
# def toAscii85(data):
4+
# return '<~' + base64.a85encode(data.encode()).decode('ASCII') + '~>'
5+
6+
# def fromAscii85(data):
7+
# return base64.a85decode(data[2:-2].encode()).decode('ASCII')
8+
9+
def toAscii85(data):
10+
return '<~' + base64.a85encode(bytes([ord(c) for c in data])).decode('ascii') + '~>'
11+
12+
def fromAscii85(data):
13+
return ''.join([ chr(c) for c in base64.a85decode(data[2:-2].encode('ascii'))])
14+
15+
print('test\x99'.encode('eascii'))
16+
17+
# print(toAscii85('easy'), '<~ARTY*~>')
18+
# print(toAscii85('somewhat difficult'), '<~F)Po,GA(E,+Co1uAnbatCif~>')
19+
# print(fromAscii85('<~ARTY*~>'), 'easy')
20+
# print(fromAscii85('<~F)Po,GA(E,+Co1uAnbatCif~>'), 'somewhat difficult')

2-egg_crush.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import operator as op
2+
import functools
3+
import scipy.special
4+
5+
def ncr(n, r):
6+
r = min(r, n-r)
7+
if r == 0: return 1
8+
numer = functools.reduce(op.mul, range(n, n-r, -1))
9+
denom = functools.reduce(op.mul, range(1, r+1))
10+
return numer//denom
11+
12+
def height_rec(n,m):
13+
if m == 0 or n == 0:
14+
return 0
15+
if m == 1:
16+
return 1
17+
if n>m:
18+
n = m
19+
return height_rec(n-1, m-1) + 1 + height_rec(n, m-1)
20+
21+
def height2(n,m):
22+
if m == 0 or n == 0:
23+
return 0
24+
if n>m:
25+
n = m
26+
a = [0]*(m+1)
27+
b = list(range(m+1))
28+
b[0] = 0
29+
for row in range(2,n+1):
30+
a[row-1] = b[row-1]
31+
for col in range(row,m+1-(n-row)):
32+
a[col] = b[col-1] + 1 + a[col-1]
33+
a, b = b, a
34+
35+
return b[m]
36+
37+
def height3(n,m):
38+
if m == 0 or n == 0:
39+
return 0
40+
if n>m:
41+
n = m
42+
a = [0]*(n+1)
43+
a[n] = 1
44+
op = 1
45+
for row in range(m-1, 0, -1):
46+
a[0] += (row+1)*a[1]
47+
a[1] = a[2]
48+
for col in range(2, n):
49+
a[col] += a[col+1]
50+
op += a[col]
51+
return sum(a) + op
52+
53+
def height(n,m):
54+
if m == 0 or n == 0:
55+
return 0
56+
if n>m:
57+
n = m
58+
59+
# bc -> https://en.wikipedia.org/wiki/Binomial_coefficient
60+
# bc(m, 1)
61+
bc = m
62+
cnt = bc
63+
for i in range(2, n+1):
64+
# Hockey Stick Pattern - http://ptri1.tripod.com/
65+
# bc(m, i) = sum(bc(x, i-1)) for x in [i-1..m-1]
66+
bc = bc * (m-i+1) // i
67+
cnt += bc
68+
return cnt
69+
70+
71+
# return sum(map(lambda r: ncr(m,r), range(1, n+1)))
72+
73+
74+
# print(height(2,14) == height_rec(2,14))
75+
# print(height(7,20) == height_rec(7,20))
76+
77+
# print(height(17,2))
78+
79+
print(height_rec(5,8), height(5,8))
80+
print(height_rec(5,11), height(5,11))
81+
print(height_rec(5,12), height(5,12))
82+
83+
# print(height(7,500) == 1507386560013475)
84+
# print(height(237,500) == 431322842186730691997112653891062105065260343258332219390917925258990318721206767477889789852729810256244129132212314387344900067338552484172804802659)
85+
# print(height(477,500) == 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127420959866658939578436425342102468327399)
86+
# print(height(2,10000))
87+
# print(height(3,10000))
88+
# print(height(2500-1000,2000))
89+
# print(height(9500,10000))
90+
# print(height(5,5000))

2-infinite_string.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def find_position(string):
2+
3+
4+
5+
print(find_position("456") , 3,"...3456...")
6+
print(find_position("454") , 79,"...444546...")
7+
print(find_position("455") , 98,"...545556...")
8+
print(find_position("910") , 8,"...7891011...")
9+
print(find_position("9100") , 188,"...9899100...")
10+
print(find_position("99100") , 187,"...9899100...")
11+
print(find_position("00101") , 190,"...9899100...")
12+
print(find_position("001") , 190,"...9899100...")
13+
print(find_position("00") , 190,"...9899100...")
14+
print(find_position("123456789") , 0)
15+
print(find_position("1234567891") , 0)
16+
print(find_position("123456798") , 1000000071)
17+
print(find_position("10") , 9)
18+
print(find_position("53635") , 13034)
19+
print(find_position("040") , 1091)
20+
print(find_position("11") , 11)
21+
print(find_position("99") , 168)
22+
print(find_position("667") , 122)
23+
print(find_position("0404") , 15050)
24+
print(find_position("58257860625") , 24674951477)

2-regexp_div7.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
3+
# REGEXP = '(0*|1(0(111|01)*(00|110))*(1|0(111|01)*10)(01*0(0|11(111|01)*10|(10|11(111|01)*(00|110))(0(111|01)*(00|110))*(1|0(111|01)*10)))*1)*'
4+
REGEXP = '\A((0|1(0(111|01)*(00|110))*(1|0(111|01)*10)(01*0(0|11(111|01)*10|(10|11(111|01)*(00|110))(0(111|01)*(00|110))*(1|0(111|01)*10)))*1)0*)+\Z'
5+
6+
# for i in range(0, 10000):
7+
# if (re.fullmatch(REGEXP, '{:b}'.format(i)) != None) != (i%7 == 0):
8+
# print(i)
9+
10+
rgx = re.compile(REGEXP)
11+
12+
# num = 0
13+
14+
# while bool(rgx.match(bin(num)[2:])) == (num%7 == 0):
15+
# num += 1
16+
17+
# print('Testing for: '+str(num))
18+
# print(bin(num)[2:])
19+
# print(bool(rgx.match(bin(num)[2:])),num%7 == 0)
20+
21+
for num in range(0,50001):
22+
if bool(rgx.match(bin(num)[2:])) != (num%7 == 0):
23+
print('Testing for: '+str(num))
24+
print(bin(num)[2:])
25+
print(rgx.match(bin(num)[2:]),num%7 == 0)
26+
27+
# *1(0(111|01)*(00|110))*(1|0(111|01)*10)
28+
# 01*0(0|11(111|01)*10|)
29+
# (10|11(111|01)*(00|110))(0(111|01)*(00|110))*(1|0(111|01)*10)
30+
31+
# (0*1(0(111|01)*(00|110))*(1|0(111|01)*10)(01*0(0|11(111|01)*10|(10|11(111|01)*(00|110))(0(111|01)*(00|110))*(1|0(111|01)*10)))*1)*
32+
# (0*|1(0(111|01)*(00|110))*(1|0(111|01)*10)(01*0(0|11(111|01)*10|(10|11(111|01)*(00|110))(0(111|01)*(00|110))*(1|0(111|01)*10)))*1)*

3-atoms.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from collections import Counter
2+
3+
BRC_MATCH = {'(': ')', '[': ']', '{': '}'}
4+
5+
def parse_molecule(formula):
6+
atoms = Counter()
7+
elem, qnt, brc = formula[0], '', ''
8+
if formula[0] in '([{':
9+
elem = ''
10+
brc = BRC_MATCH[formula[0]]
11+
12+
for c in formula[1:]:
13+
if brc:
14+
if c == brc:
15+
brc = ''
16+
elem = parse_molecule(elem)
17+
else:
18+
elem += c
19+
elif 'A' <= c <= 'Z' or c in '([{':
20+
if isinstance(elem, Counter):
21+
atoms += Counter({key: val * (int(qnt) if qnt else 1) for key, val in elem.items()})
22+
else:
23+
atoms += Counter({elem: int(qnt) if qnt else 1})
24+
elem, qnt = c, ''
25+
if c in '([{':
26+
elem = ''
27+
brc = BRC_MATCH[c]
28+
elif 'a' <= c <= 'z':
29+
elem += c
30+
elif '0' <= c <= '9':
31+
qnt += c
32+
33+
if isinstance(elem, Counter):
34+
atoms += Counter({key: val * (int(qnt) if qnt else 1) for key, val in elem.items()})
35+
else:
36+
atoms += Counter({elem: int(qnt) if qnt else 1})
37+
return atoms
38+
39+
40+
41+
def equals_atomically(obj1, obj2):
42+
if len(obj1) != len(obj2):
43+
return False
44+
for k in obj1:
45+
if obj1[k] != obj2[k]:
46+
return False
47+
return True
48+
49+
50+
print(equals_atomically(parse_molecule("(C5H5)Fe(CO)2CH3"),
51+
{'H': 2, 'O': 1}), "Should parse water")
52+
# print(equals_atomically(parse_molecule("H2O"),
53+
# {'H': 2, 'O': 1}), "Should parse water")
54+
# print(equals_atomically(parse_molecule("Mg(OH)2"), {
55+
# 'Mg': 1, 'O': 2, 'H': 2}), "Should parse magnesium hydroxide: Mg(OH)2")
56+
# print(equals_atomically(parse_molecule("K4[ON(SO3)2]2"), {
57+
# 'K': 4, 'O': 14, 'N': 2, 'S': 4}), "Should parse Fremy's salt: K4[ON(SO3)2]2")

3-base64.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import base64
2+
3+
def to_base_64(string):
4+
return base64.b64encode(string.encode('ascii')).decode('ascii').replace('=', '')
5+
6+
def from_base_64(string):
7+
string += '=' * (len(string) % 4)
8+
return base64.b64decode(string.encode('ascii')).decode('ascii')
9+
10+
tests = [["this is a string!!","dGhpcyBpcyBhIHN0cmluZyEh"],
11+
["this is a test!","dGhpcyBpcyBhIHRlc3Qh"],
12+
["now is the time for all good men to come to the aid of their country.","bm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBtZW4gdG8gY29tZSB0byB0aGUgYWlkIG9mIHRoZWlyIGNvdW50cnku"],
13+
["1234567890 ", "MTIzNDU2Nzg5MCAg"],
14+
["ABCDEFGHIJKLMNOPQRSTUVWXYZ ", "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVog"],
15+
["the quick brown fox jumps over the white fence. ","dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSB3aGl0ZSBmZW5jZS4g"],
16+
["dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSB3aGl0ZSBmZW5jZS4","ZEdobElIRjFhV05ySUdKeWIzZHVJR1p2ZUNCcWRXMXdjeUJ2ZG1WeUlIUm9aU0IzYUdsMFpTQm1aVzVqWlM0"],
17+
["VFZSSmVrNUVWVEpPZW1jMVRVTkJaeUFna","VkZaU1NtVnJOVVZXVkVwUFpXMWpNVlJWVGtKYWVVRm5h"],
18+
["TVRJek5EVTJOemc1TUNBZyAg","VFZSSmVrNUVWVEpPZW1jMVRVTkJaeUFn"]]
19+
20+
for test in tests:
21+
result=to_base_64(test[0])
22+
print(result == test[1])
23+
print(from_base_64(result) == test[0])

3-calculator.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from decimal import Decimal
2+
import re
3+
4+
class Calculator(object):
5+
def evaluate(self, string):
6+
# parts = ['Decimal(' + part + ')' if re.fullmatch(r'\d+(\.\d+)?', part) else part for part in string.split(' ')]
7+
parts = map(lambda p: "Decimal('" + p + "')" if re.fullmatch(r'\d+(\.\d+)?', p) else p , string.split(' '))
8+
code = ' '.join(parts)
9+
print(code)
10+
11+
return float(eval(code))
12+
13+
14+
print(Calculator().evaluate("2 / 2 + 3 * 4 - 6"))
15+
print(Calculator().evaluate("1.1 * 2.2 * 3.3"))

3-did_you_mean.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Dictionary:
2+
def __init__(self,words):
3+
self.words=words
4+
5+
def __levenshtein(self, s1, s2):
6+
''' https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python '''
7+
if len(s1) < len(s2):
8+
return self.__levenshtein(s2, s1)
9+
10+
# len(s1) >= len(s2)
11+
if len(s2) == 0:
12+
return len(s1)
13+
14+
previous_row = range(len(s2) + 1)
15+
for i, c1 in enumerate(s1):
16+
current_row = [i + 1]
17+
for j, c2 in enumerate(s2):
18+
insertions = previous_row[j + 1] + 1 # j+1 instead of j since previous_row and current_row are one character longer
19+
deletions = current_row[j] + 1 # than s2
20+
substitutions = previous_row[j] + (c1 != c2)
21+
current_row.append(min(insertions, deletions, substitutions))
22+
previous_row = current_row
23+
24+
return previous_row[-1]
25+
26+
def find_most_similar(self,term):
27+
word = None
28+
diff = len(term)
29+
for w in self.words:
30+
tmp_diff = self.__levenshtein(term, w)
31+
# print(term, w, tmp_diff, tmp_diff < diff)
32+
if tmp_diff < diff:
33+
diff = tmp_diff
34+
word = w
35+
return word
36+
37+
words=['cherry', 'peach', 'pineapple', 'melon', 'strawberry', 'raspberry', 'apple', 'coconut', 'banana']
38+
test_dict=Dictionary(words)
39+
print(test_dict.find_most_similar('strawbery'),'strawberry')
40+
print(test_dict.find_most_similar('berry'),'cherry')
41+
print(test_dict.find_most_similar('aple'),'apple')

3-fibbonacci.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def my_matrix_multiply(a, b):
2+
m0000 = a[0][0] * b[0][0]
3+
# m0101 = m0110 = m1001 = m1010 = a[0][1] * b[1][0]
4+
m0110 = m1001 = a[0][1] * b[1][0]
5+
# m0001 = m0010 = a[0][0] * b[0][1]
6+
m0001 = a[0][0] * b[0][1]
7+
# m0111 = m1011 = a[0][1] * b[1][1]
8+
m0111 = a[0][1] * b[1][1]
9+
m1111 = a[1][1]*b[1][1]
10+
return [
11+
[m0000 + m0110, m0001 + m0111],
12+
# [a[1][0]*b[0][0] + a[1][1]*b[1][0], a[1][0]*b[0][1] + a[1][1]*b[1][1]]
13+
[m0001 + m0111, m1001 + m1111]
14+
]
15+
16+
def fib(n):
17+
q = 1
18+
if n == 0:
19+
return 0
20+
if n < 0:
21+
n = -1*n
22+
if n % 2 == 0:
23+
q = -1
24+
matrix = [[1, 1], [1, 0]]
25+
result = [[1, 0], [0, 1]]
26+
for c in '{:b}'.format(n-1)[::-1]:
27+
if c == '1':
28+
result = my_matrix_multiply(result, matrix)
29+
matrix = my_matrix_multiply(matrix, matrix)
30+
return q * result[0][0]
31+
32+
33+
print(fib(0))
34+
print(fib(1))
35+
print(fib(2))
36+
print(fib(3))
37+
print(fib(17))
38+
# print(fib(1000000))
39+
print(fib(10))
40+
print(fib(-10))

0 commit comments

Comments
 (0)