MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / digit_replacements

Function digit_replacements

project_euler/problem_051/sol1.py:56–75  ·  view source on GitHub ↗

Returns all the possible families of digit replacements in a number which contains at least one repeating digit >>> digit_replacements(544) [[500, 511, 522, 533, 544, 555, 566, 577, 588, 599]] >>> digit_replacements(3112) [[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3

(number: int)

Source from the content-addressed store, hash-verified

54
55
56def digit_replacements(number: int) -> list[list[int]]:
57 """
58 Returns all the possible families of digit replacements in a number which
59 contains at least one repeating digit
60
61 >>> digit_replacements(544)
62 [[500, 511, 522, 533, 544, 555, 566, 577, 588, 599]]
63
64 >>> digit_replacements(3112)
65 [[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3882, 3992]]
66 """
67 number_str = str(number)
68 replacements = []
69 digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
70
71 for duplicate in Counter(number_str) - Counter(set(number_str)):
72 family = [int(number_str.replace(duplicate, digit)) for digit in digits]
73 replacements.append(family)
74
75 return replacements
76
77
78def solution(family_length: int = 8) -> int:

Callers 1

solutionFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected