0

I am new to python and trying to append characters of a card number to two different arrays. 4003600000000014 every other digit, starting with the number’s second-to-last digit so the first digit is 1(that is left of the 4) and by jumping one number going all the way to the 0. After that, numbers that did NOT appended to the first array (mbt) should be appended to the 2nd array(normal).

mbt should be like = 4 0 6 0 0 0 0 1 normal should be like = 0 3 0 0 0 0 0 4 (two arrays combined will be again equal to 4003600000000014)

import math


def digitnumber(n):
    if n > 0:
        digits = int(math.log10(n)) + 1
        return digits


def isvalid(n, mbt=[], normal=[]):
    cardnumber = 4003600000000014
    dnumber = digitnumber(4003600000000014)
    n = dnumber - 1,
    mbt = []
    while 1 <= n < dnumber:
        x = int(cardnumber / pow(10, n) % 10)
        mbt.append(x)
        n -= 2
        n = dnumber - 2
        normal = []
        while 1 <= n < dnumber:
            x = int(cardnumber / pow(10, n) % 10)
            normal.append(x)
            n -= 2


def main():
    mbt = []
    normal = []
    isvalid(4003600000000014, mbt=[], normal=[])
    print(len(mbt))


main()
IDK
  • 105
  • 6

3 Answers3

2

From what I understand you are trying to slice number to get individual digits. You can find more information on slicing in Python: Understanding slice notation

Here's a solution using python slicing to the problem. The output arrays can be reversed as needed.

def isvalid(n):
    string_num = str(n)
    mbt = [int(x) for x in string_num[1::2]]
    normal = [int(x) for x in string_num[0::2]]
    return mbt, normal


def main():
    mbt, normal = isvalid(378282246310005)
    print(len(mbt))


main()
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
Kassym Dorsel
  • 4,773
  • 1
  • 25
  • 52
  • Thanks a lot, this is a pythonic way of it I guess. – IDK Jun 08 '20 at 13:33
  • Hey me again, `mbt = [int(x) for x in string_num[1::2]]` what does `int(x)` does in here convert all elements to ints ? and the reason why you `str(n)` was slicing is a operation for strings. – IDK Jun 08 '20 at 16:48
  • That is correct on both. Slicing requires an object that supports indexing (`str`, `list`, etc). It would not work directly on an `int` being passed in. If you're ok with the output being a string than you don't need the list comprehension to convert it. – Kassym Dorsel Jun 08 '20 at 17:10
  • Thank you so much for your time it really helped :) @Kasymm Dorsel and @ Gustav Rasmussen – IDK Jun 08 '20 at 18:38
0

You can use this function:

def split(num):
    num = [n for n in str(num)]
    num1 = []
    num2 = []
    for n in range(len(num)//2): # Move all element from num to num1 and num2. Since we are moving elements from 1 list to 2 lists, divide by two to distribute evenly. If the number of elements in num is odd, the // will get get rid of the decimal part
        num1.append(num.pop()) # Removes last element from num and adds it to num1
        num2.append(num.pop()) # Removes last element from num and adds it to num2
    if num: # If there is still an element left (as in, the number of elements in num was odd to begin with):
        num1.append(num.pop()) # Move that element to num1

    return ' '.join(reversed(num1)),' '.join(reversed(num2))

print(split(4003600000000014))

Output:

('0 3 0 0 0 0 0 4', '4 0 6 0 0 0 0 1')
Red
  • 26,798
  • 7
  • 36
  • 58
  • Can you clarify `(len(num)//2)` by pseudocode and the usage of .pop function if you don't mind? Thanks a lot for your time. – IDK Jun 08 '20 at 16:51
  • @IDK Added it to my post :) – Red Jun 08 '20 at 18:19
  • Thanks for the help, python looks like I can everything I imagined with all the dynamic arrays and all, thanks for the help :) – IDK Jun 08 '20 at 18:37
0

Assuming that your input is an integer and you expect 2 lists of integers as output:

x = 4003600000000014
x = list(str(x))
a = list(map(int, x[1::2]))
b = list(map(int, x[0::2]))

print(a)
print(b)

[0, 3, 0, 0, 0, 0, 0, 4]
[4, 0, 6, 0, 0, 0, 0, 1]
alec_djinn
  • 10,104
  • 8
  • 46
  • 71