iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 6
0
自我挑戰組

自我挑戰 Ruby 刷題 30 天系列 第 6

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

  • 分享至 

  • xImage
  •  

中秋連假漸入尾聲,覺得假好短阿~
來塊比較簡單的蛋糕輕鬆度過一回合 (想摸魚吼
挑戰 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), [])

影片解題:
Yes


答案:

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

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


上一篇
要刷 1 題還是 2 題呢? 小孩子才做選擇,我全都要!!
下一篇
刷一週題目了,有變強一些嗎?
系列文
自我挑戰 Ruby 刷題 30 天31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言