Thursday, July 21, 2016

Print Function - Hacker Rank Solution

Print Function - Hacker Rank Solution

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
3
Sample Output
123
Tip 
You can use the print function inside a map(). Can you write a  line code to solve the problem above?

Print Function - Hacker Rank Solution


Problem Setter's code :
Python 2
from __future__ import print_function
print(*range(1, input() + 1), sep="")  
Python 3
print(*range(1, int(input()) + 1), sep="")  
 Tested by DOSHI
Problem Tester's code :
Python 2
from __future__ import print_function
map(lambda x: print(x,end=''), range(1,input()+1))
Python 3
map(lambda x: print(x,end=''), range(1,input()+1))

45 comments:

  1. print ''.join(map(str,range(1,int(raw_input()+1))))

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

      Delete
    2. not working showing compilation error

      Delete
    3. if __name__ == '__main__':
      n = int(input())
      for i in range(1,n+1):
      print(i, end = "")

      Delete
    4. Anyone looking for solution with explanation on print function?

      here you go Hackerrank - Print Function Solution

      Delete
  2. a=input()
    x=""
    for i in range(a,0,-1):
    x=str(i)+x
    print(x)

    ReplyDelete
  3. for i in range(n+1):
    if i==0:
    continue
    print(i,end="")

    would also work successfully

    ReplyDelete
    Replies
    1. can you tell me, how does your range and print works in this..!!

      Delete
    2. https://www.blogger.com/profile/02831904704265034785
      can you tell me what is the use of end??

      Delete
    3. n = int(input())
      s=0
      for i in range(n):
      s=s+1
      print(s,end="")
      simple af:x

      Delete
    4. By default there is a newline character appended to the item being printed (end='\n'), and end='' is used to make it printed on the same line.

      Delete
  4. if __name__ == '__main__':
    n = int(input())
    for i in range(n+1):
    if(i!=0):
    print(i,end="")

    correct answer

    ReplyDelete
    Replies
    1. tell how n+1 is range im fully confused and where i comes from and print line how😥😥😥

      Delete
    2. here in print line end means

      Delete
    3. By default there is a newline character appended to the item being printed (end='\n'), and end='' is used to make it printed on the same line.

      Delete
  5. from __future__ import print_function
    n=int(raw_input())
    for i in range(1,n+1):
    print(i,end="")

    ReplyDelete
  6. import numpy as np
    #enter number you want on below line prompt
    n = int(raw_input())
    arr = []
    for i in range(1, n+1):
    arr.append(i)

    c = pow(10, (len(arr)-1))

    arr2 = []
    while(c >= 1):
    arr2.append(c)
    c = c/10


    a = np.array(arr)
    b = np.array(arr2)

    c = a*b

    print sum(c)




    ReplyDelete
  7. Can anyone tell me why this doesn't work ?
    ```
    n = int(input())
    i=1
    a=1
    while(i<n):
    a=a*10
    i=i+1
    a=a+i
    print(a)
    ```

    ReplyDelete
    Replies
    1. Because you take two variables one variable value enter from user and one you already declared and also initialised so that is why your program not run

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

    ReplyDelete
  9. Hi, I put:

    def nums(N):
    for i in range(1, N+1):
    print(i, end="")
    nums(3)

    and the program returns me:
    2/3 test cases failed :(

    What do I do wrong?

    ReplyDelete
    Replies
    1. change your for loop as
      for i in range(N+1):
      if i==0:
      continue;
      print(i,end="");

      Delete
  10. for i in range(n):
    i+1

    print("{0}{1}{2}".format(i-1,i,i+1));

    this is for pyton-3

    ReplyDelete
  11. #simplicity is the smart policy#
    try it...

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

    ReplyDelete
  12. n=int(input())
    for i in range(1, n+1)
    print(i, end="")
    This worked for me

    ReplyDelete
  13. if __name__ == '__main__':
    n = int(input())
    for i in range (1,n+1) :
    print (i,end="")

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

    ReplyDelete
  15. n=int(input())
    for num in range(1,n+1):
    print(f"{num}",end="")
    # it will show red highlighted code but when you will run it, it will definitely work

    ReplyDelete
  16. 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

    ReplyDelete
  17. if __name__ == '__main__':
    n = int(input())

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

    ReplyDelete
  18. if __name__ == '__main__':
    n = int(input())
    z =41*n
    print(str(z))

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

    ReplyDelete
  20. print(''.join((map(str, ((range(1, int(input())+1)))))))
    # Python 3 code - much efficient

    ReplyDelete
  21. n = int(input())
    x = 1
    y = 0
    while x <= n:
    y = y*10 + x
    x=x+1
    print (y)

    ReplyDelete
  22. if __name__ == '__main__':
    n = int(input())
    lst = []
    for i in range(1,n+1):
    lst.append(i)
    print("".join(map(str,lst)))

    ReplyDelete
  23. from __future__ import print_function

    if __name__ == '__main__':
    n = int(raw_input())

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

    ReplyDelete
  24. def is_leap(year):
    leap = False

    # Write your logic here
    if (year%4)==0:
    if(year%100)==0:
    if(year%400)==0:
    leap=True
    else :
    leap= False
    else :
    leap = True
    else:
    leap= False
    return leap

    year = int(input())
    print(is_leap(year))

    ReplyDelete
  25. if __name__ == '__main__':
    n = int(input())
    s=""
    for i in range(0,n):
    i=i+1
    s=s+str(i)
    print(s)

    ReplyDelete
  26. n = int(input())
    x=""
    for i in range (1,n+1):
    x=x+str(i)
    print(x)

    ReplyDelete

Powered by Blogger.