blob: 5c1e781f0c3e7ea576a57a2333874dc2de2f5961 (
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
33
34
35
36
37
38
39
|
#!/usr/bin/env python3
# Challenge 113
#
# TASK #1 - Represent Integer
# Submitted by: Mohammad S Anwar
# You are given a positive integer $N and a digit $D.
#
# Write a script to check if $N can be represented as a sum of positive
# integers having $D at least once. If check passes print 1 otherwise 0.
#
# Example
# Input: $N = 25, $D = 7
# Output: 0 as there are 2 numbers between 1 and 25 having the digit 7
# i.e. 7 and 17. If we add up both we don't get 25.
#
# Input: $N = 24, $D = 7
# Output: 1
import sys
import re
from itertools import combinations
def nums_containing(n, d):
nums = list(filter(lambda x: re.search(str(d), str(x)), range(n+1)))
return nums
def represent(n, d):
nums = nums_containing(n, d)
for k in range(1, len(nums)+1):
for combin in combinations(nums, k):
if sum(combin) == n:
return True
return False
if represent(int(sys.argv[1]), int(sys.argv[2])):
print(1)
else:
print(0)
|