你必須很努力

刷題的標題好難想 (沒梗了 XD)

2019/09/21
字數統計: 165閱讀時間: 1 min

一開始想標題還有點梗
當每天做重覆的事情時
想梗反而比解題還麻煩 XD
這次Coderwars LV6


題目(Count characters in your string)

1
2
3
4
5
The main idea is to count all the occuring characters(UTF-8) in string. If you have string like this aba then the result should be { 'a': 2, 'b': 1 }

What if the string is empty ? Then the result should be empty object literal { }

For C#: Use a Dictionary<char, int> for this kata!

1
2
3
4
5
6
def count_chars(s)
# your code here
end

Test.assert_equals(count_chars("aba"), {"a" => 2, "b" => 1})
Test.assert_equals(count_chars(""), {})


影片解題: Yes


答案:

1
2
3
4
# Count characters in your string
def count_chars(s)
s.split('').map{ |i| ["#{i}", s.count(i)] }.to_h
end

本文同步發布於 小菜的 Blog https://riverye.com/

原文連結:https://riverye.com/2019/09/21/刷題的標題好難想-沒梗了-XD/

發表日期:2019-09-21

更新日期:2019-10-09

CATALOG