Update canonical-data.json

Also, if there is an edge case that is really language-depending, the track maintainers can choose to add that piece of information in an additional file, that is shown next to the standard instructions. It is not necessary to cram everything into the tests. I can understand why it might be more accessible to have the information visible in the instructions as well.

But if the 5 is doubled, we aren’t starting at the very right.

You start at the right,
the first (right-most) digit keeps its value,
the next digit gets doubled,
the next digit keeps its value,
the next digit gets doubled, etc.


Concrete, with the digits [0, 5, 9]:

  • double every second digit, starting from the right.
    digits = [0, 2 * 5, 9] = [0, 10, 9]
    
  • If doubling the number results in a number greater than 9 then subtract 9 from the product.
    digits = [0, 10 - 9, 9] = [0, 1, 9]
    
  • sum all of the digits
    sum = 0 + 1 + 9 = 10
    
  • If the sum is evenly divisible by 10, then the number is valid.
    In this case the sum is 10 which is divisible by 10, therefore the number is value valid.

It might be easier to understand with longer numbers, take a look at the examples in the instructions.

But why then is it different for the even numbers?

It’s not.

  • even numbered input: 123456

    starting from the right, double each 2nd digit: 123456

  • odd numbered input: 12345

    starting from the right, double each 2nd digit: 12345

It only looks different because the left-most digit is doubled for even numbered input. But that’s irrelevant because we’re starting from the right.

It might make more sense if we reverse the input first:

  • input: 123456
    reversed: 654321
    doubled: 654321
  • input: 12345
    reversed: 54321
    doubled: 54321

I’m not sure if I understand the question. Do you mean an even number of digits?


Concrete, with the digits [0, 9, 5, 2, 4, 5, 8, 8]:

  • double every second digit, starting from the right.
    digits = [2 * 0, 9, 2 * 5, 2, 2 * 4, 5, 2 * 8, 8]
           = [0, 9, 10, 2, 8, 5, 16, 8]
    
  • If doubling the number results in a number greater than 9 then subtract 9 from the product.
    digits = [0, 9, 10 - 9, 2, 8, 5, 16 - 9, 8]
           = [0, 9, 1, 2, 8, 5, 7, 8]
    
  • sum all of the digits
    sum = 0 + 9 + 1 + 2 + 8 + 5 + 7 + 8 = 40
    
  • If the sum is evenly divisible by 10, then the number is valid.
    In this case the sum is 40 which is divisible by 10, therefore the number is value valid.

OK, if I make my algorithm skip the first number, it works. Sorry, my bad.

A common technique is to initialize a boolean “double” variable to false. Then for each loop iteration, negate the variable.