Repeat a Line N Times
Python
Easy
6 views
Problem Description
Read n and a text line a, output the same line a exactly n times (each on new line).
Input Format
Line1 n. Line2 a (can have spaces).
Sample Test Case
Output:
hello world
hello world
hello world
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines:
sys.exit(0)
try:
n=int(lines[0].strip())
except Exception:
n=0
s=lines[1] if len(lines)>1 else ''
out=[]
for _ in range(n):
out.append(s)
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!