Tuesday, May 1, 2012

Challenge 064’s Solution

An input of “la-la-la!” will produce an output of “ala-a” in the Terminal window. The original variable str has a value of “la-la-la!” with a length of 9. First, it checks if its length is greater than or equal to 3. Next, the substring method gets the length and divides it by 3, which is 3. Thus, all elements of str that are after element 0 (inclusive) up to the third element are run through the method again via recursion. This second str has a value of “la-” and a length of 3. Running this str through the first substring method creates an str with a length less than 3, so its recursion doesn’t matter. Next, the second str is run through a substring that gets all elements after element 1 (inclusive) up to the second element and prints them on the screen. This produces an output of “a”. After this, str has its first 2 elements removed and runs again through the method; however, this str’s length is less than 3, so nothing happens. The original str, with a length of 9, now ascends to the next line of code. This str is run through a substring that gets all elements after element 3 (inclusive) up to the sixth element and prints them on the screen. This produces an output of “la-”. After this, the original str has its first 6 elements removed and runs again through the method. This third str has a value of “la!” and a length of 3. Like the second str, running this str through the first substring method creates an str with a length less than 3, so its recursion doesn’t matter. The third str is run through a substring method that gets all elements after element 1 (inclusive) up to the second element and prints them on the screen. This produces an output of “a”. Finally, str has its first 2 elements removed and runs again through the method; however, this str’s length is less than 3, so nothing happens. In total, this creates an output of “ala-a”.
 
−Phoenix

No comments:

Post a Comment