Skip to content

Instantly share code, notes, and snippets.

@jgagner
Created April 13, 2010 18:09
Show Gist options
  • Save jgagner/364900 to your computer and use it in GitHub Desktop.
Save jgagner/364900 to your computer and use it in GitHub Desktop.
#Output
#Jerome-Gagners-MacBook-Pro:amazon jgagner$ ruby bids.rb
#Current bid: 6, bid total 6, current max 0
#Current bid: 6, bid total 12, current max 6
#Current bid: 6, bid total 18, current max 12
#Current bid: 3, bid total 12, current max 18
#Accepted bids = 666 at 18
#Current bid: 6, bid total 6, current max 0
#Current bid: 5, bid total 10, current max 6
#Current bid: 4, bid total 12, current max 10
#Current bid: 3, bid total 12, current max 12
#Current bid: 3, bid total 15, current max 12
#Accepted bids = 65433 at 15
bids1 = [6,6,6,3,3,3,2,1]
bids2 = [6,5,4,3,3,3,2,1]
max_instances = 5
def calc_max_bid max_instances, bids
accepted_bids = []
i = 1
current_max, final_price = 0
#loop until we meet the max instances
while i <= max_instances do
current_bid = bids[i-1]
bid_total = current_bid * i
puts "Current bid: #{current_bid}, bid total #{bid_total}, current max #{current_max}"
#If the amount of revenue from current bid is higher than the previous bid, that is now the new max revenue
if bid_total >= current_max
#add the current bid to the accepted bids
accepted_bids << current_bid
#set the current max to the bid total
current_max = bid_total
#Set the final price to the current max
final_price = current_max
else
#If it is not higher, then the previous iteration is the highest revenue
break
end
i = i + 1
end
puts "Accepted bids = #{accepted_bids} at #{final_price}"
end
calc_max_bid 5,bids1
calc_max_bid 5,bids2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment