How to Select the Longest String from a Ruby Array

How can I select the longest string from a Ruby array?

Just do as below using Enumerable#max_by :

ar = ['one','two','three','four','five']
ar.max_by(&:length) # => "three"

How can I find the longest string in a multi-dimensional array?

=> a.flatten.max_by(&:size)
=> # "MAKKGKPRPDHRPPAHNPHYAHDPPPYSQQQPPLQQQNYAQQMHHGGGGGNRQHARPRPSPPSEVSDCVKYSLFLYNCIFWVSMHSS"

Ruby longest word in array

I would do

class Array
def longest_word
group_by(&:size).max.last
end
end

How to find the longest string in a column with Ruby on Rails?

With MySql:

# Change your model name and field to your needs
Participant.order("MAX(CHAR_LENGTH(first_name)) desc").limit(1)

# works too:
Participant.order("MAX(CHAR_LENGTH(first_name)) desc").first

# and this is the most efficient to get the field directly:
Participant.limit(1).order("MAX(CHAR_LENGTH(first_name)) desc").pluck(:first_name)

With postgres:

Participants.limit(1).order("MAX(CHAR_LENGTH(name)) desc").group("id").pluck(:name)

Find and return the longest array in a nested array with its size

The following code should print the desired result. I explained code with Inline comments.

#Initialize @max to empty array, @max is an array with two elements, like this: [max_array_size, max_array]
@max = []

def max_with_size(array)
# when @max is empty or when array size is greater than what is store in @max, store array size and array contents in @max
(@max = [array.size, array]) if @max.empty? || (@max[0] < array.size)

#Iterate through each element in array
array.each do |x|

#Skip to next element if x is not an array
next unless x.is_a? Array
#Recursively find max of array x
max_with_size(x)
end
@max
end

A way to select maximal values from an array

Here we go :

["abcd","efgh","ijk"].group_by(&:size).max.last #=> ["abcd","efgh"]

Explanation :

  • Enumerable#group_by gives a hash containing each unique size of strings contained in the array as keys, and the matching strings as values => {4=>["abcd", "efgh"], 3=>["ijk"]}
  • Enumerable#max applied on a Hash will give us the highest key with its matching values, in an Array like this : [key, values] => [4, ["abcd", "efgh"]]
  • Array#last will give us the last element of the array ... => ["abcd", "efgh"]

Ruby getting the longest word of a sentence

It depends on how you want to split the string. If you are happy with using a single space, than this works:

def longest(source)
arr = source.split(" ")
arr.sort! { |a, b| b.length <=> a.length }
arr[0]
end

Otherwise, use a regular expression to catch whitespace and puntuaction.

How do I find the size of the longest subarray?

It's so simple

arr.max_by(&:size).size
=> 6

Finding common string in array of strings (ruby)

Here's a rubyish way of doing it. You should use a more advanced algorithm if you have a bunch of strings or they are very long, though:

def longest_common_substr(strings)
shortest = strings.min_by &:length
maxlen = shortest.length
maxlen.downto(0) do |len|
0.upto(maxlen - len) do |start|
substr = shortest[start,len]
return substr if strings.all?{|str| str.include? substr }
end
end
end

puts longest_common_substr(["Extra tv in bedroom",
"Extra tv in living room",
"Extra tv outside the shop"])

How to get the length of longest string in an array

try

$maxlen = max(array_map('strlen', $ary));


Related Topics



Leave a reply



Submit