-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevelop An Algorithm.py
More file actions
264 lines (203 loc) · 9.08 KB
/
Develop An Algorithm.py
File metadata and controls
264 lines (203 loc) · 9.08 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# ============================================================
# Script: develop_an_algorithm.py
# Author: Joshua Gouvisis
# Date: March 2026
# Certificate: Google Cybersecurity Professional Certificate
# Lab: Develop an Algorithm — User & Device Verification
#
# Description: Builds a step-by-step algorithm in Python to
# verify that a user is approved to access a
# system and that the device they are using is
# the one assigned to them. Culminates in a
# reusable login() function covering all three
# access scenarios.
# ============================================================
# ============================================================
# TASK 1 — Exploring List Indices
#
# Two parallel lists store approved usernames and their
# corresponding device IDs. The element at index n in
# approved_users corresponds to the element at index n
# in approved_devices.
# ============================================================
print("=" * 55)
print("TASK 1 — Exploring List Indices")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "tshah", "sgilmore", "eraab"]
approved_devices = ["8rp2k75", "hl0s5o1", "2ye3lzg", "4n482ts", "a307vir"]
print(approved_users[0]) # elarson
print(approved_devices[1]) # hl0s5o1
# ============================================================
# TASK 2 — Adding a New User
#
# A new employee joins the organization. Use .append() to
# add their username and device ID to the respective lists.
# ============================================================
print("\n" + "=" * 55)
print("TASK 2 — Adding a New User")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "tshah", "sgilmore", "eraab"]
approved_devices = ["8rp2k75", "hl0s5o1", "2ye3lzg", "4n482ts", "a307vir"]
new_user = "gesparza"
new_device = "3rcv4w6"
approved_users.append(new_user)
approved_devices.append(new_device)
print(approved_users)
print(approved_devices)
# ============================================================
# TASK 3 — Removing a Departed User
#
# An employee has left the team. Use .remove() to delete
# their username and device ID from both lists.
# ============================================================
print("\n" + "=" * 55)
print("TASK 3 — Removing a Departed User")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "tshah", "sgilmore", "eraab", "gesparza"]
approved_devices = ["8rp2k75", "hl0s5o1", "2ye3lzg", "4n482ts", "a307vir", "3rcv4w6"]
removed_user = "tshah"
removed_device = "2ye3lzg"
approved_users.remove(removed_user)
approved_devices.remove(removed_device)
print(approved_users)
print(approved_devices)
# ============================================================
# TASK 4 — Checking User Approval
#
# Conditional statement that checks whether a given username
# exists in approved_users and displays an appropriate message.
# ============================================================
print("\n" + "=" * 55)
print("TASK 4 — Checking User Approval")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "sgilmore", "eraab", "gesparza"]
approved_devices = ["8rp2k75", "hl0s5o1", "4n482ts", "a307vir", "3rcv4w6"]
username = "sgilmore"
if username in approved_users:
print("The user", username, "is approved to access the system.")
else:
print("The user", username, "is not approved to access the system.")
# ============================================================
# TASK 5 — Finding a User's Index
#
# Use .index() to find the position of username within
# approved_users and store it in a variable named ind.
# ============================================================
print("\n" + "=" * 55)
print("TASK 5 — Finding a User's Index")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "sgilmore", "eraab", "gesparza"]
approved_devices = ["8rp2k75", "hl0s5o1", "4n482ts", "a307vir", "3rcv4w6"]
username = "sgilmore"
ind = approved_users.index(username)
print(ind) # Output: 2
# ============================================================
# TASK 6 — Cross-Referencing the Device List
#
# Use the index from Task 5 to retrieve the corresponding
# device ID from approved_devices, demonstrating how the
# two parallel lists can be cross-referenced.
# ============================================================
print("\n" + "=" * 55)
print("TASK 6 — Cross-Referencing the Device List")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "sgilmore", "eraab", "gesparza"]
approved_devices = ["8rp2k75", "hl0s5o1", "4n482ts", "a307vir", "3rcv4w6"]
username = "sgilmore"
ind = approved_users.index(username)
print(approved_devices[ind]) # Output: 4n482ts
# ============================================================
# TASK 7 — Verifying Username and Device Together
#
# Conditional using the "and" logical operator to verify
# that the username is approved AND the device ID matches
# the one assigned to that user.
# ============================================================
print("\n" + "=" * 55)
print("TASK 7 — Verifying Username and Device Together")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "sgilmore", "eraab", "gesparza"]
approved_devices = ["8rp2k75", "hl0s5o1", "4n482ts", "a307vir", "3rcv4w6"]
username = "sgilmore"
device_id = "4n482ts"
ind = approved_users.index(username)
if username in approved_users and approved_devices[ind] == device_id:
print("The username", username, "is approved to access the system.")
print(device_id, "is the assigned device for", username)
# ============================================================
# TASK 8 — Handling a Device Mismatch
#
# Extends the conditional with an elif branch to handle the
# case where the username is approved but the device ID
# does not match the assigned one.
# ============================================================
print("\n" + "=" * 55)
print("TASK 8 — Handling a Device Mismatch")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "sgilmore", "eraab", "gesparza"]
approved_devices = ["8rp2k75", "hl0s5o1", "4n482ts", "a307vir", "3rcv4w6"]
username = "sgilmore"
device_id = "8rp2k75" # intentionally incorrect device
ind = approved_users.index(username)
if username in approved_users and device_id == approved_devices[ind]:
print("The user", username, "is approved to access the system.")
print(device_id, "is the assigned device for", username)
elif username in approved_users and approved_devices[ind] != device_id:
print("The user", username, "is approved to access the system.")
print(device_id, "is not their assigned device for", username)
# ============================================================
# TASK 9 — The Complete login() Function
#
# All previous logic combined into a reusable login()
# function. A nested conditional handles all three cases:
# 1. Unapproved user
# 2. Approved user with correct device
# 3. Approved user with incorrect device
# ============================================================
print("\n" + "=" * 55)
print("TASK 9 — The Complete login() Function")
print("=" * 55)
approved_users = ["elarson", "bmoreno", "sgilmore", "eraab", "gesparza"]
approved_devices = ["8rp2k75", "hl0s5o1", "4n482ts", "a307vir", "3rcv4w6"]
def login(username, device_id):
"""
Verifies whether a user is approved to access the system
and whether they are using their assigned device.
Args:
username (str): The username attempting to log in.
device_id (str): The device ID the user is presenting.
"""
if username in approved_users:
print("The user", username, "is approved to access the system.")
ind = approved_users.index(username)
if device_id == approved_devices[ind]:
print(device_id, "is the assigned device for", username)
else:
print(device_id, "is not their assigned device.")
else:
print("The username", username, "is not approved to access the system.")
# --- Example calls ---
print("\n--- Test Case 1: Typo in username (not approved) ---")
login("elarsen", "8rp2k75") # typo — not in approved_users
print("\n--- Test Case 2: Approved user, wrong device ---")
login("bmoreno", "4n482ts") # approved user, wrong device
print("\n--- Test Case 3: Approved user, wrong device ---")
login("eraab", "hl0s5o1") # approved user, wrong device
# ============================================================
# CONCLUSION — Key Takeaways
#
# Parallel lists: Two synced lists act as a lightweight
# key-value store for user-device pairs.
# List mutation: .append() and .remove() keep both
# lists in sync at runtime.
# Conditional logic: "in", comparison operators, and "and"
# enable multi-factor access checks.
# Nested conditionals: Separate device-match and mismatch
# scenarios without repeating the
# username check.
# Encapsulation: login() makes the algorithm reusable,
# testable, and easy to extend.
# ============================================================
print("\n" + "=" * 55)
print("All 9 tasks completed successfully.")
print("=" * 55)