刷 1000 題 Coderwars LV8 或 LV7
不代表有本事刷 LV1 一題
前面練習是為了更熟悉 Ruby 語法及如何使用
重點在於如何思考邏輯、解題
接下來會漸漸把強度加強
這次以 LV6、LV7 各一題
1
2
3
4
5Task
Given a list of digits, return the smallest number that could be formed from these digits, using the digits only once (ignore duplicates).
Notes:
Only positive integers will be passed to the function (> 0 ), no negatives or zeros.
1
2
3
4
5
6
7def min_value(digits)
# your code here
end
Test.assert_equals(min_value([1, 3, 1]), 13)
Test.assert_equals(min_value([4, 7, 5, 7]), 457)
Test.assert_equals(min_value([4, 8, 1, 4]), 148)
1
2
3
4Complete the solution so that the function will break up camel casing, using a space between words.
Example
solution('camelCasing') # => should return 'camel Casing'
1
2
3
4
5
6def solution(string)
# complete the function
end
Test.assert_equals(solution('camelCasing'), 'camel Casing')
Test.assert_equals(solution('camelCasingTest'), 'camel Casing Test')
影片解題:
答案:
1
2
3
4
5
6
7
8
9
10# Form The Minimum
def min_value(digits)
digits.uniq.sort.join.to_i
end
# Break camelCase
def solution(string)
string.gsub(/([A-Z])/, ' \1')
end
本文同步發布於 小菜的 Blog https://riverye.com/