中秋連假漸入尾聲,覺得假好短阿~
來塊比較簡單的蛋糕輕鬆度過一回合 (想摸魚吼
挑戰 Codewars LV6 題目
題目:
1
2
3
4
5
6
7
8
9
10
11
12
13Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.
#Example 1: a1 = ["arp", "live", "strong"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns ["arp", "live", "strong"]
#Example 2: a1 = ["tarp", "mice", "bull"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns []
1
2
3
4
5
6
7
8
9def in_array(array1, array2)
...
end
a1 = ["arp", "live", "strong"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
Test.assert_equals(in_array(a1, a2), ["arp", "live", "strong"])
a1 = ["tarp", "mice", "bull"]
Test.assert_equals(in_array(a1, a2), [])
影片解題:
答案:
1
2
3def in_array(array1, array2)
array1.select{|x| array2.any?{|y| y.include?(x)}}.sort
end
本文同步發布於 小菜的 Blog https://riverye.com/