Last active
January 5, 2020 20:42
-
-
Save SeanFelipe/36c95031545ea0bccca48eaacdfb60ec 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
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| | |
record['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} "} | |
# -------------------------------------------------------------------------------------- | |
# REFACTOR AFTER STUDYING sorting algorithms ! | |
# I haven't worked on a lot of sorting problems in my career, but I knew my first | |
# iteration was primitive. Here's another attempt after studying quicksort and the like. | |
# Not necessarily the most optimal, but better, and straightforward at least in my mind. | |
require 'httparty' | |
require 'json' | |
def tally_toppings(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 filter_left_right(array, side=:left, offset=0) | |
comparator = array.first | |
comparator_count = comparator.last | |
left_side, right_side = Array.new, Array.new | |
for index in 1..array.length-1 do | |
target = array[index] | |
if target.last <= comparator_count | |
left_side.append(target) | |
elsif target.last > comparator_count | |
right_side.append(target) | |
end | |
end | |
comparator_index = offset + left_side.length | |
right_offset = offset + left_side.length + 1 | |
@final[comparator_index] = comparator | |
if left_side.length >= 3 | |
filter_left_right(left_side, :left, offset) | |
elsif left_side.length == 2 | |
if left_side.first.last > left_side.last.last | |
@final[offset], @final[offset + 1] = left_side.last, left_side.first | |
else | |
@final[offset], @final[offset + 1] = left_side.first, left_side.last | |
end | |
elsif left_side.length == 1 | |
@final[offset] = left_side.first | |
end | |
if right_side.length >= 3 | |
filter_left_right(right_side, :right, right_offset) | |
elsif right_side.length == 2 | |
if right_side.first.last > right_side.last.last | |
@final[right_offset], @final[right_offset + 1] = right_side.last, right_side.first | |
else | |
@final[right_offset], @final[right_offset + 1] = right_side.first, right_side.last | |
end | |
elsif right_side.length == 1 | |
@final[right_offset] = right_side.first | |
end | |
end | |
def init_final_array(toppings_hash) | |
arr = Array.new | |
toppings_hash.length.times { arr << nil } | |
return arr | |
end | |
res = HTTParty.get('http://files.olo.com/pizzas.json') | |
data = JSON.parse(res) | |
@toppings_data = tally_toppings(data) | |
@final = init_final_array(@toppings_data) | |
unsorted_array = @toppings_data.to_a | |
filter_left_right(unsorted_array) | |
for ii in 0..19 do | |
topping = @final[@final.length - ii - 1] | |
rank = ii + 1 | |
puts "#{topping.first} | rank: #{rank} count: #{topping.last}" | |
end |
Author
SeanFelipe
commented
Jan 3, 2020
refactor output :
pepperoni | rank: 1 count: 6335
four cheese | rank: 2 count: 1611
mozzarella cheese | rank: 3 count: 1461
bacon | rank: 4 count: 1447
beef | rank: 5 count: 1140
sausage | rank: 6 count: 831
mushrooms | rank: 7 count: 733
italian sausage | rank: 8 count: 672
black olives | rank: 9 count: 456
chicken | rank: 10 count: 401
pineapple | rank: 11 count: 360
ham | rank: 12 count: 341
jalapenos | rank: 13 count: 258
green peppers | rank: 14 count: 206
canadian bacon | rank: 15 count: 174
diced white onions | rank: 16 count: 168
cheddar cheese | rank: 17 count: 154
diced tomatoes | rank: 18 count: 128
alredo sauce | rank: 19 count: 127
onions | rank: 20 count: 121
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment