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()
asked Jun 8, 2020 at 13:13
4
  • Sorry, but I can't quite get your question. What is the expected output? Given 378282246310005 what the two arrays should look like? Commented Jun 8, 2020 at 13:20
  • I edited it. Hope you can understand now. @alec_djinn Commented Jun 8, 2020 at 13:31
  • @IDK I understood before you edited it :) Commented Jun 8, 2020 at 13:38
  • :) thanks for the super short solutions, I guess for a person who came from C, python includes a lot of features. Commented Jun 8, 2020 at 13:40

3 Answers 3

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
4,0394 gold badges32 silver badges56 bronze badges
answered Jun 8, 2020 at 13:30
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, this is a pythonic way of it I guess.
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.
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.
Thank you so much for your time it really helped :) @Kasymm Dorsel and @ Gustav Rasmussen
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]
answered Jun 8, 2020 at 13:44

Comments

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')
answered Jun 8, 2020 at 13:35

3 Comments

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 Added it to my post :)
Thanks for the help, python looks like I can everything I imagined with all the dynamic arrays and all, thanks for the help :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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