ruju11235 / csp

Small programs using the JavaScript syntax. I'm just starting to learn :)
0 stars 1 forks source link

Write a recursive function `print_binary_stings` that prints all the binary strings of a given length (n >= 0) #10

Closed kedarmhaswade closed 3 weeks ago

kedarmhaswade commented 1 month ago

Here is a sample output:

print_binary_strings(3);

// produces
000
001
010
011
100
101
110
111

If n equals zero, it prints an empty string (which only appears like an empty line).

We have discussed a similar function that returns the list (let's call that function get_binary_strings) of all binary strings of length n.

You can utilize that function here:

However, this function, print_binary_strings, does not want us to return such a list. It only wants us to print all the binary strings of length n. This relaxed requirement may result in a more straightforward (but tricky) recursion code for the function.

You'll like the "domino effect" once you attain it.

Can you then modify the code slightly to also print all the binary strings of lengths 0 to n? For example, with this little change, the program should produce the following output:

print_binary_strings(3);

// produces

0
00
000
001
01
010
011
1
10
100
101
11
110
111