Skip to content

Commit b4980d6

Browse files
author
Artsiom Tkachou
committed
Init commit 2
1 parent 966bf96 commit b4980d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+555391
-0
lines changed

4statements.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import itertools
2+
3+
4+
def f1(x, y, z):
5+
return (x, z + 2, z)
6+
7+
8+
def f2(x, y, z):
9+
return (y + z * 2, y, z)
10+
11+
12+
def f3(x, y, z):
13+
return (y - 3, y, z)
14+
15+
16+
def f4(x, y, z):
17+
return (x, y, x + y)
18+
19+
20+
arr_fun = [f1, f2, f3, f4]
21+
22+
23+
def get_max():
24+
max = float("-infinity")
25+
result = []
26+
for arr in itertools.permutations(arr_fun):
27+
xyz = (1, 2, 3)
28+
for fun in arr:
29+
xyz = fun(*xyz)
30+
if xyz[0] > max:
31+
max = xyz[0]
32+
result = arr
33+
return max, result
34+
35+
36+
m, vector = get_max()
37+
print m
38+
print vector

aquareroot.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def squareroot(n):
2+
if n < 0:
3+
raise ValueError
4+
if n == 1:
5+
return 1
6+
result = 1
7+
for i in range(n / 2 + 1):
8+
if i ** 2 == n:
9+
return i
10+
elif i ** 2 > n:
11+
return i - 1
12+
return result
13+
14+
15+
def squareroot2(n):
16+
pass
17+
18+
19+
print(squareroot(1))

array_pair_sum.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def get_pairs_sum(array, sum):
2+
array.sort()
3+
result = []
4+
s = 0
5+
e = len(array) - 1
6+
while s < e:
7+
if array[s] + array[e] == sum:
8+
result.append((array[s], array[e]))
9+
s += 1
10+
elif array[s] + array[e] < sum:
11+
s += 1
12+
else:
13+
e -= 1
14+
return result
15+
16+
if __name__ == "__main__":
17+
a = [2, 5, 7, 9, 4, 10, 13, 1]
18+
print(get_pairs_sum(a, 14))

bash_cmd.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bashCommand = "ls"
2+
import subprocess
3+
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
4+
output = process.communicate()[0].split("\n")[:-1]
5+
print(output[0])
6+
for file in output:
7+
print(file)

bin_search.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
a = [1, 34, 76, 45, 97, 56, 79, 356, 579, 454, 578, 467, 786]
2+
a = sorted(a)
3+
4+
5+
def bin_search(iterable, target):
6+
start = 0
7+
end = len(iterable)
8+
9+
while end - start != 1:
10+
guess = (end + start) / 2
11+
if iterable[guess] == target:
12+
return guess
13+
elif iterable[guess] < target:
14+
start = guess
15+
else:
16+
end = guess
17+
18+
return start, end
19+
20+
21+
class BinSearchArray(object):
22+
def __init__(self):
23+
self.container = []
24+
25+
def add(self, item):
26+
index = self.bin_search_position(item)
27+
self.container.insert(index, item)
28+
29+
def bin_search_position(self, target):
30+
start = 0
31+
end = len(self.container)
32+
if start != end:
33+
while end - start != 1:
34+
guess = (end + start) / 2
35+
if self.container[guess] == target:
36+
return guess
37+
elif self.container[guess] < target:
38+
start = guess + 1
39+
else:
40+
end = guess - 1
41+
return start
42+
43+
44+
# print a
45+
# print bin_search(a, 0)
46+
47+
search = BinSearchArray()
48+
search.add(3)
49+
search.add(1)
50+
search.add(2)
51+
52+
print search.container

bin_search_tree_check.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Node(object):
2+
def __init__(self, val):
3+
self.left, self.right, self.val = None, None, val
4+
5+
6+
MINUS_INF = float("-infinity")
7+
PLUS_INF = float("infinity")
8+
9+
10+
def check_tree(tree, min_val=MINUS_INF, max_val=PLUS_INF):
11+
if tree is None:
12+
return True
13+
14+
if not min_val <= tree.val <= max_val:
15+
return False
16+
17+
return check_tree(tree.left, min_val, tree.val) and \
18+
check_tree(tree.right, tree.val, max_val)
19+
20+

camel_case_matcher.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
l = ["Hello", "World" "HelloMars", "HelloWorld", "HelloWorldMars", "HiHo"]
2+
3+
patterns = ["H", "He", "HeW", "HeWo", "HeWor", "HeWorM"]
4+
# patterns = ["H", "HW", "Ho", "HeWorM", "He"]
5+
6+
d = {
7+
"HelloMars": ["Hello", "Mars"],
8+
"HelloWorld": ["Hello", "World"],
9+
"HelloWorldMars": ["Hello", "World", "Mars"],
10+
"HiHo": ["Hi", "Ho"]
11+
}
12+
13+
14+
def split_camel_case(word):
15+
result = []
16+
start = 0
17+
for i in range(1, len(word)):
18+
if word[i].isupper():
19+
result.append(word[start:i])
20+
start = i
21+
result.append(word[start:])
22+
return result
23+
24+
25+
# for word in l:
26+
# print split_camel_case(word)
27+
28+
print '-----------'
29+
30+
pat = "HeWorM"
31+
32+
33+
# print split_camel_case(pat)
34+
35+
36+
def generate_splitetd_dict(l):
37+
result = {}
38+
for word in l:
39+
result[word] = split_camel_case(word)
40+
return result
41+
42+
43+
# print generate_splitetd_dict(l)
44+
45+
46+
def get_classes(p, l):
47+
pl = split_camel_case(p)
48+
spl_d = generate_splitetd_dict(l)
49+
for i in range(len(pl)):
50+
for k, v in spl_d.items():
51+
approved = False
52+
for w_i in range(i, len(v)):
53+
if v[w_i].startswith(pl[i]):
54+
approved = True
55+
if not approved:
56+
del spl_d[k]
57+
return spl_d.keys()
58+
59+
60+
for pattern in patterns:
61+
print pattern, get_classes(pattern, l)
62+
63+
64+
def word_mut(word):
65+
result = []
66+
for i in range(len(word)):
67+
result.append(word[:i + 1])
68+
return result
69+
70+
71+
def get_all_patterns(clazz):
72+
words = split_camel_case(clazz)
73+
result = []
74+
for word in words:
75+
result.append(word_mut(word))
76+
return result
77+
78+
79+
print "---------"
80+
print get_all_patterns("HelloWorldMars")

check_balanced_parentheses.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def check(expr):
2+
if len(expr) % 2 != 0:
3+
return False
4+
matcher = {("(", ")"), ("{", "}"), ("[", "]")}
5+
opened = set([x[0] for x in matcher])
6+
stack = []
7+
for char in expr:
8+
if char in opened:
9+
stack.append(char)
10+
else:
11+
if len(stack) == 0:
12+
return False
13+
last = stack.pop()
14+
if (last, char) not in matcher:
15+
return False
16+
return len(stack) == 0
17+
18+
expr = "{()}"
19+
expr2 = "([{{{}}}])"
20+
21+
print check(expr)
22+
print check(expr2)

chess_horse.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import collections
2+
3+
4+
def get_acceptable_steps(i, j):
5+
template = {(i + 1, j - 2), (i + 1, j + 2), (i - 1, j - 2),
6+
(i - 1, j + 2), (i + 2, j - 1), (i + 2, j + 1),
7+
(i - 2, j + 1), (i - 2, j - 1)}
8+
return {x for x in template if 0 <= x[0] <= 7 and 0 <= x[1] <= 7}
9+
10+
11+
def generate_horse_graph():
12+
graph = collections.defaultdict(set)
13+
for i in range(8):
14+
for j in range(8):
15+
graph[(i, j)] = graph[(i, j)] | get_acceptable_steps(i, j)
16+
17+
return dict(graph)
18+
19+
20+
def path(graph, start, goal):
21+
paths = collections.deque([[start]])
22+
extended = set()
23+
while len(paths) != 0:
24+
current_path = paths.popleft()
25+
current_point = current_path[-1]
26+
if current_point == goal:
27+
return current_path
28+
elif current_point in extended:
29+
continue
30+
extended.add(current_point)
31+
transforms = graph[current_point]
32+
for word in transforms:
33+
if word not in current_path:
34+
paths.append(current_path[:] + [word])
35+
return []
36+
37+
38+
def bfs_paths(graph, start, goal):
39+
queue = [(start, [start])]
40+
while queue:
41+
(vertex, path) = queue.pop(0)
42+
for next in graph[vertex] - set(path):
43+
if next == goal:
44+
yield path + [next]
45+
else:
46+
queue.append((next, path + [next]))
47+
48+
49+
graph = generate_horse_graph()
50+
# print graph
51+
sp = (0, 0)
52+
ep = (4, 3)
53+
54+
# print path(graph, sp, ep)
55+
gen = bfs_paths(graph, sp, ep)
56+
print gen.next()

cli_scelet.py

Whitespace-only changes.

cli_template.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python
2+
3+
# This script is a template for writing CLI commands in python.
4+
5+
# Note using "#!/usr/bin/env python" above will
6+
# cause the system to register the name (in ps etc.)
7+
# as "python" rather than the command name
8+
# (as "python" is passed to exec by env).
9+
10+
import sys
11+
12+
13+
# The following exits cleanly on Ctrl-C,
14+
# while treating other exceptions as before.
15+
def cli_exception(type, value, tb):
16+
if not issubclass(type, KeyboardInterrupt):
17+
sys.__excepthook__(type, value, tb)
18+
19+
20+
if sys.stdin.isatty():
21+
sys.excepthook = cli_exception
22+
23+
# The following demonstrates common option argument processing
24+
import os
25+
26+
27+
def Usage():
28+
print "Usage: %s [OPTION] [PATHS]" % os.path.split(sys.argv[0])[1]
29+
print " --name=value whatever"
30+
print " --help display help"
31+
32+
33+
import getopt
34+
35+
try:
36+
lOpts, lArgs = getopt.getopt(sys.argv[1:], "", ["help", "name="])
37+
38+
if len(lArgs) == 0:
39+
lArgs = [os.getcwd()]
40+
41+
if ("--help", "") in lOpts:
42+
Usage()
43+
sys.exit(None)
44+
45+
name = None
46+
for opt in lOpts:
47+
if opt[0] == "--name":
48+
name = opt[1]
49+
print "name =", name
50+
51+
except getopt.error, msg:
52+
print msg
53+
print
54+
Usage()
55+
sys.exit(2)
56+
57+
print "processing", lArgs
58+
print "Ctrl-C to exit"
59+
sys.stdin.readline()

codementor_io/__init__.py

Whitespace-only changes.

codementor_io/q3.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
A0 = dict(zip(('a', 'b', 'c', 'd', 'e'), (1, 2, 3, 4, 5)))
2+
print A0
3+
A1 = range(10)
4+
print A1
5+
A2 = sorted([i for i in A1 if i in A0])
6+
print A2
7+
A3 = sorted([A0[s] for s in A0])
8+
print A3
9+
A4 = [i for i in A1 if i in A3]
10+
print A4
11+
A5 = {i: i * i for i in A1}
12+
print A5
13+
A6 = [[i, i * i] for i in A1]
14+
print A6

0 commit comments

Comments
 (0)