Skip to content

Commit 32b20e2

Browse files
victore07keon
authored andcommitted
Add decimal_to_binary_ip.py (keon#339)
* Add decimal_to_binary_ip.py Converts dotted_decimal ip address to binary ip address. * Include tests for decimal_to_binary_ip Some tests cases for decimal_to_binary_ip function. * Fix TestDecimalToBinaryIP method name changed method from test_int2base to test_decimal_to_binary_ip * Import decimal_to_binary_ip Added decimal_to_binary_ip to imports * Update README.md Add to decimal_to_binary_ip * resolve conflicts in test_maths
1 parent d1b0999 commit 32b20e2

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ If you want to uninstall algorithms, it is as simple as:
176176
- [maths](algorithms/maths)
177177
- [base_conversion](algorithms/maths/base_conversion.py)
178178
- [combination](algorithms/maths/combination.py)
179+
- [decimal_to_binary_ip](algorithms/maths/decimal_to_binary_ip.py)
179180
- [extended_gcd](algorithms/maths/extended_gcd.py)
180181
- [factorial](algorithms/maths/factorial.py)
181182
- [gcd/lcm](algorithms/maths/gcd.py)

algorithms/maths/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .base_conversion import *
2+
from .decimal_to_binary_ip import *
23
from .extended_gcd import *
34
from .factorial import *
45
from .gcd import *
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Given an ip address in dotted-decimal representation, determine the
3+
binary representation. For example,
4+
decimal_to_binary(255.0.0.5) returns 11111111.00000000.00000000.00000101
5+
accepts string
6+
returns string
7+
"""
8+
9+
def decimal_to_binary_util(val):
10+
bits = [128, 64, 32, 16, 8, 4, 2, 1]
11+
val = int(val)
12+
binary_rep = ''
13+
for bit in bits:
14+
if val >= bit:
15+
binary_rep += str(1)
16+
val -= bit
17+
else:
18+
binary_rep += str(0)
19+
20+
return binary_rep
21+
22+
def decimal_to_binary_ip(ip):
23+
values = ip.split('.')
24+
binary_list = []
25+
for val in values:
26+
binary_list.append(decimal_to_binary_util(val))
27+
return '.'.join(binary_list)

tests/test_maths.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from algorithms.maths import (
22
int_to_base, base_to_int,
3+
decimal_to_binary_ip,
34
extended_gcd,
45
factorial, factorial_recur,
56
gcd, lcm,
@@ -25,15 +26,29 @@ class TestBaseConversion(unittest.TestCase):
2526
unittest {[type]} -- [description]
2627
"""
2728

28-
def test_int2base(self):
29+
def test_int_to_base(self):
2930
self.assertEqual("101", int_to_base(5, 2))
3031
self.assertEqual("0", int_to_base(0, 2))
3132
self.assertEqual("FF", int_to_base(255, 16))
3233

33-
def test_base2int(self):
34+
def test_base_to_int(self):
3435
self.assertEqual(5, base_to_int("101", 2))
3536
self.assertEqual(0, base_to_int("0", 2))
3637
self.assertEqual(255, base_to_int("FF", 16))
38+
39+
40+
class TestDecimalToBinaryIP(unittest.TestCase):
41+
"""
42+
Test for the file decimal_to_binary_ip.py
43+
44+
Arguments:
45+
unittest {[type]} -- [description]
46+
"""
47+
48+
def test_decimal_to_binary_ip(self):
49+
self.assertEqual("00000000.00000000.00000000.00000000", decimal_to_binary_ip("0.0.0.0"))
50+
self.assertEqual("11111111.11111111.11111111.11111111", decimal_to_binary_ip("255.255.255.255"))
51+
self.assertEqual("11000000.10101000.00000000.00000001", decimal_to_binary_ip("192.168.0.1"))
3752

3853

3954
class TestExtendedGcd(unittest.TestCase):

0 commit comments

Comments
 (0)