-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_auctions.py
More file actions
73 lines (64 loc) · 1.97 KB
/
student_auctions.py
File metadata and controls
73 lines (64 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# This script processes a given text file to produce a required output.
#
# Created for the AiCore Hackathon.
#
# Requires the input.txt in the same folder as the script to be run.
#
# Daniel Killen 17/03/2024
auctions = {}
def update_bid(tick):
"""
Updates an existing auction with a new bid
"""
# confirm bid is within time limit
if int(tick[0]) < auctions[tick[3]]["end_time"]:
# confirm new bid is higher
if float(tick[4]) > auctions[tick[3]]["current_bid"]:
# set last price
auctions[tick[3]]["previous_bid"]=auctions[tick[3]]["current_bid"]
# update latest price
auctions[tick[3]]["current_bid"]=float(tick[4])
# update winning user
auctions[tick[3]]["winning_user"]=tick[1]
# update if reserve met
if float(tick[4]) > auctions[tick[3]]["reserve_price"]:
auctions[tick[3]]["reserve_met"]=True
return
def create_auction(tick):
"""
Creates a new auction
"""
auctions[tick[3]] = {
"reserve_price": float(tick[4]),
"current_bid": 0,
"previous_bid": 0,
"reserve_met": False,
"winning_user": "",
"end_time": int(tick[5])
}
return
with open('input.txt','r') as input_file:
input = input_file.readlines()
for i in input:
tick = i.strip().split('|')
# check if heartbeat
if len(tick) == 1:
pass
# check if sell
elif tick[2] == "SELL":
create_auction(tick)
# check if bid
elif tick[2] == "BID":
update_bid(tick)
for i, key in enumerate(auctions):
close_time=auctions[key]['end_time']
item = key
if auctions[key]['reserve_met'] == True:
status= 'SOLD'
user_id=auctions[key]['winning_user']
price_paid=auctions[key]['previous_bid']
else:
status = 'UNSOLD'
user_id=""
price_paid=0.00
print(f"{close_time}|{item}|{user_id}|{status}|{price_paid:.2f}")