Q1: In the language of an alien race, all words take the form of Blurbs. A Blurb
ID: 3814542 • Letter: Q
Question
Q1: In the language of an alien race, all words take the form of Blurbs. A Blurb is a Whoozit followed by between zero and ten Whatzits. A Whoozit is the character 'x' followed by between one and ten 'y's. A Whatzit is a 'q' followed by either a 'z' or a 'd', followed by a Whoozit. Design and implement a recursive program that generates 10 random Blurbs in this alien language. (Hint: start by trying to generate Whoozits, Whatzits, and Blurbs by hand and on paper. Be sure to follow that order. Once you have a feeling for the structure of the language, then start to think about how you might program it.)
Q2: Considering the language just discussed, write a program that reads a string from the user, and checks whether or not is a valid sentence. If it is valid, it prints "The word is fine.", otherwise it prints "The word is a mess!" The program should loop and ask the user for new input until they enter DONE. (Hint: try Whoozits first, followed by Whatzits, and then Blurbs. For each method, test it by running it on known valid strings generated by the methods you had to write for Q1.) Note: if it helps you, you can omit checking for the maximum number of times Whatzits or 'y's occur (e.g., "between one and ten" becomes "one or more").
Explanation / Answer
Okay lets get to pseudcode:
Lets bifercate Blurb:
<Whoozit > <0 or 10 Whatzits>
=><x <1 or 10 y>> < qz/d Whoozit> [ see recursion here]
=> <x <1 or 10 y>> < qz/d x <1 or 10 y>> [generalised example of blurb]
Q1.
static i=1; // for generating 10 random blurbs
generateBlurbs(random y1, random y2, random z_or_d)
{
++x;
if i > 1 : return;
else : x <1 or 10 y>> < qz/d x <1 or 10 y>> [generalised example of blurb]
printLine=> print('x') concat while(y1>0) {print ('y'); --y} concat q concat z_or_d concat while(y1>0) {print ('y'); -- y}
generateBlurbs(new_random y1, new_random y2, new_random z_or_d) // again generate random numbers recursive call
}
Q2.
Simply using thos approach you can decipher the input and parse it.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.