Reputation:
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()
Upvotes: 0
Views: 101
Reputation: 27547
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')
Upvotes: 0
Reputation: 10789
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]
Upvotes: 0
Reputation: 4843
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()
Upvotes: 2