In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of December 14th
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #09 exercise sheet]
In this problem you should submit a function as described. Inside the function do not print anything that was not asked!
(in this class you are expected to write a solution that uses recursion - mooshak will not force it, but that should be your goal)
Bender finds himself in a peculiar situation. Due to a temporary glitch in his processing core, he can now only communicate in binary. But there's a catch - his circuits can handle only a limited number of 1's in a message at a time. To avoid overheating, Bender needs your help to generate all possible binary strings of lengths ranging from a to b, inclusive, while ensuring that no string has more than k 1's.
He refuses to write this program himself because he's busy bending things and drinking oil, so it’s up to you to save the day! Help Bender generate his new vocabulary of binary strings.
Write a function binary(a, b, k) that returns a list containing all possible binary strings of lengths between a and b, inclusive, with at most k 1's. Each string should consist only of the characters '0' and '1'.
The following limits are guaranteed in all the test cases that will be given to your program:
1 ≤ a ≤ b ≤ 10 | range to consider | |
0 ≤ k ≤ 10 | maximum number of 1's |
Example Function Calls | Example Output |
output = binary(1, 2, 3) print(sorted(output)) print() output = binary(2, 4, 2) print("output is of type", type(output)) print("output has size:", len(output)) for x in sorted(output): print(x) |
['0', '00', '01', '1', '10', '11'] output is of type <class 'list'> output has size: 22 00 000 0000 0001 001 0010 0011 01 010 0100 0101 011 0110 10 100 1000 1001 101 1010 11 110 1100 |