Return sub-array from defined range by substrings of defined array elements
来源:互联网 发布:开淘宝商城开店步骤 编辑:程序博客网 时间:2024/05/29 10:12
I have a sorted array of elements (unique and not consecutive):
array= ["AAA", "BBB", "CCC", "DDD", "EEE"]
I defined a range of elements:
range_1 = ("CC" .. "DD")range_2 = ("B" .. "E")
The range of elements are just strings which refers to an array
elements but only ifstarts_with?
is true
for these elements. Example:
"C", "CC" and "CCC" in range - fits to "CCC" in array"D", "DD" and "DDD" in range - fits to "DDD" in array
The desired results for range_1
and range_2
would be like this:
result_1 = ["CCC", "DDD"]result_2 = ["BBB", "CCC", "DDD", "EEE"]
How to implement this in Ruby?
Another way:
def git_em(array, range) array.select {|e| range.any? {|r| e.start_with? r}}endarray= ["AAA", "BBB", "CCC", "DDD", "EEE"]range_1 = ("CC" .. "DD")range_2 = ("B" .. "E")git_em(array,range_1) # => ["CCC", "DDD"]git_em(array,range_2) # => ["BBB", "CCC", "DDD", "EEE"]
0 0
- Return sub-array from defined range by substrings of defined array elements
- Interview Return Products of All Other Elements in Array
- GeeksforGeeks Sort an array according to the order defined by another array
- max sub array of the given array
- Sorting array elements by date
- Sorting array elements by date
- Given a non-empty array of integers, return the k most frequent elements.
- Write efficient code for extracting unique elements from a sorted list of array. e.g.
- Array系列之from(),of()
- kernel编译报Can't use 'defined(@array)'问题
- Can't use 'defined(@array)' (Maybe you should just omit the defined()?)
- Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at kernel/timeconst.pl
- 转载:how to automate Microsoft Excel and return the values from a multi-cell range to an array
- hdu4618Palindrome Sub-Array(乱搞)
- hdu4618 Palindrome Sub-Array
- hdu4618 Palindrome Sub-Array
- HDU4618 Palindrome Sub-Array
- 11536 - Smallest Sub-Array
- &
- Lucene入门教程(一)
- The Power Method for Eigenvectors
- 第二十七题(跳台阶问题)
- hbase详解-----终于了解了
- Return sub-array from defined range by substrings of defined array elements
- poj 2533:Longest Ordered Subsequence
- 能力教学方式
- Lucene入门教程(二)- 理解索引过程的核心类
- Lucene入门教程(二)- 理解索引过程的核心类
- shell 参数列表
- git for c#, clone方法
- 杭电ACM----统计汉字的个数
- ZigZag Conversion