-2

I have a homework from my intro to programming class where they ask me to separate an int value into its different numbers so i can then multiply them.

For example, the number is 860007386.

The verification number is calculated by doing the steps below:

Multiply each digit by the numbers 41, 37, 29, 23, 19, 17, 13, 7 and 3 in that order. For example, In this case it should go like this: 8x41, 6x37, 0x29, 0x23, 0x19, 7x17, 3x13, 8x7, 6x3.

then add the product of all the operations above.

calculate the residue that you get from dividing the sum by 11.

If the residue is 1 or 0 then then that is the verification code.

If not then the verification code is given by substracting the residue of 11.

L G
  • 11
  • 1
  • 1
    Does this answer your question? [How to split an integer into an array of digits?](https://stackoverflow.com/questions/1906717/how-to-split-an-integer-into-an-array-of-digits) – wjandrea Mar 09 '21 at 01:12
  • 1
    https://idownvotedbecau.se/noattempt/ – Cireo Mar 09 '21 at 01:12
  • Thats the problem, i dont even know where to begin, thats why i ask for help i dont want a solution i just want a tip or some insight – L G Mar 09 '21 at 01:14
  • 1
    Welcome to Stack Overflow! Check out the [tour] and [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/4518341) as well as [ask]. In the future, please put some effort into solving the problem yourself. Start by researching. I googled "python split number in digits" to find an existing question that covers this. – wjandrea Mar 09 '21 at 01:14
  • can i multiply two arrays? like if a turn the number into an array and have a different array for the numbers that multiply in a different array can i multiply them? – L G Mar 09 '21 at 01:17

2 Answers2

0

We represent numbers with collections of symbols. That means that the number 860007386 can also be understood as the string "860007386". For a human, the conversion is immediate, but a computer needs it to be explicitly transformed.

Essentially, what you need to do is as follows:

  1. Convert your initial number into a string
  2. Split it into the different digits
  3. Convert each individual digit back into a number

You'll want to research the .split() string method in Python, along with the str() and int() functions.

Peritract
  • 761
  • 5
  • 13
0

I think this question is more complex than it seems because it has many different parts!

We'll need first to use a List Comprehension and str() since a .split() won't help us make 860007386 into a separate list of numbers since we don't have an identifier between the numbers to use inside .split(). If we had spaces between 860007386 we could have used .split(' '), or if we had numbers separated by - we could have used .split('-') as the identifier to split.

Therefore, you'll need a List Comprehension to iterate the index position of each number in the string to split them, like this:

Create two variables.

[1] largenum = 860007386 
[2] nummul = [41, 37, 29, 23, 19, 17, 13, 7 , 3]

First, convert largenum into a string and assign it to a variable.

[3] str_largenum = str(largenum)

Now, you'll see, as I mentioned before .split will not separate the numbers in the string variable, instead it will give you only one string:

In[4] str_largenum.split()
Out[5] ['860007386']

To solve this problem, we'll use a List Comprehension, going from a range 0 to the length of largenum as a string, so from 0 to 9. For each index i in the range, which is the same number of numbers in largenum, you'll create a list of strings by selecting the index location of str_largenum from i to i+1. The i:i+1 is the indexing slicing that separates each string (each number) for us.

In[6] print([str_largenum[i:i+1] for i in range(0,len(str_largenum),1)])
      ['8', '6', '0', '0', '0', '7', '3', '8', '6']

I create a variable called split_str_largenum to assigned the List Comprehension to create the separate string of numbers.

[7] split_str_largenum = [str_largenum[i:i+1] for i in range(0,len(str_largenum),1)]

Now, you'll have to convert the List of strings back into integers with a List Comprehension.

[8] [int(numstring) for numstring in split_str_largenum]

And now assign this to a new variable called integer_split_largenum

[9] integer_split_largenum = [int(numstring) for numstring in split_str_largenum]

[10] print(integer_split_largenum)
     [8, 6, 0, 0, 0, 7, 3, 8, 6]

So now we have to Lists, which in Python can be thought as List arrays.

[8, 6, 0, 0, 0, 7, 3, 8, 6]

[41, 37, 29, 23, 19, 17, 13, 7 , 3]

Now we need to use Linear Algebra, and take both of these arrays and multiple them.

For this, we'll need to import numpy package, and do a dot product multiplication to multiple each number in both arrays and add them. First, we will convert both variables with the list arrays into numpy arrays with np.array(); then use the np.dot() function.

[11] import numpy as np
    a = np.array(integer_split_largenum)
    b = np.array(nummul)
    dotproduct = np.dot(a,b)

which gives you 782

[12] residual = dotproduct / 11

[13] print(residual)
     71.0909090909091

[14] verification_code = residual - 11
     print(verification_code)
     60.09090909090909

I think this what you are looking for. Let me know if you have any questions.