Author: Adabelle Xie
Given a number N and a string S, print out a line that contains N space separated repetitions of S.
Use a loop to repeat a print statement printing S and a space N-1 times. Then print S (with no space).
Since we need N copies of S to be printed, naturally we will want to use a loop. We need the spaces between the copies of S, but not any leading or trailing spaces. Therefore, the last copy of S cannot have a space behind it. If we run the print statement the full N times printing S + “ “, the last S will have a space. Therefore, we want to run the print statement N-1 times, and include a print statement after that only prints S.
You can also include a conditional inside your loop to check when you are printing the last S. Inside the body of the conditional, include the print statement printing only S.
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.