Write a program that reads lists of integers from standard input and replicates the same integers on standard output.
Each line of input begins with a number L, indicating the number of integers to be repeated, followed by L numbers all separated by a single space.
For each line of input there should be a line of output
with the number of integers
followed by elements:
followed by the numbers given in the input.
Output values should not contain leading zeroes
and should be separated by a single space.
Output lines should not have trailing spaces.
Each list has at least one element and at most a hundred elements, i.e., 1 ≤ L ≤ 100.
4 1 2 3 4
2 32 16
3 12 360 60
4 elements: 1 2 3 4
2 elements: 32 16
3 elements: 12 360 60
Here are hints that may help:
Beware of trailing spaces in the output:
If your program produce trailing spaces, it will get a score of 1/6.
You can check if your output has trailing spaces
by redirecting it to a file with > file.txt
on the command line
then opening the file on a plain text editor.
To avoid trailing spaces try to think of
preceding numbers with a space
instead of
following numbers by a space.
Beware of leading zeroes in the input: Input may contain leading zeroes. A simple way to get rid of them is to convert input to integers even if you have to immediately convert them back to a string.
Partial output lines:
Remember you can produce a single line with separate print commands.
In C, printf("Hello, World!\n");
is equivalent to
printf("Hello,");
printf(" World!");
printf("\n");
In Python, print('Hello, World!\n')
is equivalent to
print('Hello,', end='')
print(' World!', end='')
print() # <--- produces a line break
The same goes for other languages. This works from within loop iterations. You can use this to your advantage when solving this exercise.
CScx book: You can find a detailed explanation for how to solve this exercise on section 4.8. Sequences of the Computer Science by Example book.
try next: index-ints
Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/repeat-list