forked from johnmee/codility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex-3-1-frogjmp.py
43 lines (30 loc) · 1008 Bytes
/
ex-3-1-frogjmp.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
import unittest
import random
INT_RANGE = (1, 1000000000)
def solution(X, Y, D):
"""
Calculate the miminum number of jumps from X to Y
:param X: start integer
:param Y: minimum end integer
:param D: size of the jump
:return: minium number of jumps in O(1) time and space complexity
"""
# assume X is less than Y
assert X <= Y
quot, rem = divmod(Y-X, D)
return quot if rem == 0 else quot + 1
class TestFrogJump(unittest.TestCase):
def test_example1(self):
self.assertEqual(solution(10, 85, 30), 3)
def test_one(self):
self.assertEqual(solution(0, 10, 1), 10)
def test_big_steps(self):
self.assertEqual(solution(0, 10, 20), 1)
def test_even_steps(self):
self.assertEqual(solution(10, 100, 10), 9)
def test_equal_steps(self):
self.assertEqual(solution(10, 10, 10), 0)
def test_odd_steps(self):
self.assertEqual(solution(9, 29, 10), 2)
if __name__ == '__main__':
unittest.main()