1

I want to read an integer, and without using any string methods, I want to print something like this using the map() function:

123..N  

For Example:

N:5  
output:12345  

And not:

1  
2  
3  
4  
5

I have already read the following answer, which is not what i want. i want to use map() function which is not used in the answer given below

Print in one line dynamically

Community
  • 1
  • 1
Tony Mathew
  • 191
  • 2
  • 12
  • @vaultah I disagree with the dup closure, but, this isn't a pearl of a question either, a custom close reason might suffice? – Dimitris Fasarakis Hilliard Sep 01 '16 at 15:06
  • @vaultah: Why did you close this question as duplicate? This is a genuine question! – Tony Mathew Sep 01 '16 at 15:10
  • 1
    It isn't genuine, it a badly formatted question with no attempt by your part and no justification on why exactly you might need such quirky behavior. It probably isn't a dupe (unless I missed one answer) but it also isn't the best you could do. – Dimitris Fasarakis Hilliard Sep 01 '16 at 15:12
  • @Jim you have the privilege to reopen questions. – vaultah Sep 01 '16 at 15:13
  • @vaultah yup, but I don't like doing that without first consulting the one who closed it :-) – Dimitris Fasarakis Hilliard Sep 01 '16 at 15:14
  • @TonyMathew sorry for closing your unclear question as dupe of a question with several potential solutions. – vaultah Sep 01 '16 at 15:27
  • @TonyMathew qua functionality, the question is very easy to answer (and answered in the link you mention). Why do you want to do it with `map()`, and why no string methods? – Jacob Vlijm Sep 01 '16 at 15:32
  • Why not use list comprehension and `join` or if you must use map, `''.join(map(str, range(1, N+1)))` which is almost identical to a comment made on xortion's answer to the linked question. – Nick is tired Sep 01 '16 at 17:19
  • I came up with two solutions: ','.join([str(x) for x in xrange(5)]) and ','.join(map(str, xrange(5)). I assume that the comprehension list is the fastest and the most pythonic solution. However I'm wondering if there is something more efficient – Monica Sep 01 '16 at 23:29

5 Answers5

2
n = int(input())

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

You Welcome :D

eMeM
  • 41
  • 4
1

Here is the answer:

from sys import stdin
N = int(stdin.read())

print ''.join(map(str,range(1,N+1)))
SESN
  • 1,275
  • 13
  • 22
1

the exact solution for this is

import math
def get_my_number(num):
    my_num = 0
    for i in range(1, num+1):
        digits = int(math.log10(i))+1  # count number of digits in number
        my_num = my_num * (10 ** digits) + i
    return my_num 
print (get_my_number(n))
kumar
  • 21
  • 3
1

if you are using python 2.7, just use ; at end of print statement if you want to print in single line.

Example:

for a in range(0,4):
  print '*',

it will produce the following output,

 * * * *

or if if you are using python 3.7, just use end=' ' in print statement as second argument.

Example:

for a in range(0,4):
      print('*', end=' ')

it will produce the following output,

 * * * *
-1

you can try this on python 2:

from future import print_function

map(lambda y:print (y,end=""),[x for x in range(1,int(input())+1)])

Rakesh
  • 1
  • 1