iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
0
自我挑戰組

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

刷一週題目了,有變強一些嗎?

  • 分享至 

  • xImage
  •  

這是一場與自己的比賽
逼自己每天刷題記錄過程
並試著錄影講解解題過程
即便錄得很爛、沒人看也沒關係
(大不了之後刪除
想知道這段時間能走多遠
當時若再早點報名
說不定現在已經超過 10 天了


這次依然挑戰 Codewars LV6 題目
為什麼這麼愛刷 LV6 題目呢?
是因為目前 Codewars 帳號等級在 LV7
不知是要刷一定程度才能跳級還是能跳級刷題
反正題目來就刷 XD


題目:

There is an array with some numbers. All numbers are equal except for one. Try to find it!

find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55
It’s guaranteed that array contains more than 3 numbers.

The tests contain some very huge arrays, so think about performance.

This is the first kata in series:

Find the unique number (this kata)
Find the unique string
Find The Unique
def find_uniq(arr)
  
end

describe "Solution" do
  it "should test for something" do
    Test.assert_equals(find_uniq([1,1,1,1,0]), 0)
    Test.assert_equals(find_uniq([ 1, 1, 1, 2, 1, 1 ]), 2);
    Test.assert_equals(find_uniq([ 0, 0, 0.55, 0, 0 ]), 0.55);
  end
end

影片解題:
Yes


答案:

def find_uniq(arr)
  arr.select{ |x| arr.count(x) == 1 }.join.to_f
end

注意:此寫法不夠優化,會顯示 Execution Timed Out (12000 ms)

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


上一篇
週末來塊蛋糕吧~ Codewars - Which are in?
下一篇
壓線前逼自己刷一題 Codewars
系列文
自我挑戰 Ruby 刷題 30 天 共 31 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言