-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassignment.py
55 lines (41 loc) · 1.26 KB
/
assignment.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
# ----submitted by Rupak----
# Given a text file as input containing the input in the following format. Return the linear and circular distance
# sample input:
# cake make
# pod lot
# abc def
# xyz abc
# rail liar
# sample output:
# 10 10
# 20 14
# 9 9
# 69 9
# 28 28
import sys
# function to calculate linear and circular distance between the given two words
def distance(word1: str, word2: str) -> tuple:
word1, word2 = word1.lower(), word2.lower()
# if the length of the words are different then return
if(len(word1) != len(word2)):
print('skipping current line (words are of different length)')
return ()
dist_linear = 0
dist_circular = 0
for i in range(len(word1)):
current = abs(ord(word1[i]) - ord(word2[i]))
dist_linear += current
dist_circular += min(current, 26 - current)
return (dist_linear, dist_circular)
# driver code
if __name__ == '__main__':
try:
with open(sys.argv[1], 'r') as f:
lines = f.readlines()
for line in lines:
word1, word2 = line.split()
res = distance(word1, word2)
print(res[0], res[1])
# if file not found then print the following text
except:
print('text file not found')