Skip to content

Commit

Permalink
Add decimal_to_binary_ip.py (keon#339)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
victore07 authored and keon committed Jun 9, 2018
1 parent d1b0999 commit 32b20e2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ If you want to uninstall algorithms, it is as simple as:
- [maths](algorithms/maths)
- [base_conversion](algorithms/maths/base_conversion.py)
- [combination](algorithms/maths/combination.py)
- [decimal_to_binary_ip](algorithms/maths/decimal_to_binary_ip.py)
- [extended_gcd](algorithms/maths/extended_gcd.py)
- [factorial](algorithms/maths/factorial.py)
- [gcd/lcm](algorithms/maths/gcd.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/maths/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .base_conversion import *
from .decimal_to_binary_ip import *
from .extended_gcd import *
from .factorial import *
from .gcd import *
Expand Down
27 changes: 27 additions & 0 deletions algorithms/maths/decimal_to_binary_ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Given an ip address in dotted-decimal representation, determine the
binary representation. For example,
decimal_to_binary(255.0.0.5) returns 11111111.00000000.00000000.00000101
accepts string
returns string
"""

def decimal_to_binary_util(val):
bits = [128, 64, 32, 16, 8, 4, 2, 1]
val = int(val)
binary_rep = ''
for bit in bits:
if val >= bit:
binary_rep += str(1)
val -= bit
else:
binary_rep += str(0)

return binary_rep

def decimal_to_binary_ip(ip):
values = ip.split('.')
binary_list = []
for val in values:
binary_list.append(decimal_to_binary_util(val))
return '.'.join(binary_list)
19 changes: 17 additions & 2 deletions tests/test_maths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from algorithms.maths import (
int_to_base, base_to_int,
decimal_to_binary_ip,
extended_gcd,
factorial, factorial_recur,
gcd, lcm,
Expand All @@ -25,15 +26,29 @@ class TestBaseConversion(unittest.TestCase):
unittest {[type]} -- [description]
"""

def test_int2base(self):
def test_int_to_base(self):
self.assertEqual("101", int_to_base(5, 2))
self.assertEqual("0", int_to_base(0, 2))
self.assertEqual("FF", int_to_base(255, 16))

def test_base2int(self):
def test_base_to_int(self):
self.assertEqual(5, base_to_int("101", 2))
self.assertEqual(0, base_to_int("0", 2))
self.assertEqual(255, base_to_int("FF", 16))


class TestDecimalToBinaryIP(unittest.TestCase):
"""
Test for the file decimal_to_binary_ip.py
Arguments:
unittest {[type]} -- [description]
"""

def test_decimal_to_binary_ip(self):
self.assertEqual("00000000.00000000.00000000.00000000", decimal_to_binary_ip("0.0.0.0"))
self.assertEqual("11111111.11111111.11111111.11111111", decimal_to_binary_ip("255.255.255.255"))
self.assertEqual("11000000.10101000.00000000.00000001", decimal_to_binary_ip("192.168.0.1"))


class TestExtendedGcd(unittest.TestCase):
Expand Down

0 comments on commit 32b20e2

Please sign in to comment.