Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added problem38/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions problem38/problem.py
Original file line number Diff line number Diff line change
@@ -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)
64 changes: 64 additions & 0 deletions problem38/test.py
Original file line number Diff line number Diff line change
@@ -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