ruby kuta on codewars, min bonus

The Ruby Kata; Minimum Bonus, A Codewars Challenge

Posted on Posted in Ruby, Software Engineering

So it is week 4 of the pre-course for Makers Academy. I know what you are thinking .. where have you been on week3? I don’t see any posts? … Well the answer to your question is Barcelona, which meant having to do the whole of week 3 in two days .. which NO i do not recommend doing…

I thought I would delight my reader by creating a post on a codewars kuta I am bumbling through, since this week is all about achieving a level 5 kyu.

You can find my GitHub Repository to this challenge HERE so you can follow along with the commits and steps there if you prefer…  I would love it if you gave my little independent blog the chance to shine and stay on here though (..please mum …I know you don’t really read this, dad).

 

ruby kuta on codewars, minimum bonus
Obviously I was going to use a post on ‘kata’s to show off my pictures of Japan.

The Challenge:

Task which was given by codewars:

International hackers group organized a programming competition, in which n teams participated.

They were assigned to separate rooms for competitions, and the rooms were lined up in a straight line.

The game was over and each team scored points. It's time to pay bonuses. The rule is:

- The bonus unit is 1K($1000), and each team gets at least 1k.
- The bonus payment process is not public.
- A team can know the bonus amount of its adjacent team, if the
  score of the adjacent team is lower than itself.
- If a team finds that its bonus is no higher than the adjacent team whose
  score is lower than itself, the team will not be satisfied
Given an integer array scores represents the score of all teams. Your task is to calculate how much bonuses international hackers group need to pay to keep all teams satisfied.

Note, the unit of bonus is 1K. All teams are in a straight line, and their order is the same as that of the array elements.

Example

For scores = [10,20,30], the output should be 6.

team1's score = 10
team2's score = 20
team3's score = 30

team1 can get 1K, The team was satisfied
because it knew nothing about the other teams.

team2 can know team1's bonus amount,
So team2 at least get 2K to be satisfied

team3 can know team2's bonus amount,
So team3 at least get 3K to be satisfied

1 + 2 + 3 = 6
For scores = [10,20,20,30], the output should be 6.

The possible bonus amount of each team can be:[1,2,1,2]

For scores = [20,30,10,30,40,10,20,30,40,30], the output should be 20.

The possible bonus amount of each team can be:[1,2,1,2,3,1,2,3,4,1]

 

The Solution:

Step 1:

Understanding what is being asked and breaking it down into an english Explanation of what I want the code to do:

breaking the ruby kata down into english

def minimum_bonus(scores)
Making sense of the problem - writing it out in English 
  #scores are an array of numbers
  #the length of this array should be known/ calculated??)
  #1 (value) is asigned to the first number
  #if the second number is larger than the first it is given 2 (new_value =value +1)
  #if the second number is the same size or smaller than the first it is given 1
  #if the 3rd number is larger than the second it returns 3 (new_value +1)
  #if the 3rd number is smaller than the second it returns 1 (value)
  #all the values returned are added together and calculated.
end

The first thing I did was google; ‘how to select each element in an array with it’s index number’.

I found one extremely useful bit of information on how to best iterate through an array in ruby on stackoverflow:

  • each is sufficient for many usages, since I don’t often care about the indexes.
  • each_ with _index acts like Hash#each – you get the value and the index.
  • each_index – just the indexes. I don’t use this one often. Equivalent to “length.times”.
  • map is another way to iterate, useful when you want to transform one array into another.
  • select is the iterator to use when you want to choose a subset.
  • inject is useful for generating sums or products, or collecting a single result.

So I did this in irb:

2.4.0 :031 > minimum_bonus=[20,30,10,30,40,10,20,30,40,30]
 => [20, 30, 10, 30, 40, 10, 20, 30, 40, 30]
2.4.0 :032 > minimum_bonus.each_with_index
 => #<Enumerator: [20, 30, 10, 30, 40, 10, 20, 30, 40, 30]:each_with_index>
2.4.0 :033 > minimum_bonus.each_with_index do |x, index|
2.4.0 :034 >     if x[index

…..and before completing it I though .. oh I don’t think I need the index ..What I need is to change the input scores into the bonus number .. so I need map?

  • map is another way to iterate, useful when you want to transform one array into another.

The next thing I tested in irb was this:

def minimum_bonus(scores)
  scores=[20,30,10,30,40,10,20,30,40,30]
    scores.map do |x|
        x(1) = 1
        end
end

Then I googled:

“how to access an element in an array by its index number ruby”

And I came across each_with_index again, so decided to give it another chance.

def minimum_bonus(scores)
  scores=[20,30,10,30,40,10,20,30,40,30]
    scores.map.each_with_index do |item, index|
        if index == 0 then 
          item = 1
        end
    end

 

I still needed the map because I needed to alter the values in the original array.

When I tried this in irb it seemed to work:

2.4.0 :054 > scores=[20,30,10,30,40,10,20,30,40,30]
 => [20, 30, 10, 30, 40, 10, 20, 30, 40, 30]
2.4.0 :055 >     scores.map.each_with_index do |item, index|
2.4.0 :056 >             if index == 0 then
2.4.0 :057 >                 item = 1
2.4.0 :058?>             end
2.4.0 :059?>       end
 => [1, nil, nil, nil, nil, nil, nil, nil, nil, nil]

When I extended the logic from this method it did not seem to work:

2.4.0 :088 > scores.map.each_with_index do |item, index|
2.4.0 :089 >             if index == 0 then
2.4.0 :090 >                 item = 1
2.4.0 :091?>             end
2.4.0 :092?>           if index[1] > index [0] then
2.4.0 :093 >                 item = 2
2.4.0 :094?>             end
2.4.0 :095?>         end
 => [nil, nil, 2, nil, nil, nil, 2, nil, nil, nil]
2.4.0 :096 >

So it looks like separating out the if statements over-rides the first one and the item in the array is only set for the second statement when it is returned.

It also looks like there is an issue with the numbering I am assigning to the index.

2.4.0 :096 >   scores.map.each_with_index do |item, index|
2.4.0 :097 >             if index == 0 then
2.4.0 :098 >                 item = 1
2.4.0 :099?>             elsif index[1] > index [0] then
2.4.0 :100 >                 item = 2
2.4.0 :101?>             end
2.4.0 :102?>     end
 => [1, nil, 2, nil, nil, nil, 2, nil, nil, nil]

I noticed I was still messing up on the index number while comparing values in the array. It then dawned on me that this is a ‘comparator’ question. We are to compare values in an array with each other and then assign them a new value based on that. So map may still be correct but I needed to set about googling:

“Comparing numerical values in an array with each other, Ruby”

I started to realised I had been spending too long on this kata and it is currently above my ability. I thought the best thing for me to do was to look at the answer and try to understand it and walk through it.

The Answer:

def minimum_bonus(scores)
  not_satisfy = true
  bonus = [1]
  (1...scores.size).each { |i| bonus << (scores[i] > scores[i-1] ? bonus[i-1]+1 : 1) }
  while not_satisfy
    (1...bonus.size).each do |i|  
      bonus[i-1]+=1 if bonus[i] == bonus[i-1] && scores[i] < scores[i-1]
    end
    
    (1...bonus.size).each do |i|
      if bonus[i] == bonus[i-1] && scores[i] != scores[i-1]
        not_satisfy = true
        break
      else
        not_satisfy = FALSE
      end
    end  
  end
  bonus.reduce(:+)
end

 

The Walk Through:

 

So there you have it, my complete thought process and logical reasoning while working through a codewar’s kuta on comparing elements within an array. I am very new to Ruby so I am assuming I will look back on this in the future and cringe at how long winded my approach was and how I went so wrong.

 

One thought on “The Ruby Kata; Minimum Bonus, A Codewars Challenge

Leave a Reply

Your email address will not be published. Required fields are marked *