iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 12
0
自我挑戰組

自我挑戰 Ruby 刷題 30 天系列 第 12

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

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


題目(Count characters in your string)

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!
def count_chars(s)
  # your code here
end

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

影片解題:
Yes


答案:

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

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


上一篇
刷題難度逐漸增強
下一篇
Day13 - Codewars 刷題
系列文
自我挑戰 Ruby 刷題 30 天31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言