0

I'm a few weeks into learning python and I am trying to write a script that takes an input of any length of numbers and splits them in one-character lengths. like this: input:

123456

output:

1           2            3            4            5            6

I need to do this without using strings, and preferably using divmod... something like this:

 s = int(input("enter numbers you want to split:"))
     while s > 0:
         s, remainder = divmod(s, 10)

I'm not sure how to get the spacing right.

Thank you for the help.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142

4 Answers4

2

As your priority is to use divmod, you can do it like this:

lst=[]
while s>0:
    s, remainder = divmod(s, 10)
    lst.append(remainder)

for i in reversed(lst):
    print i,

Output:

enter numbers you want to split:123456
1 2 3 4 5 6

You can use join() to achieve that. Cast to string if your are using python 2.*

s = input("enter numbers you want to split:")
s= str(s)
digitlist=list(s)
print " ".join(digitlist)

In case, you are in need of integers, just do it.

intDigitlist=map(int,digitlist)
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • Two simplifications to the approach that doesn't use `divmod`: (1) you don't need `s = str(s)` -- `input` returns `str`s; (2) rather than setting `digitlist` to that comprehension, just say `digitlist = list(s)`. – BrianO Oct 06 '15 at 14:45
  • In python 2.*, input does return type, you can check that. Thanks for `list()` suggestion, i have edited it. I knew about it, just don't know why i didn't use it. – Ahsanul Haque Oct 06 '15 at 14:47
  • True, but there's no "Python 2" tag on the question so I assumed it means Python -- you know, current Python not legacy ;) Perhaps mention that in your answer. In any case, my 2nd point is still true. – BrianO Oct 06 '15 at 14:49
  • 1
    PS -- In Python 3, to print the numbers all on the same line, use `print(i, ' ', end='')` inside the `for i ...` loop. – BrianO Oct 06 '15 at 14:54
  • I still can't accept print as a function. I love old python ;) – Ahsanul Haque Oct 06 '15 at 14:56
0

Try it with mod:

while x > 0:
   x = input
   y = x % 10
   //add y to list of numbers
   x = (int) x / 10

e.g. if x is 123:

123 % 10 is 3 -> you save 3. the Integer value of 123 / 10 is 12. Then 12 % 10 is 2 -> you save 2 Int of 12 / 10 is 1. 1 % 10 = 1 -> you save 1

Now you have all numbers. You can invert the list after that to have it like you want.

  • So isches @hiroprotagonist – M.Zuberbühler Oct 06 '15 at 13:54
  • Welcome to Stack Overflow! While this answer may be correct, please add some explanation. Imparting the underlying logic is more important than just giving the code, because it helps the OP and other readers fix this and similar issues themselves. – CodeMouse92 Oct 06 '15 at 14:00
0

What about the following using the remainder:

s = 123456
output = []
while s > 0:
    s, r = divmod(s, 10)
    output.append(r)

fmt='{:<12d}'*len(output)
print fmt.format(*output[::-1])

Output:

1           2           3           4           5           6

This also uses some other useful Python stuff: the list of digits can be reversed (output[::-1]) and formatted into 12-character fields, with the digit aligned on the left ({:<12d}).

xnx
  • 24,509
  • 11
  • 70
  • 109
0

You can iterate over Python string amd use String.join() to get result:

>>>'  '.join(str(input("Enter numbers you want to split: ")))
Enter numbers you want to split: 12345
1  2  3  4  5  
Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52