From 75579e1a355839aacf20dce3fd0a486279aef4c2 Mon Sep 17 00:00:00 2001 From: Hong-Phuc Bui <hong-phuc.bui@htwsaar.de> Date: Tue, 27 May 2025 15:38:37 +0200 Subject: [PATCH] lösung --- aufgabe6-quatal-verbrauch/verbrauch-2025.csv | 10 +++- aufgabe6-quatal-verbrauch/verbrauch.py | 42 +++++++++++++++++++++ aufgabe6-quatal-verbrauch/verbrauch_test.py | 26 +++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/aufgabe6-quatal-verbrauch/verbrauch-2025.csv b/aufgabe6-quatal-verbrauch/verbrauch-2025.csv index f398716..70bc20d 100644 --- a/aufgabe6-quatal-verbrauch/verbrauch-2025.csv +++ b/aufgabe6-quatal-verbrauch/verbrauch-2025.csv @@ -1,6 +1,10 @@ # 2025 -5,5; 30; 32; 33,2; 38; 22; 21; 35,3 + 5,5; 30; 32; 33,2; 38; 22; 21; 35,3 30,3; 31,2; 35,4; 31.1; 28; 12; 13; 38 -23,1; 25,4; 35; 32.3; 36,2; 33,4; 38; 12,5 -38,2; 29,4; 31; 30; 28,9; 31,1; 19,2; 25,7 +23,1; 25,4; 35; 32.3; 36,2; 33,4; 38; 12,5 +38,2; 29,4; 31; 30; 28,9; 31,1; 19,2; 25,7 # 2026 +5,5; 40; 42; 44,2; 48; 22; 21; 45,4 +40,4; 41,2; 45,4; 41.1; 28; 12; 14; 48 +24,1; 25,4; 45; 42.4; 46,2; 44,4; 48; 12,5 +48,2; 29,4; 41; 40; 28,9; 41,1; 19,2; 25,7 \ No newline at end of file diff --git a/aufgabe6-quatal-verbrauch/verbrauch.py b/aufgabe6-quatal-verbrauch/verbrauch.py new file mode 100644 index 0000000..c09be26 --- /dev/null +++ b/aufgabe6-quatal-verbrauch/verbrauch.py @@ -0,0 +1,42 @@ +yearly_consumption = dict({}) +""" +{ + "2025": [ + [3.4, 4.6, 3.7, 2.5], + [...] + ], + "2026": [], +} +""" +year = None + +def start_new_year(line: str) -> (bool,int|None): + try: + year_part = line.strip()[1:].strip() + int(year_part) + return (True, year_part) + except : + return (False, None) + +def convert_line_to_consumpt(line:str) -> [float]: + line = line.replace(',', '.') + line = line.split(';') + values = [] + for txt in line: + txt = txt.strip() + values.append(float(txt)) + return values + +with open('./verbrauch-2025.csv') as f: + for line in f.readlines(): + (is_new_year, year_nr) = start_new_year(line) + print(is_new_year, year_nr) + if is_new_year: + year = [] # new list + yearly_consumption[year_nr] = year + continue + year.append( convert_line_to_consumpt(line) ) + +# debug +print(yearly_consumption) + diff --git a/aufgabe6-quatal-verbrauch/verbrauch_test.py b/aufgabe6-quatal-verbrauch/verbrauch_test.py new file mode 100644 index 0000000..7067ab9 --- /dev/null +++ b/aufgabe6-quatal-verbrauch/verbrauch_test.py @@ -0,0 +1,26 @@ +from verbrauch import start_new_year, convert_line_to_consumpt + + +def test_start_new_year(): + line = "# 2025\n" + (is_new_year, year_nr) = start_new_year(line) + assert is_new_year + assert year_nr == "2025" + +def test_not_start_new_year(): + line = "[2025]\n" + (is_new_year, year_nr) = start_new_year(line) + assert not is_new_year + assert year_nr is None + +def test_not_start_new_year(): + line = " # 2025\n" + (is_new_year, year_nr) = start_new_year(line) + print(is_new_year, year_nr) + assert is_new_year + assert year_nr == "2025" + +def test_convert_line_to_consumpt(): + line = "5,5; 30; 32; 33,2; 38; 22; 21; 35,3" + consum = convert_line_to_consumpt(line) + print(consum)# eine liste von 5.5, 30, .... -- Gitblit v1.10.0-SNAPSHOT