From b96100596554ad663e6e0e94778ddcd3b3114568 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Thu, 27 Apr 2023 14:17:57 +0300 Subject: [PATCH] add code for lab 23 --- labs/23/processor.py | 46 +++++++++++++++++++++++++++++++++++++++++ labs/23/sample_data.txt | 26 +++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 labs/23/processor.py create mode 100644 labs/23/sample_data.txt diff --git a/labs/23/processor.py b/labs/23/processor.py new file mode 100644 index 0000000..33757ca --- /dev/null +++ b/labs/23/processor.py @@ -0,0 +1,46 @@ +import prettytable + +tables = {} +active_table_id = 0 +r = 3 + +def avg(a): + return sum(a) / len(a) + +with open("sample_data.txt") as f: + for l in f: + if l.rstrip("\n\r") == '': + continue + + if l[0] == "#": + sl = l[1:].split() + + if len(sl) == 2: + if sl[0] == "table": + new_id = int(sl[1]) + tables[new_id] = {"s": [], "C": 0.0} + active_table_id = new_id + + elif sl[0] == "C": + tables[active_table_id]["C"] = float(sl[1]) + + else: + tables[active_table_id]['s'].append(list(map(float, l.split()))) + +ti = list(tables.items()) +ti.sort(key = lambda x: x[0]) + +for i, t in ti: + print(f"Table {i}") + + s = t['s'] + c = t['C'] + + avg_n = [avg(a) for a in zip(*s)] + print("❬n❭ : " + ", ".join(list(map(lambda x: str(round(x, r)), avg_n)))) + + avg_Cix_U = [c * avg_n[2*a] / avg_n[2*a+1] for a in range(2)] + print("❬Cix(Ui)❭ : " + ", ".join(list(map(lambda x: str(round(x, r)), avg_Cix_U)))) + + avg_Cix_final = avg(avg_Cix_U) + print(f"Результат для ❬Сix❭: {avg_Cix_final}\n") diff --git a/labs/23/sample_data.txt b/labs/23/sample_data.txt new file mode 100644 index 0000000..224388c --- /dev/null +++ b/labs/23/sample_data.txt @@ -0,0 +1,26 @@ +# table 1 +# C 0.82 + +4.5 1.1 0.6 1.7 +4.3 1.0 0.7 1.6 + + +# table 2 +# C 1.5 + +4.5 2.0 0.6 3.1 +4.3 1.9 0.7 3.0 + + +# table 3 +# C 2.7 + +4.5 3.8 0.6 3.1 +4.3 3.9 0.7 3.0 + + +# table 4 +# C 0.22 + +4.5 2.6 0.6 4.9 +4.3 2.9 0.7 4.6