-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_words_distance.py
89 lines (72 loc) · 2.36 KB
/
my_words_distance.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
"""
Created on Nov 19, 2019
This program tests the function,
which finds distance between any
two words in a sentence, even if
input words are the same ones.
Distance here is interpreted as a
number of words between input words
@author: Vlad
"""
import random
import unittest
class TestDistMethods(unittest.TestCase):
myString = "Moscow Portland Boston Sunnyvale Moscow Denver Chicago Boston Washington Miami"
myRandomNmb = random.randint(1, 10)
w1 = "Moscow"
w2 = "Sunnyvale"
def find_distance(self):
words = self.myString.split(" ")
i1 = -1
i2 = -1
for i in range(len(words)):
if i1 == -1 and words[i] == self.w1:
i1 = i
else:
if i2 == -1 and words[i] == self.w2:
i2 = i
distance = i2 - i1 - 1
print ("distance between <" + str(self.w1) + "> and <" + str(self.w2) + "> is " + str(distance))
return distance
@classmethod
def setUpClass(cls):
# This method is called before tests in an individual class are run
print ("Test String: " + cls.myString + "\n")
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
pass
# Method called to prepare the test fixture.
# This is called immediately before calling the test method
def tearDown(self):
pass
def test_case_1(self):
# this test_case uses default data for testing
d = self.find_distance()
self.assertEqual(2, d, "Distance is incorrect")
def test_case_2(self):
self.w1 = "Moscow"
self.w2 = "Moscow"
d = self.find_distance()
self.assertEqual(3, d, "Distance is incorrect")
def test_case_3(self):
self.w1 = "Boston"
self.w2 = "Boston"
d = self.find_distance()
self.assertEqual(4, d, "Distance is incorrect")
def test_case_4(self):
self.w1 = "Boston"
self.w2 = "Washington"
d = self.find_distance()
self.assertEqual(5, d, "Distance is incorrect")
def test_case_5(self):
self.w1 = "Moscow"
self.w2 = "Miami"
d = self.find_distance()
self.assertEqual(8, d, "Distance is incorrect")
if __name__ == '__main__':
# function below will be executed only
# if we run this program as a script,
# i.e. - "python my_files.py"
unittest.main()