index | submit | rank | book

repeat-list – Repeat-a-list

Write a program that reads lists of integers from standard input and replicates the same integers on standard output.

Repeating a list

Input and 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.

Example Input

4 1 2 3 4
2 32 16
3 12 360 60

Example Output

4 elements: 1 2 3 4
2 elements: 32 16
3 elements: 12 360 60



Here are hints that may help:

  1. 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.

  2. 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.

  3. Partial output lines: Remember you can produce a single line with separate print commands. In C, printf("Hello, World!\n"); is equivalent to

     printf(" World!");

    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.

  4. 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 first: repeat swap

try next: index-ints

index | submit | rank | book

Copyright © 2020-2023 Rudy Matela
All rights reserved