add code for lab 23

This commit is contained in:
dymik739 2023-04-27 14:17:57 +03:00
parent 34da704168
commit b961005965
2 changed files with 72 additions and 0 deletions

46
labs/23/processor.py Normal file
View File

@ -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")

26
labs/23/sample_data.txt Normal file
View File

@ -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