-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tinybid' into feat/auctions
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from nada_dsl import * | ||
|
||
def nada_main(): | ||
# Define parties | ||
auctioneer = Party(name="auctioneer") | ||
parties = [Party(name=f"Party{i}") for i in range(5)] | ||
|
||
# Collect bids from each party | ||
bids = [SecretInteger(Input(name=f"bid_{i}", party=parties[i])) for i in range(5)] | ||
|
||
# Initialize variables to track the highest bid and the winning party index | ||
highest_bid = bids[0] | ||
winning_index = Integer(0) | ||
|
||
# Compare bids to find the highest | ||
for i in range(1, 5): | ||
is_higher = bids[i] > highest_bid | ||
highest_bid = is_higher.if_else(bids[i], highest_bid) | ||
winning_index = is_higher.if_else(Integer(i), winning_index) | ||
|
||
# Output the highest bid and the winning party index | ||
return [ | ||
Output(highest_bid, "highest_bid", auctioneer), | ||
Output(winning_index, "winning_index", auctioneer) | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from nada_dsl import * | ||
|
||
def nada_main(): | ||
# Define the auctioneer as the party responsible for overseeing the auction results | ||
auctioneer = Party(name="auctioneer") | ||
|
||
# Create a list of 5 bidders participating in the auction, 'bidder_0' to 'bidder_4' | ||
num_bidders = 5 | ||
bidders = [Party(name=f"bidder_{i}") for i in range(num_bidders)] | ||
|
||
# Collect bids from each bidder, where each bid is a SecretInteger input unique to the respective bidder | ||
bids = [SecretInteger(Input(name=f"bid_{i}", party=bidders[i])) for i in range(num_bidders)] | ||
|
||
# Determine the highest bid among all the bidders | ||
# Start by initializing the highest bid to the first bidder's bid | ||
highest_bid = bids[0] | ||
# Iterate through the remaining bids to update the highest bid if a higher one is found | ||
for i in range(1, num_bidders): | ||
is_higher = bids[i] > highest_bid | ||
highest_bid = is_higher.if_else(bids[i], highest_bid) | ||
|
||
# Create a list of outputs for each bidder, indicating if their bid matches the highest bid | ||
# Each output is a flag (1 if the bid matches the highest bid, 0 otherwise), visible to the auctioneer | ||
placed_highest_bid = [ | ||
Output((bids[i] == highest_bid).if_else(Integer(1), Integer(0)), f"bidder_{i}_placed_highest_bid", auctioneer) | ||
for i in range(num_bidders) | ||
] | ||
|
||
# Return the highest bid and a list of flags indicating which bidders placed the highest bid | ||
return [Output(highest_bid, "highest_bid", auctioneer)] + placed_highest_bid |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
program: auction_can_tie | ||
inputs: | ||
bid_0: 3 | ||
bid_1: 3 | ||
bid_2: 4 | ||
bid_3: 3 | ||
bid_4: 4 | ||
expected_outputs: | ||
bidder_0_placed_highest_bid: 0 | ||
bidder_1_placed_highest_bid: 0 | ||
bidder_2_placed_highest_bid: 1 | ||
bidder_3_placed_highest_bid: 0 | ||
bidder_4_placed_highest_bid: 1 | ||
highest_bid: 4 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
program: auction | ||
inputs: | ||
bid_0: 10 | ||
bid_1: 2 | ||
bid_2: 3 | ||
bid_3: 11 | ||
bid_4: 9 | ||
expected_outputs: | ||
highest_bid: 11 | ||
winning_index: 3 |