-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPunster.py
More file actions
48 lines (47 loc) · 1.81 KB
/
Copy pathPunster.py
File metadata and controls
48 lines (47 loc) · 1.81 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
def fit(tostr, fromstr, leeway=1):
tostr = [letter.lower() for letter in tostr]
fromstr = [letter.lower() for letter in fromstr]
scores = [[0 for j in range(len(fromstr))] for i in range(len(tostr))]
highs, highscore = [], 0
for i in range(len(tostr)):
for j in range(len(fromstr)):
lastchance = 0
for k in range(min(len(tostr) - i, len(fromstr) - j)):
if tostr[i + k] == fromstr[j + k]:
scores[i][j] += 1 + lastchance
lastchance = 0
elif lastchance < leeway:
lastchance += 1
else:
break
if scores[i][j] > highscore:
highscore = scores[i][j]
highs = [(i, j)]
print(i, j, scores[i][j])
elif scores[i][j] == highscore:
highs.append((i, j))
returnstrs = []
for high in highs:
offset = 0
returnstrlist = tostr.copy()
for k in range(len(fromstr)):
if k - high[1] < 0:
returnstrlist.insert(high[0] + offset, fromstr[k])
offset += 1
elif k - high[1] >= highscore or high[0] + k - high[1] > len(tostr):
returnstrlist.insert(high[0] + offset + highscore, fromstr[k])
offset += 1
else:
returnstrlist[high[0] + offset + k] = fromstr[k]
returnstr = ""
for letter in returnstrlist:
returnstr += letter
returnstrs.append(returnstr)
return returnstrs
while True:
phrase = input("Phrase in which to fit pun\n>> ")
word = input("Word to fit in phrase for pun\n>> ")
for i in range(1, 4):
print("\n".join(fit(phrase,
word,
i)) + "\n")