Hackerrank - Print Function Solution

Hackerrank - Print Function Solution

Hackerrank - Print Function Solution

Check Tutorial tab to know how to to solve.

Read an integer .

Without using any string methods, try to print the following:

Note that "" represents the values in between.

Input Format

The first line contains an integer .

Output Format

Output the answer as explained in the task.

Sample Input 0

3

Sample Output 0

123

Solution in python

Solution 1.

n = int(input())
for i in range(1,n+1):
    print(i,end="")


Solution 2.

n = int(input())
print(*range(1,n+1),sep="")

Explanation

Range gives us numbers between 2 given numbers

>>> range(0,4)
[0,1,2,3]
>>> range(2,5)
[2,3,4]
>>> range(3,7)
[3,4,5,6]

The * is used to unpack a iterable(list, string,tuple etc) in python

>>> print(*range(0,4))
0 1 2 3
>>> print(*(2,3))
2 3
>>> print(*[0,5,7])
0 5 7

By default the output is separated by a space " "

To remove the space we can use  sep=""

>>> print(*range(0,4), sep="")
0123
>>> print(*(2,3),sep=",")
2,3
>>> print(*[0,5,7], sep="\n")
0
5
7

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe