This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWriter.py
More file actions
38 lines (33 loc) · 1.27 KB
/
Writer.py
File metadata and controls
38 lines (33 loc) · 1.27 KB
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
import random
class Writer():
def __is_edge(self,word1, word2): # examine those two words whether they have edge or not
count = 0
for letter in word1:
if letter in word2:
count += 1
word2 = word2.replace(letter, '', 1) # remove the counting element to not counts again
if count == len(word1) - 3: # 3 letters are different
return True
else:
return False
def ident_vert(self,amount):
file2 = open("words7.txt", "r")
words = file2.read().strip().split()
file2.close()
vertices = random.sample(words, amount) # the stament that determines # of vertices in the graph
return vertices
def write_to(self,filename,amount):
vertices=self.ident_vert(amount)
file = open(filename, "w")
file.writelines("graph = {\n")
for i in vertices:
file.writelines("\"" + i + "\":[")
for y in vertices:
if i == y:
continue
else:
if self.__is_edge(i, y):
file.writelines("\"" + y + "\",")
file.writelines("],\n")
file.writelines("}")
file.close()