CodeWar Weekly - 0217~0223
CodeWar Weekly[0217-0223]
6kyu Highest Scoring Word
Given a string of words, you need to find the highest scoring word.
Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc.
You need to return the highest scoring word as a string.
If two words score the same, return the word that appears earliest in the original string.
All letters will be lowercase and all inputs will be valid.
解題思考:取字母轉成數字會是最大值的詞,取 =
max_by,後面的條件就要依照先轉數字ord,然後在計算… ex:sum,reduce,inject…
參考方法 & 學習筆記
Ruby
max_by
max_by {|obj| block } → obj || max_by → an_enumerator
Returns the object in enum that gives the maximum value from the given block.
If no block is given, an enumerator is returned instead.
1 | |
跟 max 的不同在,能不能在設定條件而已,原本的 max 只會取開頭字母最大的 ord
JavaScript
charCodeAt()
The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
1 | |
ord
ord → self
Returns the int itself.
1 | |
Ruby
Solution(1):
1 | |
Solution(2):
1 | |
JavaScript
Solution(1):
1 | |
Solution(2):
1 | |