你必須很努力

Day28 - Codewars 刷題

2019/10/07
字數統計: 686閱讀時間: 3 min

先補上昨天未解完 Codewars LV4 的答案,
時間複雜度沒有解決,若有更好解法可在下方留言讓我知道喔~~

到 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)

題目(Most frequently used words in a text)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Write a function that, given a string of text (possibly with punctuation and line-breaks), returns an array of the top-3 most occurring words, in descending order of the number of occurrences.

Assumptions:
A word is a string of letters (A to Z) optionally containing one or more apostrophes (') in ASCII. (No need to handle fancy punctuation.)
Matches should be case-insensitive, and the words in the result should be lowercased.
Ties may be broken arbitrarily.
If a text contains fewer than three unique words, then either the top-2 or top-1 words should be returned, or an empty array if a text contains no words.
Examples:
top_3_words("In a village of La Mancha, the name of which I have no desire to call to
mind, there lived not long since one of those gentlemen that keep a lance
in the lance-rack, an old buckler, a lean hack, and a greyhound for
coursing. An olla of rather more beef than mutton, a salad on most
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
on Sundays, made away with three-quarters of his income.")
# => ["a", "of", "on"]

top_3_words("e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e")
# => ["e", "ddd", "aa"]

top_3_words(" //wont won't won't")
# => ["won't", "wont"]
Bonus points (not really, but just for fun):
Avoid creating an array whose memory footprint is roughly as big as the input text.
Avoid sorting the entire array of unique words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def top_3_words(text)
# ...
end

Test.assert_equals(top_3_words("a a a b c c d d d d e e e e e"), ["e", "d", "a"])
Test.assert_equals(top_3_words("e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e"), ["e", "ddd", "aa"])
Test.assert_equals(top_3_words(" //wont won't won't "), ["won't", "wont"])
Test.assert_equals(top_3_words(" , e .. "), ["e"])
Test.assert_equals(top_3_words(" ... "), [])
Test.assert_equals(top_3_words(" ' "), [])
Test.assert_equals(top_3_words(" ''' "), [])
Test.assert_equals(top_3_words("""In a village of La Mancha, the name of which I have no desire to call to
mind, there lived not long since one of those gentlemen that keep a lance
in the lance-rack, an old buckler, a lean hack, and a greyhound for
coursing. An olla of rather more beef than mutton, a salad on most
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
on Sundays, made away with three-quarters of his income."""), ["a", "of", "on"])


影片解題:


答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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
next_bigger(1234567980)
#會有時間複雜度喔



# Most frequently used words in a text
def top_3_words(text)
text.scan(/[A-Za-z']+/)
.select{ |x| /[A-Za-z]/ =~ x }
.group_by{ |x| x.downcase }
.sort_by{ |k, v| -v.count }
.first(3)
.map(&:first)
end

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

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

發表日期:2019-10-07

更新日期:2022-12-21

CATALOG