diff --git a/example_tasks/from_alhawraa/multiplication_table.py b/example_tasks/from_alhawraa/multiplication_table.py new file mode 100644 index 0000000..3797cca --- /dev/null +++ b/example_tasks/from_alhawraa/multiplication_table.py @@ -0,0 +1,40 @@ +"""Multiplication table utility for from_alhawraa.""" + +from __future__ import annotations + + +def get_int_input(prompt: str, min_value: int | None = None) -> int: + while True: + raw_value = input(prompt).strip() + if not raw_value: + print("Input cannot be empty. Please enter a number.") + continue + + if not raw_value.lstrip("+-").isdigit(): + print("Please enter a valid integer.") + continue + + value = int(raw_value) + if min_value is not None and value < min_value: + print(f"Please enter a number greater than or equal to {min_value}.") + continue + + return value + + +def format_multiplication_table(number: int, limit: int) -> list[str]: + return [f"{number} x {i} = {number * i}" for i in range(1, limit + 1)] + + +def main() -> None: + print("=== Multiplication Table Utility ===") + number = get_int_input("Enter the number: ") + limit = get_int_input("Enter the limit: ", min_value=1) + + print("\nResult:") + for line in format_multiplication_table(number, limit): + print(line) + + +if __name__ == "__main__": + main() diff --git a/main.py b/main.py index 6e313b6..12f399c 100644 --- a/main.py +++ b/main.py @@ -35,4 +35,5 @@ async def post_feedback(request: fastapi.Request): if __name__ == "__main__": import uvicorn - uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file + uvicorn.run(app, host="0.0.0.0", port=8000) + \ No newline at end of file diff --git a/project_enigma/decoding_utils.py b/project_enigma/decoding_utils.py index 775b218..9c06563 100644 --- a/project_enigma/decoding_utils.py +++ b/project_enigma/decoding_utils.py @@ -1,16 +1,82 @@ 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.""" + results = [] + i = 0 + n = len(encoded_string) - Args: - encoded_string (str): The encoded string to decode. + def read_value_token(): + nonlocal i + if i >= n or encoded_string[i] == ' ': + return None + c = encoded_string[i] + if c == '_': + i += 1 + return 0 + val = 0 + while i < n: + ch = encoded_string[i] + if ch == 'z': + val += 26 + i += 1 + elif ch == '_': + i += 1 + return val + elif ch == ' ': + return val + else: + val += ord(ch) - ord('a') + 1 + i += 1 + return val + return val - 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. + while i < n: + c = encoded_string[i] + + if c == ' ': + i += 1 + continue + + if c == '_': + while i < n and encoded_string[i] != ' ': + i += 1 + results.append(0) + continue + + count = 0 + zero_cycle = False + while i < n: + ch = encoded_string[i] + if ch == ' ': + break + if ch == '_': + while i < n and encoded_string[i] != ' ': + i += 1 + results.append(0) + zero_cycle = True + break + if ch == 'z': + count += 26 + i += 1 + else: + count += ord(ch) - ord('a') + 1 + i += 1 + break + + if zero_cycle: + continue + + total = 0 + for _ in range(count): + if i >= n or encoded_string[i] == ' ': + break + val = read_value_token() + if val is None: + break + total += val + + results.append(total) + + return results if __name__ == "__main__": @@ -37,4 +103,4 @@ def decode_measurements(encoded_string: str) -> list[int]: 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})") + print(f"{status}: decode_measurements({encoded!r}) = {result} (expected {expected})") \ No newline at end of file