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?
- New Content published on w3resource :
- 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