Sum of the digits of n is substring of n: Difference between revisions

m
→‎{{header|Python}}: (abstracted out the formatting of the full script version)
m (→‎{{header|Python}}: (abstracted out the formatting of the full script version))
Line 207:
 
from functools import reduce
from itertools import chain
 
 
Line 223 ⟶ 224:
def main():
'''Matches in [0..999]'''
print(
 
limit = 1000 showMatches(
digitSumIsSubString,
xs = list(
filter)(999)
digitSumIsSubString,
(str(n) for n in range(0, limit))
)
)
w = len(xs[-1])
 
 
print(f'{len(xs)} matches < {limit}:\n')
# ----------------------- DISPLAY ------------------------
print(
 
'\n'.join(
# showMatches :: (String -> Bool) -> Int -> String
' '.join(cell.rjust(w, ' ') for cell in row)
def showMatches(p):
for row in chunksOf(10)(xs)
'''A listing of the integer strings [0..limit]
which match the predicate p.
'''
def go(limit):
def triage(n):
s = str(n)
return [s] if p(s) else []
 
xs = list(
chain.from_iterable(
(str(n) for n in map(triage, range(0, 1 + limit))
)
)
w = len(xs[-1])
print( return f'{len(xs)} matches < {limit}:\n') + (
'\n'.join(
' '.join(cell.rjust(w, ' ') for cell in row)
for row in chunksOf(10)(xs)
)
)
 
return go
 
 
9,659

edits