-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyhedra(script).py
148 lines (127 loc) · 5.79 KB
/
polyhedra(script).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def create_polyhedra_database_from_file(file_path):
database = []
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if line:
try:
polyhedron_data = line.split(',')
name = polyhedron_data[0].strip()
wythoff = polyhedron_data[1].strip()
symbol = polyhedron_data[2].strip()
c_num = polyhedron_data[3].strip()
w_num = polyhedron_data[4].strip()
u_num = polyhedron_data[5].strip()
k_num = polyhedron_data[6].strip()
vertices = int(polyhedron_data[7].strip())
edges = int(polyhedron_data[8].strip())
faces = int(polyhedron_data[9].strip())
faces_by_type = polyhedron_data[10].strip()
polyhedron = {
'name': name,
'wythoff': wythoff,
'symbol': symbol,
'c_num': c_num,
'w_num': w_num,
'u_num': u_num,
'k_num': k_num,
'vertices': vertices,
'edges': edges,
'faces': faces,
'faces_by_type': faces_by_type
}
database.append(polyhedron)
except IndexError:
print("Invalid data format in line: ", line)
return database
def filter_polyhedra_by_field(database, field_name, field_value):
filtered_polyhedra = []
for polyhedron in database:
if field_name in polyhedron and polyhedron[field_name] == field_value:
filtered_polyhedra.append(polyhedron)
return filtered_polyhedra
def group_polyhedra_by_attribute(database, attribute):
grouped_polyhedra = {}
for polyhedron in database:
if attribute in polyhedron:
attribute_value = polyhedron[attribute]
if attribute_value in grouped_polyhedra:
grouped_polyhedra[attribute_value].append(polyhedron)
else:
grouped_polyhedra[attribute_value] = [polyhedron]
return grouped_polyhedra
def calculate_similarity(polyhedron1, polyhedron2):
similarity_score = 0
attributes_to_compare = ['vertices', 'edges', 'faces']
for attribute in attributes_to_compare:
if attribute in polyhedron1 and attribute in polyhedron2:
value1 = polyhedron1[attribute]
value2 = polyhedron2[attribute]
similarity_score += abs(value1 - value2)
return similarity_score
def find_most_similar_polyhedron(database, polyhedron):
most_similar_polyhedron = None
min_similarity_score = float('inf')
for other_polyhedron in database:
if other_polyhedron != polyhedron:
similarity_score = calculate_similarity(polyhedron, other_polyhedron)
if similarity_score < min_similarity_score:
min_similarity_score = similarity_score
most_similar_polyhedron = other_polyhedron
return most_similar_polyhedron
def print_polyhedron(polyhedron):
print('Name:', polyhedron['name'])
print('Wythoff:', polyhedron['wythoff'])
print('Symbol:', polyhedron['symbol'])
print('C#:', polyhedron['c_num'])
print('W#:', polyhedron['w_num'])
print('U#:', polyhedron['u_num'])
print('K#:', polyhedron['k_num'])
print('Vertices:', polyhedron['vertices'])
print('Edges:', polyhedron['edges'])
print('Faces:', polyhedron['faces'])
print('Faces by Type:', polyhedron['faces_by_type'])
print()
def interactive_polyhedra_comparison():
file_path = 'polyhedra.txt'
database = create_polyhedra_database_from_file(file_path)
while True:
print("--- Polyhedra Comparison ---")
print("1. Filter polyhedra by field")
print("2. Group polyhedra by attribute")
print("3. Find most similar polyhedron")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '1':
field_name = input("Enter the field name to filter by: ")
field_value = input("Enter the field value to filter by: ")
filtered_polyhedra = filter_polyhedra_by_field(database, field_name, field_value)
print("Filtered Polyhedra:")
for polyhedron in filtered_polyhedra:
print_polyhedron(polyhedron)
elif choice == '2':
attribute = input("Enter the attribute to group polyhedra by: ")
grouped_polyhedra = group_polyhedra_by_attribute(database, attribute)
print("Grouped Polyhedra:")
for attribute_value, polyhedra in grouped_polyhedra.items():
print(f"Attribute: {attribute_value}")
for polyhedron in polyhedra:
print_polyhedron(polyhedron)
print()
elif choice == '3':
polyhedron_name = input("Enter the name of the polyhedron: ")
target_polyhedron = None
for polyhedron in database:
if polyhedron['name'].lower() == polyhedron_name.lower():
target_polyhedron = polyhedron
break
if target_polyhedron:
similar_polyhedron = find_most_similar_polyhedron(database, target_polyhedron)
print("Most Similar Polyhedron:")
print_polyhedron(similar_polyhedron)
else:
print("Polyhedron not found in the database.")
elif choice == '4':
break
print()
interactive_polyhedra_comparison()