Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 2 #86

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
77 changes: 77 additions & 0 deletions homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Picture:
def __init__(self, colums, lines, matrix):
self.colums = colums
self.lines = lines
self.matrix = matrix

def check_matrix(self):
for i in range(0, self.colums):
for y in range(0, self.lines):
if self.matrix[i][y] < 0 or self.matrix[i][y] > 255:
return False
return True

def check_element(matrix, x, y, colums, lines, sum, flag):
if matrix[x][y] == 0:
return [[0], [0], 0]

random_point = matrix[x][y]
sum[0] = sum[0] + matrix[x][y]
flag[0] = flag[0] + 1
matrix[x][y] = 0

if x < (colums - 1):
check_element(matrix, x + 1, y, colums, lines, sum, flag)

if x < (colums - 1) and y < (lines - 1):
check_element(matrix, x + 1, y + 1, colums, lines, sum, flag)

if y < (lines - 1):
check_element(matrix, x, y + 1, colums, lines, sum, flag)[0]


return [sum, flag, random_point]

def avg_brightness(picture):
working_matrix = picture.matrix
avgs = []
avgs_index = 0


for i in range(0, picture.colums):
for y in range(0, picture.lines):
sum = [0]
flag = [0]
result = [0, 0]
result = check_element(working_matrix, i, y, picture.colums, picture.lines, sum, flag)
sum = result[0]
flag = result[1]

if sum[0] > 0:
print(f'Random point in area {avgs_index + 1} : {result[2]} \n')
avgs.insert(avgs_index, sum[0] / flag[0])
avgs_index = avgs_index + 1

avgs.sort(reverse = True)

return avgs

matrix = [[2, 2, 0, 5, 3],
[0, 0, 0, 3, 5],
[0, 0, 0, 0, 0],
[0, 1, 2, 3, 0],
[0, 0, 0, 0, 0]]

p1 = Picture(5, 5, matrix)

if p1.check_matrix() is False:
print("Invalid matrix numbers!")
else:
average_p1 = avg_brightness(p1)

print("\nAverage brightness of areas:\n")

print(average_p1)

for i in range (0, len(average_p1)):
print(f'\nArea {i + 1} : {average_p1[i]}\n')
19 changes: 19 additions & 0 deletions homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def num_ways(N):
first_num = 0
second_num = 1
ways = 0

for i in range(1, (N + 1)):
ways = first_num + second_num
first_num = second_num
second_num = ways

return ways

stairs1 = 5
ways_for_stairs1 = num_ways(stairs1)
print(f'{stairs1} steps could be walked by {ways_for_stairs1} ways.\n')

stairs2 = 15
ways_for_stairs2 = num_ways(stairs2)
print(f'{stairs2} steps could be walked by {ways_for_stairs2} ways.\n')
21 changes: 21 additions & 0 deletions homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

def replace(current_list, find, replacement):
for i in range(0, len(current_list)):
if isinstance(current_list[i], list):
replace(current_list[i], find, replacement)

if type(current_list[i]) is tuple:
new_list = list(current_list[i])
replace(new_list, find, replacement)
current_list[i] = tuple(new_list)

if current_list[i] == find:
current_list[i] = replacement



my_list = [ 'a', 1, [ ['a', 'b'], 1], ([1, 3, 'a'], 'b')]

replace(my_list, 'a', 'c')

print(my_list)