今日喉嚨微恙
講話比較小聲且無力
請多多包涵 ~"~
1
2
3
4
5
6
7
8
9
10Your task is to sort a given string. Each word in the string will contain a single number. This number is the position the word should have in the result.
Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).
If the input string is empty, return an empty string. The words in the input String will only contain valid consecutive numbers.
Examples
"is2 Thi1s T4est 3a" --> "Thi1s is2 3a T4est"
"4of Fo1r pe6ople g3ood th5e the2" --> "Fo1r the2 g3ood 4of th5e pe6ople"
"" --> ""
1
2
3
4
5
6
7def order(words)
# your code...
end
Test.assert_equals(order("is2 Thi1s T4est 3a"), "Thi1s is2 3a T4est")
Test.assert_equals(order("4of Fo1r pe6ople g3ood th5e the2"), "Fo1r the2 g3ood 4of th5e pe6ople")
Test.assert_equals(order(""), "", "Empty input should return empty string")
影片解題:
答案:
1
2
3
4# Your order, please
def order(words)
words.split.sort_by{ |x| x.chars.min }.join' '
end
本文同步發布於 小菜的 Blog https://riverye.com/