forked from lumisong/1008_Student_Scaffold_A3_S2_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreasure.py
67 lines (51 loc) · 2.24 KB
/
treasure.py
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
from __future__ import annotations
from config import TreasureConfig
from random_gen import RandomGen
from typing import List
class Treasure:
def __init__(self, value: int, weight: int) -> None:
"""
Complexity:
O(1)
Args:
value (int): The value of this treasure
weight (int): The weight of this treasure
"""
self.value: int = value
self.weight: int = weight
def __eq__(self, value: object) -> bool:
# Do not monitfy this function
return isinstance(value, Treasure) and value.value == self.value and value.weight == self.weight
def __str__(self) -> str:
return f"Treasure: {self.value} ({self.weight}kg)"
def __repr__(self) -> str:
return str(self)
def generate_treasures() -> List[Treasure]:
"""
This function will generate a random list of treasures with random values and weights.
The weights, values and ratios of the treasures will be unique within the output list.
Returns:
list(Treasure): A random list of treasures
Complexity:
Best Case Complexity: O(N)
Worst Case Complexity: O(N) where N is TreasureConfig.MAX_NUMBER_OF_TREASURES.value
This assumes the randint and python set operations can be done in O(1) time.
"""
number_of_treasures = RandomGen.randint(TreasureConfig.MIN_NUMBER_OF_TREASURES.value,
TreasureConfig.MAX_NUMBER_OF_TREASURES.value)
hollow_treasures: List[Treasure | None] = [None] * number_of_treasures
ratios: set[float] = set()
weights_used: set[int] = set()
values_used: set[int] = set()
treasure_count: int = 0
while treasure_count < number_of_treasures:
weight: int = RandomGen.randint(1, TreasureConfig.MAX_TREASURE_WEIGHT.value)
value: int = RandomGen.randint(1, TreasureConfig.MAX_TREASURE_WEIGHT.value)
ratio: float = value / weight
if ratio not in ratios and weight not in weights_used and value not in values_used:
hollow_treasures[treasure_count] = Treasure(value, weight)
ratios.add(ratio)
weights_used.add(weight)
values_used.add(value)
treasure_count += 1
return hollow_treasures