-
Notifications
You must be signed in to change notification settings - Fork 13
Better optimal trade bundle sorting #85
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -317,6 +317,61 @@ def find_highest_value_bundle(trades): | |
| return None | ||
|
|
||
|
|
||
| def optimal_bundle(highest_value_bundle): | ||
| """The best bundle if you have more tradable cards than the other guy has | ||
| points is the group of cards that comes closest to reaching their | ||
| available points. | ||
|
|
||
| This does the old sort from complete_trades prior to this commit if there | ||
| is less value than the reciever has available points. | ||
|
|
||
| Otherwise, it finds the biggest bundle under their available poits, sorts | ||
| that by card value, then sorts the leftovers by card value just in case | ||
| one of the things you're trying to trade out gets jumped on by a third | ||
| party.""" | ||
|
|
||
|
|
||
|
|
||
| cards = highest_value_bundle[1]["cards"] | ||
| sorted_cards = [] | ||
|
|
||
|
|
||
|
|
||
| if highest_value_bundle[1]["value"] <= highest_value_bundle[1]["points"]: | ||
| sorted_cards = sorted(cards, key=lambda k: k["value"], reverse=True) | ||
| else: | ||
| largest = 0 | ||
| sorted_cards = [] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorted_cards has already been initialized to |
||
| for i in range (1, len(cards)): | ||
| can_change = False | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename |
||
| for j in itertools.combinations(cards, i): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename |
||
| bundle_value = 0 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename |
||
| for k in j: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename |
||
| bundle_value += k["value"] | ||
| if bundle_value <= highest_value_bundle[1]["points"]: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be just |
||
| can_change = True | ||
| if bundle_value > largest: | ||
| largest = bundle_value | ||
| sorted_cards = list(j) | ||
|
|
||
| if can_change == 0: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rewrite as |
||
| break | ||
|
|
||
|
|
||
| sorted_cards.sort( key=lambda k: k["value"], reverse=True) | ||
|
|
||
| leftovers = [] | ||
| for m in cards: | ||
| if m not in sorted_cards: | ||
| leftovers.append(m) | ||
|
|
||
| for n in leftovers: | ||
| sorted_cards.append(n) | ||
|
|
||
| return(sorted_cards) | ||
|
|
||
|
|
||
|
|
||
| def complete_trades(highest_value_bundle): | ||
| """Sort the cards by highest value first and then send them all. | ||
|
|
||
|
|
@@ -329,8 +384,8 @@ def complete_trades(highest_value_bundle): | |
| return | ||
|
|
||
| cards = highest_value_bundle[1]["cards"] | ||
| # Sort the cards by highest value to make the most valuable trades first. | ||
| sorted_cards = sorted(cards, key=lambda k: k["value"], reverse=True) | ||
| # Here's the fancy new change. Trade as much stuff to the other guy as possible | ||
| sorted_cards = optimal_bundle(highest_value_bundle) | ||
|
|
||
| member_name = highest_value_bundle[1]["name"] | ||
| member_points = highest_value_bundle[1]["points"] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: within a function, please use only single whitespace lines.