#! /usr/bin/evn python """ Programm zum Berechnen den Aufwand für das Selbststudium pro Woche in einem Semester. Das Semester hat 15 Woche. Eine Doppelstunde (DS) entspricht 90 Minuten. Ein ECTS entspricht einen Aufwand von 30 Stunden. Eine Veranstaltung (Vorlesung, Übung, Tutorium) ist 2 DS. Die Vorlesungen in einem Semester werden in einer Text-Datei erfasst. Ein Template ist wie folgt: ```txt #Name; ECTS; Veranstaltung pro Woche Programmierung 2; 5; 2 Mathematik 2; 5; 3 ``` Usage: $python effort_semester.py Vorlesungen.txt Vorlesung ECTS V-Ü-T/Woche Selbstudiumszeit pro Woche Programmierung 2 5 2 <> Mathematik 2 5 3 <> -------------------------------------------------------------------------------- <> """ import sys from typing import Final, Any SEMESTER_LENGTH: Final[int] = 15 # 15 Woche pro Semester ECTS_EFFORT = 30 # 1 ECTS ~ 30 h DS = 1.5 # DS = Doppelstunde = 2 Unterrichtstunden def parse_line(line: str) -> tuple[str, float, float]: line = line.strip() line = [e.strip() for e in line.split(";")] return line[0], float(line[1]), float(line[2]) def collect_lectures_from_file(lecture_filename:str) -> list[tuple[str, float, float]]: # Your code here lectures = [] with open(lecture_filename) as file: while line := file.readline(): line = line.strip() if len(line) > 0 and line[0] != '#': line = parse_line(line) lectures.append( line ) return lectures def lecture_effort(lecture: tuple[str, float, float]) -> float: sum_effort = lecture[1] * ECTS_EFFORT lecture_effort = SEMESTER_LENGTH * (lecture[2] * DS) return (sum_effort - lecture_effort) / SEMESTER_LENGTH def compute_semester_effort(lectures: list[tuple[str, float, float]]) -> list[tuple[str, float, float, float]]: effort = [ (e[0], e[1], e[2], lecture_effort(e)) for e in lectures ] return effort def print_effort(effort: list[tuple[str, float, float, float]]) -> None: header = ("Vorlesung", "ECTS", "V;U;T/Woche", "Aufwand/Woche") max_char = len(header) sep_width = 0 sum_effort = 0 for l in effort: if (c := len(l[0])) > max_char: max_char = c sum_effort += l[-1] lecture_col_width = sep_width + max_char ects_width = len(header[1]) veranstaltung_col_width = len(header[2]) effort_col_width = len(header[3]) def format_line(line: list[tuple[Any, Any, Any, Any]] ): lecture = f"{line[0]}".rjust(lecture_col_width) ects = f"{line[1]}".center(ects_width) v = f"{line[2]}".center(veranstaltung_col_width) e = f"{line[3]}".center(effort_col_width) return f"{lecture} {ects} {v} {e}" print(format_line(header)) for l in effort: print(format_line(l)) print(f"Summe: {sum_effort}") if __name__ == "__main__": lecture_filename = sys.argv[1] lectures = collect_lectures_from_file(lecture_filename) effort = compute_semester_effort(lectures) print_effort(effort)