From 893bfe2c517a35a5c4e6af65fb3dc75163ef4629 Mon Sep 17 00:00:00 2001 From: BrianLusina <12752833+BrianLusina@users.noreply.github.com> Date: Mon, 16 Dec 2024 10:30:38 +0300 Subject: [PATCH] feat(strings): spreadsheet encoding --- puzzles/permutations_check/README.md | 20 +++++++ puzzles/permutations_check/__init__.py | 16 ++++++ puzzles/permutations_check/test_check.py | 53 +++++++++++++++++++ pystrings/spreadsheet_encoding/README.md | 3 ++ pystrings/spreadsheet_encoding/__init__.py | 32 +++++++++++ .../test_spreadsheet_encode.py | 15 ++++++ 6 files changed, 139 insertions(+) create mode 100644 puzzles/permutations_check/README.md create mode 100644 puzzles/permutations_check/__init__.py create mode 100644 puzzles/permutations_check/test_check.py create mode 100644 pystrings/spreadsheet_encoding/README.md create mode 100644 pystrings/spreadsheet_encoding/__init__.py create mode 100644 pystrings/spreadsheet_encoding/test_spreadsheet_encode.py diff --git a/puzzles/permutations_check/README.md b/puzzles/permutations_check/README.md new file mode 100644 index 00000000..421eedc1 --- /dev/null +++ b/puzzles/permutations_check/README.md @@ -0,0 +1,20 @@ +# Permutation Game + +There are n(n is even) players, conveniently labeled 1,2,3...n. These players will play m rounds of games. In each round +of games. In each round of games, the players are split into 2 teams of n/2 players each. Two players x bool: + pairs = [(i, j) for i in range(1, n + 1) for j in range(i + 1, n + 1)] + + for game_round in games: + for i, j in pairs: + + if i in game_round[:n // 2] and j in game_round[:n // 2]: + return False + + if i in game_round[n // 2:] and j in game_round[n // 2:]: + return False + + return True diff --git a/puzzles/permutations_check/test_check.py b/puzzles/permutations_check/test_check.py new file mode 100644 index 00000000..6d096343 --- /dev/null +++ b/puzzles/permutations_check/test_check.py @@ -0,0 +1,53 @@ +import unittest + +from . import check + + +class CheckTestCase(unittest.TestCase): + def test_1(self): + """should return True for n = 2, m = 1, games=[[1, 2]]""" + n = 2 + m = 1 + games = [[1, 2]] + actual = check(n, m, games) + self.assertTrue(actual) + + def test_2(self): + """should return False for n = 4, m = 2, games=[[1, 2, 3, 4], [4,3,1,2]]""" + n = 4 + m = 2 + games = [[1, 2, 3, 4], [4, 3, 1, 2]] + actual = check(n, m, games) + self.assertFalse(actual) + + def test_3(self): + """should return False for n = 4, m = 2, games=[[1, 2, 3, 4], [1, 3, 2, 4]]""" + n = 4 + m = 2 + games = [[1, 2, 3, 4], [1, 3, 2, 4]] + actual = check(n, m, games) + self.assertTrue(actual) + + def test_4(self): + """should return False for n = 6, m = 6, games=[[1, 6, 4, 5, 2], [6, 4, 2, 3, 1, 5], [4, 2, 1, 5, 6, 3], [4, 5, 1, 6, 2, 3], [3, 2, 5, 1, 6, 4], + [2, 3, 6, 4, 1, 5]]""" + n = 6 + m = 6 + games = [[1, 6, 4, 5, 2], [6, 4, 2, 3, 1, 5], [4, 2, 1, 5, 6, 3], [4, 5, 1, 6, 2, 3], [3, 2, 5, 1, 6, 4], + [2, 3, 6, 4, 1, 5]] + actual = check(n, m, games) + self.assertTrue(actual) + + def test_5(self): + """should return False for n = 6, m = 6, games=[[3, 1, 4, 5, 6, 2], [5, 3, 2, 4, 1, 6], [5, 3, 6, 4, 2, 1], [6, 5, 3, 2, 1, 4], [5, 4, 1, 2, 6, 3], + [4, 1, 6, 2, 5, 3]]""" + n = 6 + m = 6 + games = [[3, 1, 4, 5, 6, 2], [5, 3, 2, 4, 1, 6], [5, 3, 6, 4, 2, 1], [6, 5, 3, 2, 1, 4], [5, 4, 1, 2, 6, 3], + [4, 1, 6, 2, 5, 3]] + actual = check(n, m, games) + self.assertFalse(actual) + + +if __name__ == '__main__': + unittest.main() diff --git a/pystrings/spreadsheet_encoding/README.md b/pystrings/spreadsheet_encoding/README.md new file mode 100644 index 00000000..d140ca1e --- /dev/null +++ b/pystrings/spreadsheet_encoding/README.md @@ -0,0 +1,3 @@ +# Spreadsheet Encoding + +We will be considering how to solve the problem of implementing a function that converts a spreadsheet column ID (i.e., “A”, “B”, “C”, …, “Z”, “AA”, etc.) to the corresponding integer. For example, “A” equals 1 because it represents the first column, while “AA” equals 27 because it represents the 27th column. diff --git a/pystrings/spreadsheet_encoding/__init__.py b/pystrings/spreadsheet_encoding/__init__.py new file mode 100644 index 00000000..cd60c288 --- /dev/null +++ b/pystrings/spreadsheet_encoding/__init__.py @@ -0,0 +1,32 @@ +def spreadsheet_encode_column(column_id: str) -> int: + """ + Encodes a Spreadsheet column ID as an integer and returns it. Here the encoding uses the alphabets A, B, C, etc. and + further uses the indexing of the alphabet starting from 1 like: A=1, B=2, C=3,..., Z=26 + + Complexity + Where n represents the number of characters in the column_id + + - Time O(n) as we iterate over each character in the column ID to calculate the encoding + - Space O(1) no extra space is required to perform the encoding + + Args: + column_id (str): the column ID(name) as represented on a spreadsheet + Returns: + int: encoded column id as an integer + """ + num = 0 + # count will help in determining the power of the base as we convert the column id into the corresponding integer + count = len(column_id) - 1 + + for char in column_id: + # we use the base 26 system here as there are 26 letters in the alphabet + # ord(char) returns the unicode code point for char. In order to make sure that A=1, we need to determine the + # relative difference from the result given by ord & from the representation we require for base 26 system + # Now we know that ord('A') equals 65. So if we find the Unicode code point using ord() of a character, + # subtract ord('A') from it, and then add 1 to it, we’ll get the representation we want for the base 26 system + num += 26**count * (ord(char) - ord('A') + 1) + + # count is decremented by 1 as the power of the base decrements by 1 from the digits from left to right + count -= 1 + + return num diff --git a/pystrings/spreadsheet_encoding/test_spreadsheet_encode.py b/pystrings/spreadsheet_encoding/test_spreadsheet_encode.py new file mode 100644 index 00000000..477111a9 --- /dev/null +++ b/pystrings/spreadsheet_encoding/test_spreadsheet_encode.py @@ -0,0 +1,15 @@ +import unittest +from . import spreadsheet_encode_column + + +class SpreadsheetEncodeColumnTestCases(unittest.TestCase): + def test_zz(self): + """ZZ should return 702""" + column_name = "ZZ" + expected = 702 + actual = spreadsheet_encode_column(column_name) + self.assertEqual(expected,actual) + + +if __name__ == '__main__': + unittest.main()