Skip to content

Commit 86a77dd

Browse files
committed
Added a few comments
1 parent 84515a1 commit 86a77dd

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

laptop_allocation.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
from enum import Enum
33
from typing import Dict, List
44

5+
# Define available operating systems
56
class OperatingSystem(Enum):
67
MACOS = "macOS"
78
ARCH = "Arch Linux"
89
UBUNTU = "Ubuntu"
910

11+
1012
@dataclass(frozen=True)
1113
class Person:
1214
name: str
@@ -23,6 +25,7 @@ class Laptop:
2325
screen_size_in_inches: float
2426
operating_system: OperatingSystem
2527

28+
# List of available laptops
2629
laptops = [
2730
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
2831
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
@@ -31,14 +34,20 @@ class Laptop:
3134
Laptop(id=5, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
3235
Laptop(id=6, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
3336
]
37+
38+
# List of people to allocate laptops to
3439
people = [
3540
Person(name="Imran", age=22, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.UBUNTU]),
3641
Person(name="Eliza", age=34, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.MACOS,OperatingSystem.UBUNTU]),
3742
Person(name="Leila", age=45, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.UBUNTU,OperatingSystem.ARCH]),
3843
Person(name="Mary", age=35, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.ARCH]),
3944
Person(name="Sara", age=28, preferred_operating_system=[OperatingSystem.MACOS])
4045
]
46+
47+
# Global sadness counter
4148
sadness=0
49+
50+
# Allocate laptops to people to minimize total sadness
4251
def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]:
4352
sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system))
4453
allocated_history : Dict[Person,Laptop] ={}
@@ -48,19 +57,18 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person
4857
for i in range(len(person.preferred_operating_system)) :
4958
for laptop in laptops :
5059
if person.preferred_operating_system[i] == laptop.operating_system :
51-
allocated_history[person.name]=laptop
52-
# print(person.name,laptop.id,laptop.operating_system,i)
53-
sadness += i
60+
allocated_history[person.name]=laptop # assign laptop
61+
sadness += i # increment sadness by preference index
5462
laptops.remove(laptop)
5563
allocated_flag=True
5664
break
5765
if allocated_flag :
5866
break
5967

60-
if not allocated_flag :
68+
if not allocated_flag : # assign any remaining laptop if preferred OS not found
6169
allocated_history[person.name]=laptops[0]
6270
laptops.remove(laptops[0])
63-
sadness +=100
71+
sadness +=100 # high sadness for non-preferred OS
6472

6573
return allocated_history
6674

0 commit comments

Comments
 (0)