查找字母的字母顺序分数
Find a letter's alphabetical score
在字母表中查找字符位置的正确方法是什么?例如:
"A".find_score # => 1
"C".find_score # => 3
"A".ord
returns 65,"A" 的数字代码,这是字母表的起始位置。如果你想让它从 1 开始,你可以减去 64:
def get_code(c)
c.upcase.ord - 'A'.ord + 1
end
工作方式如下:
get_code('A') # 1
get_code('B') # 2
get_code('C') # 3
在字母表中查找字符位置的正确方法是什么?例如:
"A".find_score # => 1
"C".find_score # => 3
"A".ord
returns 65,"A" 的数字代码,这是字母表的起始位置。如果你想让它从 1 开始,你可以减去 64:
def get_code(c)
c.upcase.ord - 'A'.ord + 1
end
工作方式如下:
get_code('A') # 1
get_code('B') # 2
get_code('C') # 3