Skip to content

Instantly share code, notes, and snippets.

@taowen
Created June 9, 2014 02:00
Show Gist options
  • Save taowen/47dacc9578eec38fba6a to your computer and use it in GitHub Desktop.
Save taowen/47dacc9578eec38fba6a to your computer and use it in GitHub Desktop.
from decimal import Decimal
bids = [int(a) for a in '60 62 60 56 56 57'.split('\t')]
print(bids)
price = min(bids)
total_payment = (len(bids) - 1) * price
total_bid = sum(bids) - price
min_pos = bids.index(price)
max_pos = bids.index(max(bids))
bids[min_pos] = 'min'
bids[max_pos] = 'max'
remaining_payment = total_payment
payments = []
print(bids)
for bid in bids:
if 'min' == bid:
payments.append(str(total_payment))
continue
if 'max' == bid:
payments.append('max')
continue
TWOPLACES = Decimal(10) ** -2
payment = ((Decimal(bid) * total_payment) / total_bid).quantize(TWOPLACES)
payments.append('-%s' % str(payment))
remaining_payment -= payment
payments[payments.index('max')] = '-%s' % str(remaining_payment)
print('\t'.join(payments))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment