Created
January 3, 2020 20:41
-
-
Save SeanFelipe/f2268da43f7270e7a896f17c1fe93317 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'httparty' | |
require 'json' | |
=begin | |
Using any modern language, write a throw-away app or script that will download orders directly from http://files.olo.com/pizzas.json and output the top 20 most frequently ordered pizza topping combinations. List the toppings for each popular pizza topping combination along with its rank and the number of times that combination has been ordered. | |
=end | |
res = HTTParty.get('http://files.olo.com/pizzas.json') | |
data = JSON.parse(res) | |
def tally_data(data) | |
tally = Hash.new | |
data.each do |record| | |
toppings = record['toppings'] | |
toppings.each do |tp| | |
unless tally.has_key? tp | |
tally.store(tp, 1) | |
else | |
new_total = tally[tp] + 1 | |
tally.store(tp, new_total) | |
end | |
end | |
end | |
return tally | |
end | |
def rank_toppings(hash) | |
rankings = Array.new | |
hash.each_pair do |topping, orders| | |
if rankings.length == 0 | |
rankings << [topping, orders] | |
else | |
rankings.each_with_index do |record, ii| | |
if record.last < orders | |
rankings.insert(ii, [topping, orders]) | |
break | |
elsif ii + 1 == rankings.length | |
rankings << [topping, orders] | |
break | |
end | |
end | |
end | |
end | |
return rankings | |
end | |
result = rank_toppings(tally_data(data))[0..19] | |
puts | |
result.each_with_index {|record,ii| puts "#{record.first} | rank: #{ii + 1} | orders: #{record.last} "} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment