Advertisement
Guest User

Untitled

a guest
Oct 8th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.77 KB | None | 0 0
  1. def max_multiplication(str)
  2.   return unless str.is_a?(String)
  3.  
  4.   digital_combinations = str.scan(/\d+/).select { |item| item.length >= 4 }
  5.  
  6.   return if digital_combinations.empty?
  7.  
  8.   best_digital_combination = digital_combinations.map { |item| item.chars.sort { |a, b| b <=> a }.take(4) }.max
  9.   best_digital_combination.map(&:to_i).reduce(:*)
  10. end
  11.  
  12. def sort(array_of_numbers)
  13.   array_of_numbers.map { |number| number.to_s(2) }.sort do |a, b|
  14.     if a.count('1') == b.count('1')
  15.       a.to_i(2) <=> b.to_i(2)
  16.     else
  17.       a.count('1') <=> b.count('1')
  18.     end
  19.   end.map { |number| number.to_i(2) }
  20. end
  21.  
  22. p(max_multiplication('abc12345def'))
  23. p(max_multiplication('a1b2c3d4e'))
  24. p(max_multiplication('ds23fgf99fdsd54fd90'))
  25.  
  26. p(sort([3,7,8,9]))
  27. p(sort([3,12,15,5]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement