Skip to content

Problem 1 #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions QubeChallenge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'csv'
require 'byebug'
class QubeChallenge
def solution1
all_partners = load_csv_data
output_array = []
CSV.new(open('./input.csv')).each do |row|
# row[0] is delivery ID, row[1] is size row[2] id threatre
theatre_partners = all_partners.select{|p| p["Theatre"]== row[2]}

theatre_partners.each_with_index do |tp,index|
slab_size = row[1]&.to_i
d_id = row[0]
size_min,size_max = tp["Size Slab (in GB)"].split("-")
minimum_cost = tp["Minimum cost"]&.to_i
price = tp["Cost Per GB"]&.to_i
total_cost = slab_size * price
final_cost = total_cost < minimum_cost ? minimum_cost : total_cost
if (size_min.to_i..size_max.to_i).include?(slab_size)
output_array << [d_id,true,tp["Partner ID"],final_cost]
break
elsif index == (theatre_partners.size - 1)
output_array << [d_id,false,"",""]
end
end
output_array << [d_id,false,"",""] if theatre_partners.empty?
end
generate_csv(output_array)
end

def load_csv_data
csv = CSV.new(open('./partners.csv'), headers: :first_row, converters: ->(f) { f&.strip })
csv = csv.to_a.map {|row| row.to_hash }
end

def generate_csv(csv_data)
CSV.open('output.csv', 'w') do |csv|
csv_data.each { |ar| csv << ar }
end
end
end
QubeChallenge.new.solution1