Python: Reads n digits chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number(s)
Python Basic - 1: Exercise-48 with Solution
Write a Python program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination.
Two integers as number of combinations and their sum by a single space in a line. Input 0 0 to exit.
import itertools print("Input number of combinations and sum, input 0 0 to exit:") while True: x, y = map(int, input(). split()) if x == 0 and y == 0: break s = list(itertools.combinations(range(10), x)) ctr = 0 for i in s: if sum(i) == y: ctr += 1 print(ctr)
Input number of combinations and sum, input 0 0 to exit: 5 6 2 4 0 0 2
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program which reads a text (only alphabetical characters and spaces.) and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters.
Next: Write a Python program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
What is the difference between Python's list methods append and extend?
append: Appends object at the end.
x = [1, 2, 3] x.append([4, 5]) print (x)
[1, 2, 3, [4, 5]]
extend: Extends list by appending elements from the iterable.
x = [1, 2, 3] x.extend([4, 5]) print (x)
[1, 2, 3, 4, 5]
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework