-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAss1.py
More file actions
120 lines (94 loc) · 4.2 KB
/
Ass1.py
File metadata and controls
120 lines (94 loc) · 4.2 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import struct
# Part A: Modeling Political System with Inheritance
class ElectionCommission:
def __init__(self, country, total_states):
self.country = country
self.total_states = total_states
def display_info(self):
print(f"Country: {self.country}")
print(f"Total States: {self.total_states}")
class StateElectionCommission(ElectionCommission):
def __init__(self, country, total_states, state_name, num_constituencies):
super().__init__(country, total_states)
self.state_name = state_name
self.num_constituencies = num_constituencies
def state_display_info(self):
print(f"State Name: {self.state_name}")
print(f"Number of Constituencies: {self.num_constituencies}")
#Part B: Candidate Data in Bytes
class Candidate:
def __init__(self, candidate_id, name, party, age, is_incumbent, vote_share):
self.candidate_id = candidate_id
self.name = name
self.party = party
self.age = age
self.is_incumbent = is_incumbent
self.vote_share = vote_share
self.votes = bytearray([0] * 5) # 5 constituencies default votes
def encode_candidate_to_bytes(self):
candidate_id_byte = self.candidate_id.to_bytes(2, byteorder='big')
name_bytes = self.name.encode('utf-8')
name_length = len(name_bytes).to_bytes(1, 'big')
party_bytes = self.party.encode('utf-8')
party_length = len(party_bytes).to_bytes(1, 'big')
age_byte = self.age.to_bytes(1, 'big')
is_incumbent_byte = b'\x01' if self.is_incumbent else b'\x00'
vote_share_byte = struct.pack('f', self.vote_share)
return (candidate_id_byte +
name_length + name_bytes +
party_length + party_bytes +
age_byte +
is_incumbent_byte +
vote_share_byte)
@staticmethod
def decode_candidate_from_bytes(data_bytes):
candidate_id_decode = int.from_bytes(data_bytes[0:2], 'big')
name_len = data_bytes[2]
name_decode = data_bytes[3:3 + name_len].decode('utf-8')
party_index = 3 + name_len
party_len = data_bytes[party_index]
party_start = party_index + 1
party_decode = data_bytes[party_start:party_start + party_len].decode('utf-8')
age_index = party_start + party_len
age_decode = data_bytes[age_index]
is_incumbent_decode = data_bytes[age_index + 1] == 1
vote_share_start = age_index + 2
vote_share_decode = struct.unpack('f', data_bytes[vote_share_start:vote_share_start + 4])[0]
return (candidate_id_decode, name_decode, party_decode, age_decode, is_incumbent_decode, vote_share_decode)
#Part C: Working with Bytearray
def calculate_total_votes(self):
return sum(self.votes)
def update_votes(self, constituency_index, new_votes):
if 0 <= constituency_index < len(self.votes):
self.votes[constituency_index] = new_votes
else:
print("Invalid constituency index")
# Part D: Simulation
# Create State Election Commission
sec = StateElectionCommission("India", 28, "Punjab", 5)
sec.display_info()
sec.state_display_info()
# Create Candidates
c1 = Candidate(1, "Aman", "Party A", 45, True, 0.0)
c2 = Candidate(2, "Baljit", "Party B", 50, False, 0.0)
c3 = Candidate(3, "Charan", "Party C", 39, False, 0.0)
# Encode and Decode to verify correctness
for cand in [c1, c2, c3]:
encoded = cand.encode_candidate_to_bytes()
decoded = Candidate.decode_candidate_from_bytes(encoded)
print(f"Encoded & Decoded Candidate: {decoded}")
# Simulate vote casting in 5 constituencies
c1.votes = bytearray([120, 200, 75, 90, 150])
c2.votes = bytearray([100, 180, 120, 130, 140])
c3.votes = bytearray([90, 150, 110, 160, 130])
# Display Final Results
print("\nFinal Results:")
results = {}
for cand in [c1, c2, c3]:
total = cand.calculate_total_votes()
results[cand.candidate_id] = total
print(f"{cand.name} ({cand.party}) - Total Votes: {total}")
# Determine Winner
winner_id = max(results, key=results.get)
winner = {c1.candidate_id: c1, c2.candidate_id: c2, c3.candidate_id: c3}[winner_id]
print(f"\nWinner: {winner.name} from {winner.party} with {results[winner_id]} votes!")