Repository: keon/algorithms
Pull request #339 (2018-06-08 05:14:59)

Add decimal_to_binary_ip.py
(Put an `X` inside the `[ ]` to denote check mark `[X]`.)


- [x] **If creating a new file :**
  - [x] added links to it in the README files ?
  - [x] included tests with it ?
  - [x] added description (overview of algorithm, time and space compleixty, and possible edge case) in docstrings ?

- [ ] **if done some changes :**
  - [ ] wrote short description in the PR explaining what the changes do ?
  - [ ] Fixes #[issue number] if related to any issue

- [ ] **other**


keon: @goswami-rahul we are going to automate this soon with sphinx, so no need to sweat it.
goswami-rahul: @keon Thanks for the update. :+1: 

Patch:
diff --git a/README.md b/README.md
index 8cbf568..c954e5f 100644
--- a/README.md
+++ b/README.md
@@ -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)
diff --git a/README_JP.md b/README_JP.md
index f47bec7..b81f9ce 100644
--- a/README_JP.md
+++ b/README_JP.md
@@ -38,7 +38,7 @@ Python 3で開発された簡単で明確なデータ構造とアルゴリズム
 
     $ pip3 install git+https://github.com/keon/algorithms
 
-Pythonファイルを作成してテストを行うことができます：（例：「sort」の「merge_sort」を使用）
+Pythonファイルを作成してテストを実行することができます：（例：「sort」の「merge_sort」を使用）
 
 ```python3
 from sort import merge_sort
diff --git a/algorithms/maths/__init__.py b/algorithms/maths/__init__.py
index 2bf44a7..543d8d3 100644
--- a/algorithms/maths/__init__.py
+++ b/algorithms/maths/__init__.py
@@ -1,4 +1,5 @@
 from .base_conversion import *
+from .decimal_to_binary_ip import *
 from .extended_gcd import *
 from .factorial import *
 from .gcd import *
diff --git a/algorithms/maths/decimal_to_binary_ip.py b/algorithms/maths/decimal_to_binary_ip.py
new file mode 100644
index 0000000..579e340
--- /dev/null
+++ b/algorithms/maths/decimal_to_binary_ip.py
@@ -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)


Test patch:
diff --git a/tests/test_maths.py b/tests/test_maths.py
index ece9ab7..1514907 100644
--- a/tests/test_maths.py
+++ b/tests/test_maths.py
@@ -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,
@@ -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):