aboutsummaryrefslogtreecommitdiff
path: root/challenge-141/abigail/python/ch-2.py
blob: 61cc8cdd255b2e1435a81c4a96087985fcd72b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/opt/local/bin/python

#
# See ../README.md
#

#
# Run as: python ch-2.py < input-file
#

import fileinput

def substrings (n, m, prefix, max):
    if len (n) == 0:
        if prefix > -1  and prefix < max and prefix % m == 0:
            return 1
        else:
            return 0

    fc   = int (n [0 : 1])
    tail = n [1:]
    if prefix == -1:
        n_prefix = fc
    else:
        n_prefix = 10 * prefix + fc

    return substrings (tail, m, n_prefix, max) + \
           substrings (tail, m,   prefix, max)

for line in fileinput . input ():
    n, m = line . strip () . split (" ")
    print (substrings (n, int (m), -1, int (n)))