Home > Back-end >  (Hackerrank) Ruby - Enumerable - group_by if condition problem
(Hackerrank) Ruby - Enumerable - group_by if condition problem

Time:04-01

I just try to solve this problem. (https://www.hackerrank.com/challenges/ruby-enumerable-group-by/problem?isFullScreen=true&h_r=next-challenge&h_v=zen)

My solution is works at ruby mine but at the hackerrank first if condition does not work.

def group_by_marks(marks, pass_marks)
  all_students = Hash.new
  failed = Array.new
  passed = Array.new

  marks.each do |student|

    if student[1] < pass_marks
      failed.push(student)
    elsif student[1] > pass_marks
      passed.push(student)
    end

  end
  all_students["Failed"] = failed
  all_students["Passed"] = passed
  puts all_students
end

CodePudding user response:

The problem statement contains this If a particular key is empty, don't return that key.

Also, you need to remove puts as you need to return it, all_students is the same as return all_students cause ruby returns the last line automatically.

So you need to update the last 3 lines with the following.

  all_students["Failed"] = failed if failed.any?
  all_students["Passed"] = passed if passed.any?
  all_students
  •  Tags:  
  • ruby
  • Related