這是一場與自己的比賽
逼自己每天刷題記錄過程
並試著錄影講解解題過程
即便錄得很爛、沒人看也沒關係
(大不了之後刪除
想知道這段時間能走多遠
當時若再早點報名
說不定現在已經超過 10 天了
這次依然挑戰 Codewars LV6 題目
為什麼這麼愛刷 LV6 題目呢?
是因為目前 Codewars 帳號等級在 LV7
不知是要刷一定程度才能跳級還是能跳級刷題
反正題目來就刷 XD
題目:
1
2
3
4
5
6
7
8
9
10
11
12
13There 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
1
2
3
4
5
6
7
8
9
10
11def 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
影片解題:
答案:
1
2
3
4
5def find_uniq(arr)
arr.select{ |x| arr.count(x) == 1 }.join.to_f
end
# 注意:此寫法不夠優化,會顯示 Execution Timed Out (12000 ms)
本文同步發布於 小菜的 Blog https://riverye.com/