-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragonBallKata.py
87 lines (69 loc) · 2.28 KB
/
DragonBallKata.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
# DragonBall Kata
import unittest
class TestDragonBall(unittest.TestCase):
def test_1_1(self):
thing = DragonBall(1,1)
self.assertEqual(".", thing.output())
def test_1_2(self):
thing = DragonBall(2,2)
self.assertEqual("..\n..", thing.output())
def test_1_3(self):
thing = DragonBall(3, 1)
self.assertEqual("...", thing.output())
def test_1_4(self):
thing = DragonBall(1, 2)
self.assertEqual(".\n.", thing.output())
def test_2_1(self):
thing = DragonBall(1, 1)
thing.setAt(1, 1)
self.assertEqual("*", thing.output())
def test_2_2(self):
thing = DragonBall(3, 1)
thing.setAt(2, 1)
self.assertEqual(".*.", thing.output())
def test_2_4(self):
thing = DragonBall(1,2)
thing.setAt(1,2)
self.assertEqual(".\n*", thing.output())
def test_position1d_1_1(self):
thing = DragonBall(1,1)
result = thing.position1d_from_2d(1,1)
expected = 0
self.assertEqual(expected, result)
def test_position1d_2_2(self):
thing = DragonBall(2,2)
result = thing.position1d_from_2d(2,1)
expected = 1
self.assertEqual(expected, result)
def test_position1d_1_2(self):
thing = DragonBall(2,2)
result = thing.position1d_from_2d(1,2)
expected = 3
self.assertEqual(expected, result)
class DragonBall():
def __init__(self, arg1, arg2):
self.columns = arg1
self.lines = arg2
self.starX = 0
self.starY = 0
line = '.' * self.columns + "\n"
self.result = line * self.lines
self.result = self.result[:-1]
def position1d_from_2d(self, posX, posY):
posX_0 = posX - 1
posY_0 = posY - 1
return (self.columns + 1) * posY_0 + posX_0
def output(self):
if (self.starX != 0):
self.addStar()
return self.result
def addStar(self):
position = self.position1d_from_2d(self.starX, self.starY)
self.result = list(self.result)
self.result[position] = '*'
self.result = ''.join(self.result)
def setAt(self, arg1, arg2):
self.starX = arg1
self.starY = arg2
if __name__ == '__main__':
unittest.main()