你必須很努力

Day27 - Codewars 刷題

2019/10/06
字數統計: 188閱讀時間: 1 min

倒數三天強度不升級下怎行呢
Codewars LV4
打算一氣呵成寫完,結果後面卡住 (暈
正確解法明天補上~


題目(Next bigger number with the same digits)

1
2
3
4
5
6
7
8
9
10
You have to create a function that takes a positive integer number and returns the next bigger number formed by the same digits:

12 ==> 21
513 ==> 531
2017 ==> 2071
If no bigger number can be composed using those digits, return -1:

9 ==> -1
111 ==> -1
531 ==> -1

1
2
3
4
5
6
7
8
9
def next_bigger(n)
#your code here
end

Test.assert_equals(next_bigger(12),21)
Test.assert_equals(next_bigger(513),531)
Test.assert_equals(next_bigger(2017),2071)
Test.assert_equals(next_bigger(414),441)
Test.assert_equals(next_bigger(144),414)


影片解題: Yes


答案:

1
2
3
4
5
6
# Next bigger number with the same digits
def next_bigger(n)
n.to_s.split('').permutation(n.to_s.length).map(&:join).select{ |x| return x.to_i if x.to_i > n }
-1
end
#非正確答案喔!!

下集待續..

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

原文連結:https://riverye.com/2019/10/06/Day27-Codewars-刷題/

發表日期:2019-10-06

更新日期:2019-10-09

CATALOG