Closed MShajieShah closed 4 years ago
Problem # 42 implementation still pending and other exercises are incomplete because of absenteeism of @MShajieShah today without any intimation. Unfortunately this PR will also face closure delay
@imranhayat. I've assigned tasks to @MShajieShah, and explained procedure to adopt which can help him overcome the previous mistakes. Hope to see responsibility and better code in upcoming days :+1:
def find_most_occurred_item(arr) return 'Array has unique elements already' if arr.uniq == arr freq_hash = build_frequency_hash(arr) max_value = freq_hash.values.max keys = obtain_freq_hash_keys(freq_hash, max_value) "#{keys} appears #{max_value} times" end
def build_frequency_hash(arr) hash = Hash.new(0) arr.each do |value| hash[value] += 1 end hash end
def obtain_freq_hash_keys(freq_hash, max_value) freq_hash.select{ |k,v| v == max_value }.keys end
puts find_most_occurred_item([1, 2, 3, 3, '', ''])
def get_array_sum(arr) return 'Array is Empty!.' if arr.empty? return calculate_sum_before_value_seventeen(arr) if arr.include?(17) arr.sum end
def calculate_sum_before_value_seventeen(arr) i = arr.index(17) arr.slice!(index..-1) arr.sum end
get_array_sum([]) get_array_sum([1,2,3]) get_array_sum([1,2,3,17,3,4])
def find_most_occurred_item(arr) return 'Array has unique elements already' if arr.uniq == arr uniq_vals = arr.uniq repeated_vals = arr.select{ |v| arr.count > 1 } make_hash_for_values_repetitions(uniq_vals, repeated_vals) end
def make_hash_for_values_repetitions(uniq_vals, repeated_vals) result_hash = Hash.new
uniq_vals.each do |value| count = repeated_vals.count(value) result_hash.merge!(value => count) end result_hash end
puts find_most_occurred_item([1, 2, 2, 3]) => {1=>1, 2=>2, 3=>1}.
**@MShajieShah analyze this solution with me tomorrow. You have to complete the implementation of this method.