Created
July 4, 2021 13:41
-
-
Save nandhasuhendra/8f46f1392567fe061a852998b1574039 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ruby 2.7.2 | |
# Description: | |
# Galih and Ratna married during the COVID19 period | |
# and only invited the families of both parters. | |
# They rented a number of minibus to pick up all of | |
# their families to go to wedding. But during the COVID19, | |
# the government held a PSBB program to reduce the impact | |
# of the spread of the virus. Each minibus only can carry | |
# at most 4 passengers. | |
# What a minimum of busses will they needed to rent | |
# if all member of each family should ride in the same | |
# Busses. (One bus can't take more than two family) | |
# INPUT: | |
# 1. The first line contains integer n -- the number of families | |
# 2. The second line contains a sequence of integer. | |
# The integers arw sparated by space, each integer is | |
# the number of members in the family. | |
# OUTPUT: | |
# 1. Print the single number -- the minimum number of | |
# of busses nessecery to drive all family to the wedding | |
# 2. Print "Input must be equal with count of family" if | |
# number of family isn't equal with count of family | |
# Test Case | |
# 1. n = 5, s = 1 2 4 3 3, result = 4 | |
# 2. n = 8, s = 2 3 4 4 2 1 3 1, result = 5 | |
# 3. n = 5, s = 1 5, result = message | |
require 'rspec' | |
MAX_PASSENGER = 4 | |
def minimum_bus(number_of_family, family_members) | |
members = family_members.split(' ').map(&:to_i) | |
return 'Input must be equal with count of family' if members.length != number_of_family | |
busses = 0 | |
hash_passengers = {} | |
members.each do |member| | |
next if member > MAX_PASSENGER | |
if member == MAX_PASSENGER | |
busses += 1 | |
elsif hash_passengers[member].nil? | |
hash_passengers[member] = member | |
busses += 1 | |
elsif (hash_passengers[member] + member) < MAX_PASSENGER | |
hash_passengers[member] = nil | |
end | |
end | |
busses | |
end | |
RSpec.describe 'Count minimum buss for each families' do | |
it 'should be return \'Input must be equal with count of family\' bus for 5 family \'1 5\'' do | |
expect(minimum_bus(5, '1 5')).to eq('Input must be equal with count of family') | |
end | |
it 'should be return 4 bus for 5 family \'1 2 4 3 3\'' do | |
expect(minimum_bus(5, '1 2 4 3 3')).to eq(4) | |
end | |
it 'should be return 5 bus for 8 family \'2 3 4 4 2 1 3 1\'' do | |
expect(minimum_bus(8, '2 3 4 4 2 1 3 1')).to eq(5) | |
end | |
it 'should be return 2 bus for 4 family \'1 1 1 1\'' do | |
expect(minimum_bus(4, '1 1 1 1')).to eq(2) | |
end | |
it 'should be return 3 bus for 6 family \'2 2 1 1 1 1\'' do | |
expect(minimum_bus(6, '2 2 1 1 1 1')).to eq(3) | |
end | |
it 'should be return 7 bus for 11 family \'4 2 2 2 4 1 1 1 1 3 1\'' do | |
expect(minimum_bus(11, '4 2 2 2 4 1 1 1 1 3 1')).to eq(7) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment