Problem
Link to the original HackerRank problem
Task
The provided code stub reads an integer,
n
, from STDIN.For all non-negative integers
i < n
, printi^2
.Example
n = 3
The list of non-negative integers that are less than
n = 3
is[0, 1, 2]
.Print the square of each number on a separate line.
0 1 4
Input Format
The first and only line contains the integer,
n
.Constraints
1 <= n <= 20
Output Format
n
lines, one corresponding to eachi
.Sample Input 0
5
Sample Output 0
0 1 4 9 16
Code
Starter
if __name__ == '__main__':
n = int(input())
Solution
Straightforward
Using a regular for
loop:
if __name__ == '__main__':
n = int(input())
for x in range(0, n, 1):
print(x ** 2)
One-liner
Using a for
loop in a generator expression (
)
we can have something terser:
if __name__ == '__main__':
n = int(input())
print(*(x ** 2 for x in range(0, n, 1)), sep = "\n")