From 2ff8fd113f358b043f75fd068c9f05b57c017803 Mon Sep 17 00:00:00 2001 From: Sebastian Slomski Date: Tue, 3 Mar 2015 16:52:02 +0000 Subject: [PATCH] problem38 --- problem38/__init__.py | 0 problem38/problem.py | 58 +++++++++++++++++++++++++++++++++++++++ problem38/test.py | 64 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 problem38/__init__.py create mode 100644 problem38/problem.py create mode 100644 problem38/test.py diff --git a/problem38/__init__.py b/problem38/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problem38/problem.py b/problem38/problem.py new file mode 100644 index 0000000..8bfe551 --- /dev/null +++ b/problem38/problem.py @@ -0,0 +1,58 @@ +import itertools + + +def pandigital(n, multipliers): + for i in multipliers: + if i <= 0: + return None + + return int(''.join([str(n * i) for i in multipliers])) + + +def get_multipliers(n): + multiplier = 1 + multipliers = [] + + while n * multiplier < 1000: + if '0' not in str(multiplier) and \ + len(set(str(multiplier))) == len(str(multiplier)): + multipliers.append(multiplier) + + multiplier += 1 + + return multipliers + + +def is_pandigit_1_9(n): + for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]: + if str(i) not in str(n): + return False + + n_set = set(str(n)) + + return len(n_set) == len(str(n)) + + +def get_all_combinations(items): + res = [] + + for r in range(0, len(items) + 1): + for subset in itertools.combinations(items, r): + res.append(subset) + + return filter(lambda x: len(x) > 0, res) + + +def main(): + res = [] + + for i in reversed(range(1, 1000)): + multipliers = get_multipliers(i) + + for m in get_all_combinations(multipliers): + pandigit = pandigital(i, m) + + if is_pandigit_1_9(pandigit): + res.append(pandigit) + + return max(res) diff --git a/problem38/test.py b/problem38/test.py new file mode 100644 index 0000000..43d190a --- /dev/null +++ b/problem38/test.py @@ -0,0 +1,64 @@ +from problem import ( + main, pandigital, get_multipliers, is_pandigit_1_9, get_all_combinations +) + +res = main() + +def test_is_nine_digits_long(): + assert len(str(res)) == 9 + + +def test_result_does_not_contain_zeros(): + assert '0' not in str(res) + + +def test_result_should_contain_each_number_once(): + for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]: + assert str(i) in str(res) + + +def test_pandigital_192(): + assert pandigital(192, [1, 2, 3]) == 192384576 + + +def test_pandigital_9(): + assert pandigital(9 , [1, 2, 3, 4, 5]) == 918273645 + + +def test_pandigital_should_return_none_if_multiplier_lte_than_0(): + assert pandigital(9 , [0, 1, 2, 3, 4, 5]) == None + assert pandigital(9 , [0, -1, 2, 3, 4, 5]) == None + + +def test_get_multipliers(): + assert get_multipliers(999) == [1] + + +def test_get_multipliers_with_too_hight_number(): + assert get_multipliers(1000) == [] + + +def test_is_pandigit_1_9_with_too_less_characters(): + assert is_pandigit_1_9(111111) == False + + +def test_is_pandigit_1_9_with_zero(): + assert is_pandigit_1_9(102345678) == False + + +def test_is_pandigit_1_9(): + assert is_pandigit_1_9(123456789) == True + + +def test_is_pandigit_1_9_with_double_characters(): + assert is_pandigit_1_9(123456799) == False + + +def test_get_all_combinations_123(): + res = [(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] + assert get_all_combinations([1,2, 3]) == res + + +def test_get_all_combinations_12(): + res = [(1,), (2,), (1, 2)] + assert get_all_combinations([1,2]) == res