diff --git a/12.23.txt b/12.23.txt new file mode 100644 index 0000000..9a4aa07 --- /dev/null +++ b/12.23.txt @@ -0,0 +1,5 @@ +Reeves K. 0661234567 +Cage N. 0953451234 +Depp J. 0507654321 +DiCaprio L. 0664561234 +Freeman M. 0509876123 \ No newline at end of file diff --git a/12.24.txt b/12.24.txt new file mode 100644 index 0000000..2ec42c7 --- /dev/null +++ b/12.24.txt @@ -0,0 +1,10 @@ +3 red iron +5 green iron +5 blue wood +7 red cardboard +6 blue iron +2 green cardboard +4 red wood +8 yellow wood +2 blue cardboard +6 yellow iron \ No newline at end of file diff --git a/12.47.py b/12.47.py new file mode 100644 index 0000000..fc879fb --- /dev/null +++ b/12.47.py @@ -0,0 +1,40 @@ +def check_indent(filename): + with open(filename) as f: + used = [(0, '')] + lines = f.readlines() + + for line in lines: + stripped_line = line.lstrip() + indent = len(line) - len(stripped_line) + + if indent > used[-1][0] and used[-1][1] == '': + print(line) + return False + if indent <= used[-1][0] and used[-1][1] == ':': + print(line) + return False + if indent < used[-1][0]: + while indent < used[-1][0]: + used.pop() + if indent != used[-1][0]: + print(line) + return False + if ':' == line.rstrip()[-1]: + used.append((indent, ':')) + else: + used.append((indent, '')) + elif ':' == line.rstrip()[-1]: + used.append((indent, ':')) + else: + used.append((indent, '')) + print(lines.index(line)+1, indent, used) + + return True + +if __name__ == '__main__': + filename = 'exercises/12.47.txt' + print(check_indent(filename)) + + + + diff --git a/12.47.txt b/12.47.txt new file mode 100644 index 0000000..b0dfa49 --- /dev/null +++ b/12.47.txt @@ -0,0 +1,32 @@ +def check_indent(filename): + with open(filename) as f: + used = [(0, '')] + lines = f.readlines() + for line in lines: + stripped_line = line.lstrip() + indent = len(line) - len(stripped_line) + if indent > used[-1][0] and used[-1][1] == '': + print(line) + return False + if indent <= used[-1][0] and used[-1][1] == ':': + print(line) + return False + if indent < used[-1][0]: + while indent < used[-1][0]: + used.pop() + if indent != used[-1][0]: + print(line) + return False + if ':' == line.rstrip()[-1]: + used.append((indent, ':')) + else: + used.append((indent, '')) + elif ':' == line.rstrip()[-1]: + used.append((indent, ':')) + else: + used.append((indent, '')) + print(lines.index(line)+1, indent, used) + return True +if __name__ == '__main__': + filename = 'exercises/12.47.txt' + print(check_indent(filename)) \ No newline at end of file diff --git a/classwork4.py b/classwork4.py new file mode 100644 index 0000000..f4453e9 --- /dev/null +++ b/classwork4.py @@ -0,0 +1,63 @@ +# Завдання 12.23 +from collections import defaultdict + +def phone_number(filename, name): + f = open(filename) + names = f.readlines() + name = name.split() + name = f'{name[0].title()} {name[1].title()}.' + for row in names: + if f'{row.split()[0]} {row.split()[1]}' == name: + return name, row.split()[2] + f.close() + + return 'співробітника не знайдено' + +filename = 'exercises/12.23.txt' +name = input("Введіть ім'я: ") +print(f'Телефон співробітника {phone_number(filename, name)[0]}: {phone_number(filename, name)[1]}') + +# Завдання 12.24 + +def cubes_by_colours(filename): + f = open(filename) + rows = f.readlines() + colours_dict = defaultdict(int) + areas_dict = defaultdict(int) + cubes_dict = defaultdict(tuple) + + for cube in rows: + colours_dict[cube.split()[1]] += 1 + for cube in rows: + areas_dict[cube.split()[1]] += int(cube.split()[0])**3 + for colour in areas_dict.keys(): + cubes_dict[colour] = (f'Кількість кубиків: {colours_dict[colour]}', + f"Сумарний об'єм: {areas_dict[colour]}") + f.close() + + return cubes_dict + +def cubes_by_materials(filename): + f = open(filename) + rows = f.readlines() + wood_cubes = 0 + iron_cubes = 0 + + for cube in rows: + if cube.split()[2] == 'wood' and int(cube.split()[0]) == 3: + wood_cubes += 1 + if cube.split()[2] == 'iron' and int(cube.split()[0]) > 5: + iron_cubes += 1 + + return wood_cubes, iron_cubes + + +filename = 'exercises/12.24.txt' +for cube_colour, info in cubes_by_colours(filename).items(): + print(f"{cube_colour}: {info}") + +cubes = cubes_by_materials(filename) +print(f"\nКількість дерев'яних кубиків із ребром 3: {cubes[0]}") +print(f"Кількість металевих кубиків із ребром більшим за 5: {cubes[1]}") + + \ No newline at end of file