Skip to content

Commit 63804fd

Browse files
committed
Problems/Theory
1 parent 66a757d commit 63804fd

File tree

141 files changed

+3393
-0
lines changed

Some content is hidden

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

141 files changed

+3393
-0
lines changed

Algorithms/Permutations algorithm.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def permute(idx, elements):
2+
if idx >= len(elements):
3+
print(elements)
4+
return
5+
permute(idx + 1, elements)
6+
7+
for i in range(idx + 1, len(elements)):
8+
elements[idx], elements[i] = elements[i], elements[idx]
9+
permute(idx + 1, elements)
10+
elements[idx], elements[i] = elements[i], elements[idx]
11+
12+
13+
14+
elements = ['A', 'B', 'C']
15+
permute(0, elements)

Algorithms/Polinomical algorithm.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Polinomical:
2+
def __init__(self, arg, data: list):
3+
self.arg = arg
4+
self.data = data
5+
6+
def eval_polynomial(self):
7+
return sum((current_el*self.arg**idx for idx,current_el in enumerate(self.data)))
8+
9+
10+
def multiply_by_one_term(self):
11+
return [0]*self.arg + [self.arg*idx for idx in self.data]
12+
13+
obj = Polinomical(3, [1,2,3])
14+
print(obj.eval_polynomial())
15+
print(obj.multiply_by_one_term())

Algorithms/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Fibonacci:
2+
def __init__(self, n):
3+
self.n = n
4+
self.sequence = [1, 1]
5+
self.iterations = len(self.sequence)
6+
7+
def __iter__(self):
8+
return self
9+
10+
def __next__(self):
11+
if self.iterations == self.n:
12+
print(self.sequence)
13+
raise StopIteration
14+
15+
a = self.sequence[-2]
16+
b = self.sequence[-1]
17+
18+
a, b = b, a+b
19+
self.sequence.append(b)
20+
21+
self.iterations += 1
22+
23+
24+
obj = list(Fibonacci(10))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Polinomical:
2+
def __init__(self, arg, data: list):
3+
self.arg = arg
4+
self.data = data
5+
6+
def eval_polynomial(self):
7+
return sum((current_el*self.arg**idx for idx,current_el in enumerate(self.data)))
8+
9+
10+
def multiply_by_one_term(self):
11+
return [0]*self.arg + [self.arg*idx for idx in self.data]
12+
13+
obj = Polinomical(3, [1,2,3])
14+
print(obj.eval_polynomial())
15+
print(obj.multiply_by_one_term())

DecoratorsIterators/__add__method.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class MyClass:
2+
def __init__(self, vals):
3+
if not isinstance(vals, list):
4+
return
5+
self.vals = vals
6+
7+
def __add__(self, other):
8+
return self.vals + other
9+
10+
obj_1 = [1,2,3]
11+
obj_2 = [4,5,6]
12+
print(obj_1.__add__(obj_2))
13+
print(obj_1 + [1,7,5])

DecoratorsIterators/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MyClass:
2+
def __init__(self, val):
3+
if not isinstance(val, list):
4+
return
5+
self.val = val
6+
7+
@property
8+
def val(self):
9+
return self.__val
10+
11+
@val.setter
12+
def val(self, value: list):
13+
14+
self.__val = [x for x in value if type(x) == str]
15+
16+
def __str__(self):
17+
res = ''
18+
for el in self.val:
19+
res += el[0]
20+
return res
21+
22+
obj = MyClass([1, 2, "Hello", 'i', ' ', "i", "'", 4, 'm', 3, 4, ' ', 'D', 'i', 'm', 4, 'i', 6, 7, 't', 'h', 'e', 'r'])
23+
print(obj)

DecoratorsIterators/getitem_attr.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class MyClass:
2+
def __init__(self, one: list, two:list):
3+
self.one = one
4+
self.two = two
5+
6+
7+
def get_two(self):
8+
return self.two
9+
10+
def __getitem__(self, item):
11+
if item < 0 or item > len(self.one):
12+
return 0
13+
if item == 'one':
14+
return self.one[item]
15+
return self.two[item]
16+
17+
18+
19+
20+
def __setitem__(self, key, value):
21+
self.one[key] = value
22+
return self.one[key]
23+
24+
obj_one = MyClass([1, 2, 3], [3,4,5])
25+
x = obj_one.one[4] + obj_one.two[1]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Odd:
2+
def __init__(self, n):
3+
self.n = n
4+
self.iteration = 0
5+
6+
def __iter__(self):
7+
return self
8+
9+
def __next__(self):
10+
while self.iteration <= self.n:
11+
current_number = self.iteration
12+
self.iteration += 1
13+
if not current_number % 2 == 0:
14+
return current_number
15+
else:
16+
raise StopIteration
17+
18+
19+
obj = Odd(5)
20+
print(list(obj))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class MyClass:
2+
def __init__(self, number):
3+
self.number = number
4+
5+
def __add__(self, other):
6+
return self.number + other
7+
8+
def __sub__(self, other):
9+
return self.number - other
10+
11+
def __rsub__(self, other):
12+
return abs(self.number - other)
13+
14+
def __mul__(self, other):
15+
return self.number * other
16+
17+
def __truediv__(self, other):
18+
return self.number / other
19+
20+
obj = MyClass(5)
21+
print(obj + 5)
22+
print(obj - 4)
23+
print(8 - obj)
24+
print(obj * 5)
25+
print(obj / 2)
26+

DecoratorsIterators/show_method.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Person:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
def show(self):
6+
return self.name
7+
8+
9+
class Student(Person):
10+
def __init__(self, name, faculty_number):
11+
super().__init__(name)
12+
self.faculty_number = faculty_number
13+
14+
def show(self):
15+
return f"Student has name: {self.name} and faculty number: {self.faculty_number}"
16+
17+
18+
class Teacher(Person):
19+
def __init__(self, name, subject):
20+
super().__init__(name)
21+
self.subject = subject
22+
23+
def show(self):
24+
return f"Teacher {self.name} has subject {self.subject}"
25+
26+
27+
p = Person('Ivan')
28+
s = Student('Kaloyan', 'f95532')
29+
t = Teacher('Asen', 'mathematics')
30+
31+
print(p.show())
32+
print(s.show())
33+
print(t.show())
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class MyClass:
2+
def __init__(self, integer, string_type, is_float):
3+
self.integer = integer
4+
self.string_type = string_type
5+
self.is_float = is_float
6+
7+
def __int__(self):
8+
return f'Type of {self.integer} is of class int.'
9+
10+
def __str__(self):
11+
return f"Type of '{self.string_type}' is of class str."
12+
13+
def __float__(self):
14+
return f"Type of {self.is_float} is of class float."
15+
16+
obj = MyClass(1, 'hello', 5.1)
17+
print(obj.__int__())
18+
print(obj.__str__())
19+
print(obj.__float__())
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class MyClass:
2+
def __init__(self, values):
3+
if not isinstance(values, list):
4+
return
5+
self.values = values
6+
7+
def __len__(self, other):
8+
diff = abs(len(self.values) - len(other.values))
9+
if len(self.values) < len(other.values):
10+
for _ in range(diff):
11+
self.values.append(0)
12+
return self.values
13+
for _ in range(diff):
14+
other.values.append(0)
15+
return other.values
16+
17+
def __eq__(self, other):
18+
return self.values[0] == other.values[0]
19+
20+
def __ne__(self, other):
21+
return bool(self.values[1] != other.values[1])
22+
23+
def __lt__(self, other):
24+
return bool(self.values[2] < other.values[2])
25+
26+
def __gt__(self, other):
27+
return bool(self.values[3] > other.values[3])
28+
29+
def __ge__(self, other):
30+
return bool(self.values[4] >= other.values[4])
31+
32+
def __le__(self, other):
33+
return bool(self.values[5] <= other.values[5])
34+
35+
36+
37+
obj_1 = MyClass([1,2,3,4])
38+
obj_2 = MyClass([1,6,7,8, 3, 7])
39+
obj_1.__len__(obj_2)
40+
print(obj_1 == obj_2)
41+
print(obj_1 != obj_2)
42+
print(obj_1 < obj_2)
43+
print(obj_1 > obj_2)
44+
print(obj_1 >= obj_2)
45+
print(obj_1 <= obj_2)

DequesStacksMatrices/problem 4.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import math
2+
3+
4+
def valid_command(command_input):
5+
if command_input == 'up' or command_input == 'down' or command_input == 'left' or command_input == 'right':
6+
return True
7+
return False
8+
9+
def movement(row, col, current_input):
10+
11+
if current_input == "up":
12+
if 0 <= row -1 < size:
13+
row = row-1
14+
else:
15+
if row - 1 == -1:
16+
row = size - 1
17+
elif current_input == 'down':
18+
if 0 <= row + 1 < size:
19+
row += 1
20+
else:
21+
row = 0
22+
elif current_input == 'left':
23+
if 0 <= col - 1 < size:
24+
col -= 1
25+
else:
26+
col = size - 1
27+
elif current_input == 'right':
28+
if 0 <= col + 1 < size:
29+
col += 1
30+
else:
31+
col = 0
32+
33+
return row,col
34+
35+
36+
size = int(input())
37+
38+
matrix = [[x for x in input().split()] for _ in range(size)]
39+
40+
player_position = []
41+
walls_positions = []
42+
for row in range(size):
43+
for col in range(size):
44+
if matrix[row][col] == 'P':
45+
player_position.append((row,col))
46+
elif matrix[row][col].isnumeric():
47+
matrix[row][col] = int(matrix[row][col])
48+
49+
elif matrix[row][col] == 'X':
50+
walls_positions.append((row,col))
51+
52+
total_collected_coins = 0
53+
win = False
54+
current_row = player_position[0][0]
55+
current_col = player_position[0][1]
56+
57+
path_taken = [(current_row,current_col)]
58+
59+
60+
while True:
61+
if total_collected_coins >= 100:
62+
win = True
63+
break
64+
command = input()
65+
if valid_command(command):
66+
next_row, next_col = movement(current_row, current_col, command)
67+
path_taken.append((next_row, next_col))
68+
if (next_row,next_col) in walls_positions:
69+
total_collected_coins = math.floor(total_collected_coins/2)
70+
break
71+
else:
72+
if path_taken.count((next_row,next_col)) == 1:
73+
if type(matrix[next_row][next_col]) == int:
74+
current_coins = matrix[next_row][next_col]
75+
total_collected_coins += current_coins
76+
matrix[current_row][current_col] = matrix[next_row][next_col]
77+
matrix[next_row][next_col] = 'P'
78+
current_row,current_col = next_row,next_col
79+
else:
80+
if matrix[next_row][next_col] != 'X':
81+
matrix[current_row][current_col] = matrix[next_row][next_col]
82+
matrix[next_row][next_col] = 'P'
83+
current_row, current_col = next_row, next_col
84+
else:
85+
break
86+
87+
else:
88+
pass
89+
90+
if win:
91+
print(f"You won! You've collected {math.floor(total_collected_coins)} coins.")
92+
else:
93+
print(f"Game over! You've collected {math.floor(total_collected_coins)} coins.")
94+
95+
96+
print(f"Your path:")
97+
for item in path_taken:
98+
print(list(item))
99+
100+

0 commit comments

Comments
 (0)