中秋連假漸入尾聲,覺得假好短阿~
來塊比較簡單的蛋糕輕鬆度過一回合 (想摸魚吼
挑戰 Codewars LV6 題目
題目:
Given 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 []
def 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), [])
答案:
def in_array(array1, array2)
array1.select{|x| array2.any?{|y| y.include?(x)}}.sort
end
本文同步發布於 小菜的 Blog https://riverye.com/