你必須很努力

週末來塊蛋糕吧~ Codewars - Which are in?

2019/09/15
字數統計: 168閱讀時間: 1 min

中秋連假漸入尾聲,覺得假好短阿~
來塊比較簡單的蛋糕輕鬆度過一回合 (想摸魚吼
挑戰 Codewars LV6 題目


題目

1
2
3
4
5
6
7
8
9
10
11
12
13
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 []

1
2
3
4
5
6
7
8
9
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), [])


影片解題:


答案:

1
2
3
def in_array(array1, array2)
array1.select{|x| array2.any?{|y| y.include?(x)}}.sort
end

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

原文連結:https://riverye.com/2019/09/15/週末來塊蛋糕吧-Codewars-Which-are-in/

發表日期:2019-09-15

更新日期:2022-12-21

CATALOG