Problem
Link to the original HackerRank problem
The included code stub will read an integer,
n
, from STDIN.Without using any string methods, try to print the following:
123...n
Note that “
...
” represents the consecuvitve values in between.Example
n = 5
Print the string
12345
Input Format
The first line contains an integer
n
.Constraints
1 <= n <= 150
Output Formats
1 <= n <= 150
Input Format
Print the list of integers from
1
throughn
as a string, without spaces.Sample Input 0
3
Sample Output 0
123
Code
Starter
if __name__ == '__main__':
n = int(input())
Solution
Straightforward
Using a simple for
loop, iterating for each number between 1
and n
(inclusive), printing each number setting the print
function end
parameter to an empty string ""
and a last one for the sake of printing a line return:
if __name__ == '__main__':
n = int(input())
for x in range(1, n + 1, 1):
print(x, end="")
print()
One-liner
Using with a for
loop in a generator expression (
)
and the iterable unpacking operator *
can help to make the initial implementation more concise:
if __name__ == '__main__':
n = int(input())
print(*(x for x in range(1, n + 1, 1)), sep="")