This is the synopsis of the second Linux programming lab.
The assignment for the second lab
The second assignment is to write a program that prints words which are combinations of all characters of an alphabet. The combinations should have certain length. For example, if length of the words is 3 and the alphabet is “abc” then all possible combinations will be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Naive solution
Here is the easiest solution to print a list like this one above:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
However this solution has a significant limitation. We can’t get words of arbitrary length. Recursive bruteforce can overcome this limitation.
Recursive bruteforce
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Invocation of go from the main function looks like this:
1
|
|
The flowchart of the go function is presented below.
Recursive brute force can be regarded as a process of spawning a copy of the same function and entering it again with storing the current position.
The following flowchart depicts control flow of a program that prints all words of length 4. Green lines mean forward pass. Blue lines mean backward pass when the function returns to the place of its invocation. Black lines stand for inactive paths.
Press right mouse button on the image and choose “View image” to see full size image.