Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions homeworks/27_Yassen_Efremov/avg_brightness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# =============================================================================================== #
# Classes, Functions, Global variables #


def avg_recursive(matrix, cell_row, cell_col, avg_region_brightness):
avg_region_brightness.append(matrix[cell_row][cell_col]) # add the brightness of the current cell
matrix[cell_row][cell_col] = 0 # Mark the cell as visited
# Start calling the function recursively on all the neighboor cells
for i in range(-1, 2):
for j in range(-1, 2):
# Check if the next indexes are out of range
if (0 <= cell_row + i < len(matrix)) and (0 <= cell_col + j < len(matrix[0])):
# Check if the next cell has brightness 0 or is the current cell
if (matrix[cell_row + i][cell_col + j] != 0) and ([i, j] != [0, 0]):
avg_recursive(matrix, cell_row + i, cell_col + j, avg_region_brightness)


def avg_brightness(matrix):
matrix_copy = [list(matrix_inner) for matrix_inner in matrix] # copy so that we don't modify the passed list
avg_brightnesses = []

# Look for cells with brightness above 0 and start recursively going through them
for i in range(0, len(matrix_copy)):
for j in range(0, len(matrix_copy[0])):
if matrix_copy[i][j] > 0:
avg_region_brightness = []
avg_recursive(matrix_copy, i, j, avg_region_brightness)
avg_brightnesses.append(avg_region_brightness)

avg_brightnesses.sort(key = lambda row: sum(row) / len(row), reverse=True)
for row in avg_brightnesses:
print(sum(row) / len(row))


# =============================================================================================== #


def main():

# Example
matrix = (
(170, 0, 0, 255, 221, 0),
( 68, 0, 17, 0, 0, 68),
(221, 0, 238, 136, 0, 255),
( 0, 0, 85, 0, 136, 238),
(238, 17, 0, 68, 0, 255),
( 85, 170, 0, 221, 17, 0)
)

avg_brightness(matrix)


# =============================================================================================== #


if __name__ == "__main__":
main()
46 changes: 46 additions & 0 deletions homeworks/27_Yassen_Efremov/num_ways.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import time


# =============================================================================================== #
# Classes, Functions, Global variables #


def num_ways(N, found):
# Optimized with memoization

if N in found:
return found[N]

if N <= 1: return 1

num = num_ways(N - 1, found) + num_ways(N - 2, found)
found[N] = num

return num



# =============================================================================================== #


def main():

start_time = time.time()
dict = {}

# Examples
print("Ways to climb a ladder with {} steps: {}".format(2, num_ways(2, dict)))
print("Ways to climb a ladder with {} steps: {}".format(3, num_ways(3, dict)))
print("Ways to climb a ladder with {} steps: {}".format(5, num_ways(5, dict)))
print("Ways to climb a ladder with {} steps: {}".format(7, num_ways(7, dict)))

# print("Ways to climb a ladder with {} steps: {}".format(100, num_ways(200, dict)))

print("\n### Completed after {:.6f} seconds ###".format(time.time() - start_time))


# =============================================================================================== #


if __name__ == "__main__":
main()
34 changes: 34 additions & 0 deletions homeworks/27_Yassen_Efremov/replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# =============================================================================================== #
# Classes, Functions, Global variables #


def replace(a_list, to_find, replace_with):
list_copy = list(a_list) # copy so that we don't modify the passed list

for i, value in enumerate(list_copy):
if value == to_find:
# Found a value to replace
list_copy[i] = replace_with
elif type(value) in [list, tuple]:
# Found another collection => iterate through it recursively
list_copy[i] = replace(list_copy[i], to_find, replace_with)

return list_copy


# =============================================================================================== #


def main():

# Examples
list = [ 'a', 1, [['a', 'b'], 1], ([1, 3, 'a'], 'b')]
res = replace(list, 'a', 'c')
print(res) # => [ 'c', 1, [ ['c', 'b'], 1], ([1, 3, 'c'], 'b')]


# =============================================================================================== #


if __name__ == "__main__":
main()