easy +8 pts

Hex encode decode

Implement functions to convert between hex strings and integer byte values.

Write two functions: 1. `hex_encode(data: list[int]) -> str` — takes a list of integers (each in the range 0–255) and returns a lowercase hex string where each integer is represented by exactly two hexadecimal digits. For example, `[255, 0, 16]` becomes `'ff0010'`. 2. `hex_decode(hex_string: str) -> list[int]` — takes a non-empty string of hexadecimal digits (case-insensitive, even length) and returns the list of byte values decoded from it. For example, `'FF0010'` becomes `[255, 0, 16]`. Assumptions: - Input integers in `hex_encode` are always within 0–255 inclusive. - Input string in `hex_decode` is always a valid hex string with even length and at least one character. - Handle both uppercase and lowercase digits in the decode function. Do not use built-in conversion methods like `bytes.fromhex` or `binascii` — implement the conversion manually using dictionaries or arithmetic.

Constraints

- `len(data)` between 1 and 1000 for `hex_encode`. - Length of `hex_string` between 2 and 2000 for `hex_decode`. - Expected time complexity: O(n), where n is the number of input items.

Example

>>> hex_encode([255, 0, 16])
'ff0010'
>>> hex_encode([1, 2, 15])
'01020f'
>>> hex_decode('ff0010')
[255, 0, 16]
>>> hex_decode('FF0010')
[255, 0, 16]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

In hex_encode, format each integer with f"{x:02x}" and join.
In hex_decode, map each hex character to its value using a dictionary or int() with base 16.
Process pairs of characters: for each pair, compute value = first_value * 16 + second_value.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.