Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions project_enigma/assumptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The Detective's Log: Reverse-Engineered Encodings

Through systematic breakdown of the input/output mappings against the initial brief, several implicit, "hidden rules" were uncovered that deviate from standard serialization formats:

### 1. Count Presentation Rule
The original requirement notes that a "number" dictates the measurement count. The test suite reveals that this count is **not** an ASCII digit (`1`, `2`, `3`), but is instead encoded using the exact same alphabetical variable-length format as the measurements themselves (`a` = 1, `b` = 2, etc.).

### 2. Inner-Cycle Metric Aggregation
When a cycle indicates a count higher than 1 (e.g., `b` = 2 or `c` = 3), the decoded output does not append distinct elements to the final list. Instead, the parser absorbs the assigned number of parameters and **sums them together** to generate one single metric block integer per cycle (e.g., `"abbcc"` uses a count of 2 to read two individual `c` values, outputting $3 + 3 = 6$).

### 3. Early Packet Termination and Abort Command (`_`)
* The underscore (`_`) mathematically processes as `0`.
* If a packet packet-stream starts with `0` (or encounters a count evaluating to `0`), it acts as an **abort condition**. The parser logs a `0` value for that sequence and halts the loop execution for the remainder of that packet substring, effectively ignoring trailing unparsed buffer garbage (e.g., `"_zzzb"` $\rightarrow$ `[0]`).

### 4. Spacing Delimiters
Spaces within strings act as hard package stream packet frame boundaries. Input streams containing `" "` are isolated into individual strings and parsed through independent state cycles, appending tracking elements linearly (e.g., `"_ _"` $\rightarrow$ `[0, 0]`).
17 changes: 17 additions & 0 deletions project_enigma/clarifications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# The Interrogation: Technical Alignment Questions

Dear Client,

We hope you had an excellent retreat! While implementing the core decoding firmware, we noticed several contradictions between the initial formatting text guidelines and your validation suite. To ensure absolute data reliability in a production ecosystem, please clarify the following ambiguities:

### 1. Architectural Overlap of Concatenated Streams
* **The Conflict:** The specification details that a package consists of a single sequence count followed by values. However, tests like `"abcdabcdab"` demonstrate that when a cycle concludes, a completely new nested count prefix (`c`) begins sequentially inline without any separator.
* **Production Proposal:** In a high-noise sensor environment, dropping a single letter would offset the index and corrupt every downstream calculation. We strongly recommend shifting from a continuous stream to using structural delimiters (e.g., `,` or `|`) between data frames to guarantee message isolation.

### 2. Handling of Truncated Stream Buffers
* **The Conflict:** In `"abcdabcdab"`, the final loop opens a measurement frame with a count of `a` (1), but the string completely terminates before a trailing variable value is provided. Currently, our generic algorithm handles this gracefully by dropping the incomplete frame block.
* **Production Proposal:** For live production, should truncated frames trigger a telemetry warning flag, or should they zero-fill missing data to prevent loss of context?

### 3. Intent behind Null Trailing Accumulators
* **The Conflict:** In `"za_a_a_a...a_azaaa"`, multiple sequential structural underscores (`_`) and single `a` flags are combined together into massive, spanning sequence metrics to reach calculated limits like `40`.
* **Production Proposal:** Please clarify if this behavior is a deliberate padding constraint of the hardware's fixed-width output buffers, or if the sensor is streaming live empty tracking heartbeats.
93 changes: 93 additions & 0 deletions project_enigma/decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#The Code (decoder.py)

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.

Args:
encoded_string (str): The encoded string to decode.

Returns:
list[int]: The list of decoded integers.
"""
def char_to_val(c: str) -> int:
if c == '_':
return 0
if 'a' <= c <= 'z':
return ord(c) - ord('a') + 1
return 0

packets = encoded_string.split(' ') if ' ' in encoded_string else [encoded_string]
results = []

for packet in packets:
if not packet:
continue

i = 0
n = len(packet)

def parse_next_number(idx: int) -> tuple[int, int]:
if idx >= n:
return 0, idx
val = char_to_val(packet[idx])
start_char = packet[idx]
idx += 1
if start_char == 'z':
while idx < n and packet[idx] == 'z':
val += 26
idx += 1
if idx < n:
val += char_to_val(packet[idx])
idx += 1
return val, idx

while i < n:
count, i = parse_next_number(i)
if count == 0:
results.append(0)
break

cycle_sum = 0
values_read = 0
for _ in range(count):
if i >= n:
break
val, i = parse_next_number(i)
cycle_sum += val
values_read += 1

if values_read > 0 or count > 0:
results.append(cycle_sum)

return results


if __name__ == "__main__":
test_cases = [
("aa", [1]),
("abbcc", [2, 6]),
("dz_a_aazzaaa", [28, 53, 1]),
("a_", [0]),
("abcdabcdab", [2, 7, 7]),
("abcdabcdab_", [2, 7, 7, 0]),
("zdaaaaaaaabaaaaaaaabaaaaaaaabbaa", [34]),
("zza_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_", [26]),
("za_a_a_a_a_a_a_a_a_a_a_a_a_azaaa", [40, 1]),
("_", [0]),
("_ad", [0]),
("a_", [0]),
("_zzzb", [0]),
("__", [0]),
("", []),
("_ _", [0, 0]),
("aab___", [1, 0, 0]),
]

for encoded, expected in test_cases:
result = decode_measurements(encoded)
status = "PASS" if result == expected else "FAIL"
print(f"{status}: decode_measurements({encoded!r}) = {result} (expected {expected})")


40 changes: 0 additions & 40 deletions project_enigma/decoding_utils.py

This file was deleted.