Skip to content

Commit 01b7481

Browse files
committed
made code PEP8 compliant
1 parent 7878fc0 commit 01b7481

2 files changed

Lines changed: 45 additions & 34 deletions

File tree

algorithms/math/extended_gcd.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
"""
2-
extended_gcd.py
2+
extended_gcd.py
33
4-
This module implements the extended greatest common divider algorithm.
4+
This module implements the extended greatest common divider algorithm.
55
6-
Pre: two integers a and b
7-
Post: a tuple (x, y) where a*x + b*y = gcd(a, b)
6+
Pre: two integers a and b
7+
Post: a tuple (x, y) where a*x + b*y = gcd(a, b)
88
9-
Pseudo Code: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
9+
Pseudo Code: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
1010
"""
1111

12+
1213
def extended_gcd(p, q):
1314

14-
(a, b) = (p, q)
15+
(a, b) = (p, q)
16+
17+
if a < 0:
18+
a = -1 * a
19+
20+
if b < 0:
21+
b = -1 * b
22+
23+
x0 = 0
24+
y1 = 0
1525

16-
if a < 0: a = -1 * a
17-
if b < 0: b = -1 * b
26+
x1 = 1
27+
y0 = 1
1828

19-
x0 = y1 = 0
20-
x1 = y0 = 1
29+
while(b != 0):
30+
quotient = a / b
31+
(a, b) = (b, a % b)
32+
(x1, x0) = (x0 - quotient * x1, x1)
33+
(y1, y0) = (y0 - quotient * y1, y1)
2134

22-
while(b!=0):
23-
quotient = a / b
24-
(a,b) = (b, a % b)
25-
(x1 , x0) = (x0 - quotient * x1, x1)
26-
(y1 , y0) = (y0 - quotient * y1, y1)
35+
if p < 0:
36+
y0 = -1 * y0
2737

28-
if p < 0: y0 = -1 * y0
29-
if q < 0: x0 = -1 * x0
38+
if q < 0:
39+
x0 = -1 * x0
3040

31-
return (y0, x0)
41+
return (y0, x0)

algorithms/tests/test_math.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import unittest
22
from ..math.extended_gcd import extended_gcd
33

4+
45
class TestExtendedGCD(unittest.TestCase):
56

6-
def test_extended_gcd(self):
7-
# Find extended_gcd of 35 and 77
8-
(a,b) = extended_gcd(35, 77)
9-
self.assertIs(35*a + 77*b, 7)
7+
def test_extended_gcd(self):
8+
# Find extended_gcd of 35 and 77
9+
(a, b) = extended_gcd(35, 77)
10+
self.assertIs(35 * a + 77 * b, 7)
1011

11-
# Find extended_gcd of 15 and 19
12-
(a,b) = extended_gcd(15, 19)
13-
self.assertIs(15*a + 19*b, 1)
12+
# Find extended_gcd of 15 and 19
13+
(a, b) = extended_gcd(15, 19)
14+
self.assertIs(15 * a + 19 * b, 1)
1415

15-
# Find extended_gcd of 18 and 9
16-
(a,b) = extended_gcd(18, 9)
17-
self.assertIs(18*a + 9*b, 9)
16+
# Find extended_gcd of 18 and 9
17+
(a, b) = extended_gcd(18, 9)
18+
self.assertIs(18 * a + 9 * b, 9)
1819

19-
# Find extended_gcd of 99 and 81
20-
(a,b) = extended_gcd(99, 81)
21-
self.assertIs(99*a + 81*b, 9)
20+
# Find extended_gcd of 99 and 81
21+
(a, b) = extended_gcd(99, 81)
22+
self.assertIs(99 * a + 81 * b, 9)
2223

23-
# Find extended_gcd of 50 and 15
24-
(a,b) = extended_gcd(50, 15)
25-
self.assertIs(50*a + 15*b, 5)
24+
# Find extended_gcd of 50 and 15
25+
(a, b) = extended_gcd(50, 15)
26+
self.assertIs(50 * a + 15 * b, 5)

0 commit comments

Comments
 (0)