Created
March 8, 2012 18:42
-
-
Save moreirayokoyama/2002607 to your computer and use it in GitHub Desktop.
Mod 10 (declarative fashion)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mod10(numbers) { | |
var multipliers = [2, 1]; | |
var multiplicands = numbers.reverse(); | |
var products = mapProducts(multiplicands, multipliers); | |
var mod = products.map( | |
function (number) { | |
return number.toString().split('').map( | |
function (str) { | |
return parseInt(str); | |
}).sum(); | |
}).sum() % 10; | |
if (mod >= 10) return 0; | |
return 10 - mod; | |
} |
Eu escrevi ontem
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aliás, Juan... estou fazendo o curso de Algoritmos online de Stanford... https://www.coursera.org/algo/auth/welcome
O curso inicia oficialmente dia 12, mas o conteúdo já tá disponível.
Seria legal ver uma implementação sua do módulo 10... assim, não pra ficar comparando, mas pra trocar idéias. Afinal, como a citação que o professor de Stanford usa:
"Perhaps the most important principle for the good algorithm designer is to refuse to be content."