Numerology
Read something about ’name-to-number’.
It was:
1 2 3 4 5 6 7 8 9
A B C D E F G H I
J K L M N O P Q R
S T U V W X Y Z
Then: sum the numbers to total and add the parts of the number.
Ex: 'TRAZAN APANSSON' = 2+9+1+8+1+5 + 1+7+1+5+1+1+6+5
=> 26 + 27 = 53
5 + 3 = 7
# I group the letters [[AJS], [BKT]] .. and so on...
letters = [*'A'..'I'].zip([*'J'..'R'], [*'S'..'Z'])
# could use 'each' and 'index'
num = ->(ch) {letters.each_with_index {|l, i| return i+1 if l.include?(ch)}; 0 }
# remove 9 until < 10
zum = ->(i) { while i > 9; i -= 9; end; i}
# main proc
to_num = ->(nm) {zum[[].tap {|ar| nm.upcase.chars {|c| ar << num[c]} }.sum]}
name = 'Oliver Cromwell'
p to_num[name]
# => 2
To shorten it…
# I could use letters = [*'A'..'Z'] and divmod
l = [*'A'..'Z']
nm = ->(c) {l.index(c).divmod(9)[1] + 1}
# i could recurse zum...
zm = ->(i) { i > 9 ? zm[i-9] : i }
# I could use map
to_num = ->(n) {
zm[n.upcase.gsub(/[^A-Z]/, '').chars.map(&nm).sum]
}
p to_num[name]
Final code:
(using gsub to avoid ’not found')
l = [*'A'..'Z']
nm = ->(c) {l.index(c).divmod(9)[1] + 1}
zm = ->(i) { i > 9 ? zm[i-9] : i }
to_num = ->(n) {
zm[n.upcase.gsub(/[^A-Z]/, '').chars.map(&nm).sum]
}
p to_num['Oliver Cromwell']
# => 2