Jump to content

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

m
Moved Python entry into correct alphabetical order.
(Added Wren)
m (Moved Python entry into correct alphabetical order.)
Line 59:
912 913 914 915 916 917 918 919
</pre>
 
=={{header|Python}}==
Just using the command line:
 
<lang python>Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> x = [n for n in range(1000) if str(sum(int(d) for d in str(n))) in str(n)]
>>> len(x)
48
>>> for i in range(0, len(x), (stride:= 10)): print(str(x[i:i+stride])[1:-1])
 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
109, 119, 129, 139, 149, 159, 169, 179, 189, 199
200, 300, 400, 500, 600, 700, 800, 900, 910, 911
912, 913, 914, 915, 916, 917, 918, 919
>>> </lang>
 
 
or as a full script, taking an alternative route, and slightly reducing the number of str conversions required:
 
<lang python>'''Sum of the digits of n is substring of n'''
 
from functools import reduce
 
 
# digitSumIsSubString :: String -> Bool
def digitSumIsSubString(s):
'''True if the sum of the decimal digits in s
matches any contiguous substring of s.
'''
return str(
reduce(lambda a, c: a + int(c), s, 0)
) in s
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Matches in [1..999]'''
 
xs = list(
filter(
digitSumIsSubString,
(str(n) for n in range(1, 1000))
)
)
w = len(xs[-1])
 
print(f'{len(xs)} matches < 1000:\n')
print(
'\n'.join(
' '.join(cell.rjust(w, ' ') for cell in row)
for row in chunksOf(10)(xs)
)
)
 
 
# ----------------------- GENERIC ------------------------
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()
</lang>
{{Out}}
<pre>47 matches < 1000:
 
1 2 3 4 5 6 7 8 9 10
20 30 40 50 60 70 80 90 100 109
119 129 139 149 159 169 179 189 199 200
300 400 500 600 700 800 900 910 911 912
913 914 915 916 917 918 919</pre>
 
=={{header|Raku}}==
Line 155 ⟶ 240:
done...
</pre>
 
=={{header|Python}}==
Just using the command line:
 
<lang python>Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> x = [n for n in range(1000) if str(sum(int(d) for d in str(n))) in str(n)]
>>> len(x)
48
>>> for i in range(0, len(x), (stride:= 10)): print(str(x[i:i+stride])[1:-1])
 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
109, 119, 129, 139, 149, 159, 169, 179, 189, 199
200, 300, 400, 500, 600, 700, 800, 900, 910, 911
912, 913, 914, 915, 916, 917, 918, 919
>>> </lang>
 
 
or as a full script, taking an alternative route, and slightly reducing the number of str conversions required:
 
<lang python>'''Sum of the digits of n is substring of n'''
 
from functools import reduce
 
 
# digitSumIsSubString :: String -> Bool
def digitSumIsSubString(s):
'''True if the sum of the decimal digits in s
matches any contiguous substring of s.
'''
return str(
reduce(lambda a, c: a + int(c), s, 0)
) in s
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Matches in [1..999]'''
 
xs = list(
filter(
digitSumIsSubString,
(str(n) for n in range(1, 1000))
)
)
w = len(xs[-1])
 
print(f'{len(xs)} matches < 1000:\n')
print(
'\n'.join(
' '.join(cell.rjust(w, ' ') for cell in row)
for row in chunksOf(10)(xs)
)
)
 
 
# ----------------------- GENERIC ------------------------
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()
</lang>
{{Out}}
<pre>47 matches < 1000:
 
1 2 3 4 5 6 7 8 9 10
20 30 40 50 60 70 80 90 100 109
119 129 139 149 159 169 179 189 199 200
300 400 500 600 700 800 900 910 911 912
913 914 915 916 917 918 919</pre>
 
=={{header|Wren}}==
9,492

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.