2

I'm trying to split an arbitrarily long user input integer into a list where each entry is 2 digits, and if the number has an odd amount of integers, put the only single digit as the first digit. (Where I will then proceed to put a zero in front of it)

I know that putting user integer input into a list looks like:

userintegerlist = [int(i) for i in str(user_input)]
print userintegerlist

And my input (say it's 45346) will look like [4,5,3,4,6]. But I want it to look like: [4,53,46]. Or if input is 68482238, it will be: [68,48,22,38].

Is this possible? All the code is in Python by the way.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
AndyyRi
  • 23
  • 1
  • 3
  • 1
    if you know how to use a list comprehension, you must know how to use a regular `for` loop. iterate over `range(len(user_input))[::2]` and take the elements `i` and `i + 1`. i don't know that this was worth a post here. – abcd Oct 01 '15 at 07:46

7 Answers7

4

You can do it with string methods fairly easily, as other answers have already shown. I direct you to the related grouper recipe in itertools.

I want to mention that it may be more efficient to do it with maths:

>>> n = 45346
>>> output = []
>>> while n:
...     output.append(n % 100)
...     n //= 100
...     
>>> output = output[::-1]
>>> print output
[4, 53, 46]
wim
  • 338,267
  • 99
  • 616
  • 750
  • 1
    Oneline version of this solution: [n % 100**(i+1) // 100**i for i in range(0, math.ceil(math.ceil(math.log10(n))/2))][::-1] – iimos Oct 01 '15 at 08:42
0
s = raw_input()
if(len(s)%2 ==1):
    s = '0'+s
list = []
for i in range(len(s)/2):
    list.append(int(s[2*i:2*i+2]))
print list

After taking the input string, check if its of odd length, and if yes, prefix a 0 to it. Then , iterate over the string , taking two characters at a time , cast that substring to int , and append it in a list. It's that simple.

0
>>> split = lambda s: (len(s)&1 and [s[0]] or []) + [s[i-1]+s[i] for i in range(len(s)-1,0,-2)][::-1]
>>> split("45346")
['4', '53', '46']
>>> split("68482238")
['68', '48', '22', '38']
iimos
  • 4,767
  • 2
  • 33
  • 35
0

Given an input such as '12345' what you can do is turn that into a list and then iterate over it in reverse trying to pop two elements at a time and then inserting those at the beginning of the list as integers (after joining them together).

If you fail to pop a second element, then you just prefix the popped element with a 0 and also insert that at the beginning of the output list.

Here's a very dirty solution that attempts this:

def foo(user_input):
    transformed_input = list(user_input)
    out = []
    while transformed_input:
        a = transformed_input.pop()
        try:
            b = transformed_input.pop()
            a = b + a
        except IndexError:
            a = '0' + a 
        out.insert(0, a)
    return out

For an input such as '345' you get ['03', '45']

And then for '3456' you get ['34', '56']

Is this what you wanted?

Nobilis
  • 7,310
  • 1
  • 33
  • 67
0

The strategy in my code is to create a mirror list and pair up the elements between the two lists

 user_input = '123192873918271'
    split_input = list(user_input)

#EXAMINE WHETHER THE LIST HAS AND ODD OR EVENT NUMBER OF ENTRIES
if len(user_input)%2==1 :

    #create a mirror list that we will pair the original one
    mirror_list = ['0']+ split_input 

    #pair up the elements in both lists
    final_list = [i+j for i,j in zip(mirror_list, split_input)]
elif len(user_input)%2==0:

    #in this case, we pair up the list with itself, with a 1 element shift
    final_list = [i+j for i,j in zip(split_input[:-1], split_input[1:])]

for i in final_list:
 print i

    01
    12
    23
    31
    19
    92
    28
    87
    73
    39
    91
    18
    82
    27
    71
elelias
  • 4,552
  • 5
  • 30
  • 45
0

You can group the items using the same iterator on the pair, without using any indexing on the string:

user_input = '45346'
user_input = '0'+user_input if len(user_input)%2 else user_input
gi = [iter(user_input)]*2
r = [''.join(z) for z in zip(*gi)]
print(r)

produces

['04', '53', '46']
Pynchia
  • 10,996
  • 5
  • 34
  • 43
0

Code:

import math
n = 2384920394837493
print([(n//(100**i))%100 for i in range(math.ceil(math.log(n, 100)), -1, -1)][bool(math.log(n,100)%1):]

Outputs:

[23, 84, 92, 3, 94, 83, 74, 93]
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
MDR
  • 2,610
  • 1
  • 8
  • 18