Repository: riyasyash/CompetitiveProgramming

Issue:
Column Name to Number
Come out with an algorithm for getting the column number provided the column name in an excel sheet and vice versa. Excel has a naming convention of A, B..Z, AA, AB, AC...ZZ,AAA... This had to be converted to the column numbers. A will be 1 and AA will 27... Also the algorithm to find the name provided column number. 

Comments:

Patch:
diff --git a/NumberSystems/ExcelConverter.py b/NumberSystems/ExcelConverter.py
new file mode 100644
index 0000000..27c14d7
--- /dev/null
+++ b/NumberSystems/ExcelConverter.py
@@ -0,0 +1,22 @@
+class ExcelConverter:
+    column_start_ascii = 65
+    column = None
+    column_number = None
+    column_literal_base = 26
+
+    def value(self, column_literal):
+        return (ord(column_literal) - self.column_start_ascii) + 1
+
+    def is_valid_column(self):
+        return self.column.isalpha()
+
+    def to_capital(self):
+        self.column = self.column.upper()
+
+    def to_column_number(self):
+        if self.is_valid_column():
+            self.to_capital()
+            self.column = self.column[::-1]
+            self.column_number = 0
+            for i, literal in enumerate(self.column):
+                self.column_number += pow(self.column_literal_base, i) * self.value(literal)
diff --git a/NumberSystems/__init__.py b/NumberSystems/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/Readme.md b/Readme.md
index 2e17a97..725a66f 100644
--- a/Readme.md
+++ b/Readme.md
@@ -16,3 +16,6 @@ This repo contains the implementations of different data structures and solution
 #### [Queue](Queue)
 * [Implementation of Queue](Queue/Queue.py)
 * [Stack using Queue](Queue/stack_with_queue.py)
+
+#### [Number Systems](NumberSystems)
+* [Convert Excel column names to numbers](NumberSystems/ExcelConverter.py)


Test patch:
diff --git a/tests/test_excel_converter.py b/tests/test_excel_converter.py
new file mode 100644
index 0000000..a293276
--- /dev/null
+++ b/tests/test_excel_converter.py
@@ -0,0 +1,64 @@
+import unittest
+
+from NumberSystems.ExcelConverter import ExcelConverter
+
+
+class TestExcelConverter(unittest.TestCase):
+    def setUp(self):
+        self.converter = ExcelConverter()
+
+    def tearDown(self):
+        self.converter.column_number = None
+        self.converter.column = None
+
+    def test_converter_is_initialised(self):
+        self.assertIsNone(self.converter.column_number)
+        self.assertIsNone(self.converter.column)
+
+    def test_to_capital(self):
+        self.converter.column = 'Hello world'
+        self.converter.to_capital()
+        self.assertEqual('HELLO WORLD', self.converter.column)
+
+    def test_is_valid_column(self):
+        self.converter.column = 'apple'
+        self.assertEqual(True, self.converter.is_valid_column())
+        self.converter.column = 'a1pple'
+        self.assertEqual(False, self.converter.is_valid_column())
+        self.converter.column = 'a pple'
+        self.assertEqual(False, self.converter.is_valid_column())
+        self.converter.column = 'a-pple'
+        self.assertEqual(False, self.converter.is_valid_column())
+        self.converter.column = '123'
+        self.assertEqual(False, self.converter.is_valid_column())
+
+    def test_converter_converts_single_colums(self):
+        self.converter.column = 'A'
+        self.converter.to_column_number()
+        self.assertEqual(1, self.converter.column_number)
+        self.converter.column = 'B'
+        self.converter.to_column_number()
+        self.assertEqual(2, self.converter.column_number)
+        self.converter.column = 'z'
+        self.converter.to_column_number()
+        self.assertEqual(26, self.converter.column_number)
+
+    def test_converter_converts_double_colums(self):
+        self.converter.column = 'AA'
+        self.converter.to_column_number()
+        self.assertEqual(27, self.converter.column_number)
+        self.converter.column = 'AB'
+        self.converter.to_column_number()
+        self.assertEqual(28, self.converter.column_number)
+        self.converter.column = 'BC'
+        self.converter.to_column_number()
+        self.assertEqual(55, self.converter.column_number)
+
+    def test_converter_converts_multiple_colums(self):
+        self.converter.column = 'AAZZ'
+        self.converter.to_column_number()
+        self.assertEqual(18954, self.converter.column_number)
+        self.converter.column = 'ZZAAA'
+        self.converter.to_column_number()
+        self.assertEqual(12339055, self.converter.column_number)
+