Algorithm for calculating verifier digit

Asked

Viewed 2,467 times

1

Create an algorithm to request a five digit code (COD), and generate the digit (DIGV) checker module 7 for the said code.

Assuming that the five digits of the code are ABCDE, one way to calculate the desired digit, with module 7 is:

S = (6xA) + (5xB) + (4xC) + (3xD) + (2xE)
DIGV = resto da divisão S por 7

Use quotient operators and rest

I thought about developing this exercise by transforming what the user will type into a string, taking each index of that string and storing it into a variable,and doing the math, what do you think? Could someone explain me better?

  • It can be in any language?

  • Yes @rray, no problem!

4 answers

2

Python

In Python, you can do it as follows:

code = "55480"

def get_divg (code):

    code = map(int, code)
    weights = [6, 5, 4, 3, 2]

    return sum(w * c for w, c in zip(weights, code)) % 7

print(get_divg(code))

See working on Ideone.

We first convert the string to a list of integers using map; we make the internal product between the code and its respective weights through the list comprehensions; add all values through the function sum and finally get the rest of split by 7 with the operator %.

2

whereas

S = (6xA) + (5xB) + (4xC) + (3xD) + (2xE)
DIGV = resto da divisão S por 7

Would be equivalent to

S = (6 * A) + (5 * B) + (4 * C) + (3 * D) + (2 * E)
DIGV = S % 7;

We can already implement in several languages, remembering that A, B, C, D and E are COD digits. Implementing in Javascript:

COD_NUM = 55480;
COD = COD_NUM+"";

A = parseInt(COD.charAt(0));
B = parseInt(COD.charAt(1));
C = parseInt(COD.charAt(2));
D = parseInt(COD.charAt(3));
E = parseInt(COD.charAt(4));

S = (6 * A) + (5 * B) + (4 * C) + (3 * D) + (2 * E);
DIGV = S % 7;

Demonstration

1

Not taking into consideration specificities of languages (given the theme of Algoritmos):


Your solution seems adequate. You can take user input as a string and work with each character. Have, contact, care to make the calculations with the digits equivalent to those characters.

For example: if the user type 12345, we would have A = 1, nay A = '1' (A is the digit 1, not the character '1' which has decimal integer value equal to 49, see to ASCII table).


Another possible solution would be to read the user input, character by character, in a loop. This solution is quite similar to the previous one. The biggest difference would be the fact that the input is taken character by character, not the full string.


A third output would be to take the integer value typed by the user and use entire divisions to find the characters of each position.

Thus, for an entrance 12345 and whereas:

  • the operator // give us the result of the entire division;
  • the operator % give us the result of the rest of the entire division.

We would have:

Entrada <- 12345
X <- Entrada

A <- X // 10000
X <- X % 10000

B <- X // 1000
X <- X % 1000

C <- X // 100
X <- X % 100

D <- X // 10
X <- X % 10

E <- X

There are probably other ways to perform this operation, but these are some that I’ve been able to think about.

I believe that the simplest (combining the issue of implementation and clarity of code for others) is the first, also suggested by you.

-2

I did using the Joptionpane class to create a dialog box.

public Static void main(String[] args) { int Code, Digitov, A, B, C, D, E, S; String Campoa, Campob, Campoc, Campod, Campoe;

    CampoA = JOptionPane.showInputDialog(null, "Insira A", "A", JOptionPane.QUESTION_MESSAGE);
    A = Integer.valueOf(CampoA);
    CampoB = JOptionPane.showInputDialog(null, "Insira B", "B", JOptionPane.QUESTION_MESSAGE);
    B = Integer.valueOf(CampoB);
    CampoC = JOptionPane.showInputDialog(null, "Insira C", "C", JOptionPane.QUESTION_MESSAGE);
    C = Integer.valueOf(CampoC);
    CampoD = JOptionPane.showInputDialog(null, "Insira D", "D", JOptionPane.QUESTION_MESSAGE);
    D = Integer.valueOf(CampoD);
    CampoE = JOptionPane.showInputDialog(null, "Insira E", "E", JOptionPane.QUESTION_MESSAGE);
    E = Integer.valueOf(CampoE);

    Codigo = A+B+C+D+E;
    S = (6*A) + (5*B) + (4*C) + (3*D) + (2*E);
    DigitoV = S % 7;

    JOptionPane.showMessageDialog(null, "O Digito V é: "+ DigitoV +  " Obrigado!!!");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.