从数组中的数组返回最大的元素

时间:2019-02-27 01:21:04

标签: ruby

我将三个数组组合成另一个数组。数组具有不同的长度,大小和数据类型。例如:

arr1 = [
  ["foooooo", "barrrrrr", 12121113222331144450],
  ["fooollllll", "barrrrrr", 5555555555]
]

animals = [
  ['Hector', 'Chihuahua', 7],
  ['Max', 'Chihuahua', 9]
]

people = [
  %w(Jesse\ James 123\ Homewood\ Home\ Drive San\ Francisco\ CA 510-555-1234 510-123-4567),
  %w(Thomas\ Masters 123\ Sweetwood\ Drive San\ Francisco\ CA 510-656-6589 510-123-1236)
]

friends = [
  %w(first_name last_name telephone address city state zip_code birthdate salary),
  [
    'Jessica',
    'Simpson',
    '485-123-456',
    '9210 Cheery Avenue',
    'Tyler',
    'TX',
    '12345',
    '7/6/81',
    '500'
  ],
  [
    'Alexis',
    'Tyler',
    '123-123-789',
    '9210 Simpson Avenue',
    'Chandler',
    'AZ',
    '62345',
    '8/2/72',
    '1200'
  ]
]
  • arr1有六个数组,每个数组中有三个元素。
  • arr2具有五个数组,每个数组中六个元素。
  • arr3有9个数组,每个数组中有5个元素。
  • arr4具有arr1arr2arr3

我正在尝试遍历这三个数组,并从每个数组数组返回单个最长元素的字符。我想退回arr1有[原文]二十个字符。

i = 0 
while i < arr4_arrays.length
  len = arr4_arrays[i].max_by(&:length)
  len_m = len.map(&:to_s).max_by(&:length).length.to_i
  i+=1
  puts len_m
  puts "\n" 
end

我的代码未进入子数组。它从每个第一个数组返回最大数。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

对于此问题,请使用Ruby迭代器。

使用以下示例:

arrays = [[['foo','foo','foo','foo','foo','fooOOO'],
           ['foo','foo','foo','foo','foo','fooIIIIIII']],
          [['baa','baa','baa','baa','baa','baaUUU'], 
           ['baa','baa','baa','baa','baa','baaAAAAAAAA']]]

进行这样的迭代:

arrays.each do |array|
  array.each do |item|
    puts item.max_by(&:length).length
  end
end

输出:

6
10
6
11

答案 1 :(得分:0)

您似乎想要以下内容,但我不确定要问什么。

def longest(arr)
  arr.map { |a| a.flatten.map(&:to_s).max_by(&:size) }
end

longest animals
  #=> ["Chihuahua", "Chihuahua"] 
longest people
  #=> ["123 Homewood Home Drive", "123 Sweetwood Drive"]
longest friends
  #=> ["first_name", "9210 Cheery Avenue",
  #    "9210 Simpson Avenue"] 

答案 2 :(得分:0)

您似乎想要以下内容,但我不确定要问什么。

def longest(arr)
  arr.map { |a| a.flatten.max_by { |e| e.to_s.size } }
end

longest arr1
  #=> [12121113222331144450, "fooollllll"] 
longest animals
  #=> ["Chihuahua", "Chihuahua"] 
longest people
  #=> ["123 Homewood Home Drive", "123 Sweetwood Drive"] 
longest friends
  #=> ["first_name", "9210 Cheery Avenue",
  #    "9210 Simpson Avenue"]

或者也许

def longest(arr)
  arr.map { |a| a.flatten.map { |e| e.to_s.size }.max }
end

longest arr1
  #=> [20, 10] 
longest animals
  #=> [9, 9] 
longest people
  #=> [23, 19] 
longest friends
  #=> [10, 18, 19] 

甚至是

def longest(arr)
  arr.each_with_object([]) { |a,ar|
    a.flatten.max_by { |e| e.to_s.size }.
      yield_self { |e| ar << [e, e.to_s.size] } }
end

longest arr1 
  #=> [[12121113222331144450, 20],
  #    ["fooollllll", 10]]      
longest animals
  #=> [["Chihuahua", 9],
  #    ["Chihuahua", 9]] 
longest people
  #=> [["123 Homewood Home Drive", 23],
  #    ["123 Sweetwood Drive", 19]] 
longest friends
  #=> [["first_name", 10],
  #    ["9210 Cheery Avenue", 18],
  #    ["9210 Simpson Avenue", 19]]