forked from tdjohner/Python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab7TJ.py
More file actions
107 lines (78 loc) · 3.06 KB
/
Lab7TJ.py
File metadata and controls
107 lines (78 loc) · 3.06 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
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
#--------------------------
# Lab7TJ.py -
#--------------------------
# Teagan Johner -
#--------------------------
# Syntax: intersect(dict1, dict2)
# Parameter: dictionary, dictionary
# Return value: dictionary
# Description: takes two dictionaries and makes a new
# dictionary from them that contains all key/value
# pairs that are the same
def intersect(dict1, dict2):
dict3 = {}
if len(dict1) > len(dict2):
for i in dict1.keys():
for k in dict2.keys():
if i in k and dict1[i] == dict2[k]:
dict3[i] = dict1[i]
else:
for i in dict2.keys():
for k in dict1.keys():
if i in k and dict2[i] == dict1[k]:
dict3[i] = dict1[i]
return dict3
# Syntax: is_submap(dict1, dict2)
# Parameter: dictionary, dictionary
# Return value: True / False
# Description: makes sure all key/value pairs in the
# first dictionary are in the second
def is_submap(dict1, dict2):
mapFail = 0
for i in dict1.keys():
for k in dict2.keys():
if i in k and dict1[i] == dict2[k]:
mapFail = 1
if mapFail == 1:
return True
else:
return False
# Syntax: is_neighbour(stringA, stringB)
# Parameter: string, string
# Return value: True / False
# Description: returns True if the edit distance between
# strings is 1, othwerwise returns False
def is_neighbour(stringA, stringB):
editCount = 0
if len(stringA) != len(stringB):
return False
else:
for i in range(len(stringA)):
if stringA[i] != stringB[i]:
editCount += 1
if editCount != 1:
return False
else:
return True
# Syntax: build_neighbour_map(fileName)
# Parameter: string (name of file)
# Return value: dictionary
# Description: takes all strings in a file and returns
# a dictionary containing all neighbors as
# values for neighbour keys
def build_neighbour_map(fileName):
neighbourDict = {}
nameList = []
try:
with open(fileName) as file:
for line in file:
nameList.append(line)
for i in range(len(nameList)):
subNameList = []
for k in range(len(nameList)):
if is_neighbour(nameList[i], nameList[k]) == True:
subNameList.append(nameList[k])
neighbourDict[nameList[i]] = subNameList
except:
return None
return neighbourDict