Python: Find a string consisting of space-separated characters with given counts
Python Programming Puzzles: Exercise-74 with Solution
Write a Python program to find a string consisting of space-separated characters with given counts.
Input: {'f': 1, 'o': 2} Output: f o o Input: {'a': 1, 'b': 1, 'c': 1} Output: a b c
Pictorial Presentation:

Sample Solution:
Python Code:
#License: https://bit.ly/3oLErEI
def test(counts):
return " ".join(c for c, i in counts.items() for _ in range(i))
strs = {"f": 1, "o": 2}
print("Original string:",strs)
print("String consisting of space-separated characters with given counts:")
print(test(strs))
strs = {"a": 1, "b": 1, "c":1}
print("\nOriginal string:",strs)
print("String consisting of space-separated characters with given counts:")
print(test(strs))
Sample Output:
Original string: {'f': 1, 'o': 2} String consisting of space-separated characters with given counts: f o o Original string: {'a': 1, 'b': 1, 'c': 1} String consisting of space-separated characters with given counts: a b c
Flowchart:

Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find a string contains a vowel between two consonants, in a given string.
Next: Reorder numbers in increasing/decreasing order based on whether the first plus last element is even/odd.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join