3

I am fairly new to programming and have been learning some of the material through HackerRank. However, there is this one objective or challenge that I am currently stuck on. I've tried several things but still cannot figure out what exactly I am doing wrong.

Objective: Read N and output the numbers between 0 and N without any white spaces or using a string method.

N = int(input())
listofnum = []

for i in range(1, N +1):
    listofnum.append(i)
print (*(listofnum))

Output :

1 2 3
4
  • 2
    why don't you print numbers inside loop. You don't need to create array for that. Just use print(i, end="") Commented Nov 11, 2016 at 17:59
  • @EvgenyKuzmovich: Thank you so much! I am not familiar with the end='' but will definitely read up on that now. Commented Nov 11, 2016 at 18:05
  • @EvgenyKuzmovich: Could you add your answer as a comment so that I can mark it and others could reference it later? Commented Nov 11, 2016 at 18:28
  • Sure. Already done Commented Nov 11, 2016 at 18:38

7 Answers 7

2
N = int(input())
answer = ''
for i in range(1, N + 1):
    answer += str(i)
print(answer)

This is the closest I can think of to 'not using any string methods', although technically it is using str.__new__/__init__/__add__ in the background or some equivalent. I certainly think it fits the requirements of the question better than using ''.join.

Sign up to request clarification or add additional context in comments.

1 Comment

Why not use repr instead of str? str looks too much like "using a string method" to me.
2

Without using any string method, just using integer division and list to reverse the digits, print them using sys.stdout.write:

import sys
N = int(input())

for i in range(1,N+1):
    l=[]
    while(i):
        l.append(i%10)
        i //= 10
    for c in reversed(l):
        sys.stdout.write(chr(c+48))

Or as tdelaney suggested, an even more hard-code method:

import os,sys,struct

N = int(input())

for i in range(1,N+1):
    l=[]
    while(i):
        l.append(i%10)
        i //= 10
    for c in reversed(l):
        os.write(sys.stdout.fileno(), struct.pack('b', c+48))

All of this is great fun, but the best way, though, would be with a one-liner with a generator comprehension to do that, using str.join() and str construction:

"".join(str(x) for x in range(1,N+1))

Each number is converted into string, and the join operator just concatenates all the digits with empty separator.

9 Comments

"".join is a builtin string method. Chris and Alex just pointed out to me. Its not a operator.
I edited my answer. You won't see the str word in it.
Nice alternate solution, but I was thinking something more like this would be eaiser: print(*[el for el in range(0, int(input())+1)], sep="")
Great answer! But does chr still count as a string method? It calls str.__init__. struct.pack('b', c+48) may be called for here.
@tdelaney: you have to convert to char at some point. There's no distinction between char and string in python unlike C. The solution does the "heavy lifting" just like if you don't have access to integer to string conversion.
|
1

You can print numbers inside the loop. Just use end keyword in print:

print(i, end="")

2 Comments

@Jean-FrançoisFabre obviously you can't print anything without using strings somehow. I think the task says that you can't use string methods and I didn't.
I think those challenges are kind of funny but stupid. The most efficient way is just with join. and see my answer (with the help of tdelaney): no strings attached :)
0

Try ''.join([str(i) for i in range(N)])

1 Comment

"".join is a builtin string method. Chris and Alex just pointed out to me.
0

One way to accomplish this is to append the numbers to a blank string.

out = ''
for i in range(N):
    out += str(i)
print(out)

Comments

0

You can make use of print()'s sep argument to "bind" each number together from a list comprehension:

>>> print(*[el for el in range(0, int(input())+1)], sep="")
10
012345678910
>>>

2 Comments

join is a string method, no?
@leaf not sure what you mean, if you inspect it you get <built-in method join of str object at 0x10df76ab0>.
0

You have to do a simple math to do this. What they expect to do is multiply each of your list elements by powers of ten and add them up on each other. As an example let's say you have an array; a = [2,3,5] and you need to output; 235

Then you multiply each of loop elements starting from right to left by 10^0, 10^1 and 10^2. You this code after you make the string list.

a = map(int,a)
for i in range(len(a)):
    sum += (10**i)*a[-i]
print sum

You are done!

2 Comments

your code doesn't run as-is. Please provide a working snippet.
Two things: first, there are much more efficient ways (e.g. total = 0; for i in range(n):; total = total * 10 + i). Second, we weren't given a limit on what N could be. This idea works for digits, but what if we need to output the numbers 0 to 11? The algorithm won't generate 01234567891011.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.