Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions project_enigma/decoding_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
def decode_measurements(encoded_string: str) -> list[int]:
"""This function decodes an encoded string into a list of integers.
RULE 1: A generic logic should be implemented without handling edge cases using if statements for specific inputs.
RULE 2: The generic logic should handle all inputs and generate the expected outputs.
"""This function decodes an encoded string into a list of integers.

Args:
encoded_string (str): The encoded string to decode.
RULE 1: A generic logic should be implemented without handling edge cases using if statements for specific inputs.
RULE 2: The generic logic should handle all inputs and generate the expected outputs.
"""

Returns:
list[int]: The list of decoded integers.
"""
pass # Remove this pass and place your logic here to decode the string into a list of integers based on the specified encoding rules.
return [] # Placeholder return statement; replace with actual decoding logic.
def read_number(s: str, i: int) -> tuple[int, int]:
# A number is a sequence of 'z' (each = 26) followed by a terminating char
total = 0

# accumulate z's
while i < len(s) and s[i] == 'z':
total += 26
i += 1

# terminating character
if i < len(s):
c = s[i]
if 'a' <= c <= 'y':
total += ord(c) - ord('a') + 1
# if '_' or anything else → treated as 0
i += 1

return total, i

def decode_package(s: str) -> list[int]:
values: list[int] = []
i = 0
n = len(s)

while i < n:
count, i = read_number(s, i)

if count == 0:
values.append(0)
break

total = 0
for _ in range(count):
if i >= n:
break
value, i = read_number(s, i)
total += value

values.append(total)

return values

result: list[int] = []
for package in encoded_string.split(' '):
if package == "":
continue
result.extend(decode_package(package))

return result


if __name__ == "__main__":
Expand Down Expand Up @@ -38,3 +81,6 @@ def decode_measurements(encoded_string: str) -> list[int]:
result = decode_measurements(encoded)
status = "PASS" if result == expected else "FAIL"
print(f"{status}: decode_measurements({encoded!r}) = {result} (expected {expected})")