-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
87 lines (70 loc) · 2.16 KB
/
stack.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from process import Process
import random
class Stack:
"""
Implementation of a stack
Attributes:
items (list): The items of the stack
"""
def __init__(self) -> None:
self.items = []
def push(self, item: Process) -> None:
"""
Pushes a process into the stack
Returns:
None
"""
self.items.append(item)
def pop(self) -> Process:
"""
Pops a process from the stack
Returns:
Process: The top most process in the stack
"""
return self.items.pop() if self.items else None
def peak(self) -> Process:
"""
Checks the top most process from the stack
Returns:
Process: The top most process in the stack
"""
return self.items[-1] if self.items else None
def sort(self) -> None:
"""
Sorts the processes in the stack by their arrival time in descending order,
meaning the top most element of the stack will be the process with the nearest
arrival time
Returns:
None
"""
self.items.sort(key=lambda p: p.arrival_time, reverse=True)
def isEmpty(self) -> bool:
"""
Checks if the stack is empty or not
Returns:
bool: True if it is empty, False if not
"""
return not bool(self.items)
def getRandom(self, rand_value, depends_on_probability) -> Process:
"""
Get a random process in the stack from a random value and probability
Returns:
Process/None: If there is a process, None if not
"""
if rand_value < depends_on_probability:
if self.isEmpty():
return None
else:
return random.choice(self.items)
else:
return None
def searchForProcess(self, name: str) -> Process:
"""
Search for a process in the stack using a name
Returns:
Process/None: If found, None if not
"""
for process in self.items:
if process.name == name:
return process
return None