medium +20 pts

Base64 Encode Decode

Implement your own Base64 encoding and decoding without external libraries.

Implement two functions: `base64_encode(data: str) -> str` and `base64_decode(encoded: str) -> str` that perform Base64 encoding and decoding as described in RFC 4648. The input to `base64_encode` is a Python string (ASCII characters only). The output is a Base64-encoded string with padding (`=`) as needed. The input to `base64_decode` is a valid Base64 string (with or without padding) and the output is the decoded ASCII string. Your implementation must not use the built-in `base64` module or any third-party libraries. You can use standard string and integer operations. Base64 alphabet: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/` Encoding rules: - Process the input string in 3-byte groups. - For each group, convert the three bytes (ASCII values) into a single 24-bit number. - Split this 24-bit number into four 6-bit indices and map each index to the Base64 alphabet character. - If the last group has only 1 byte, encode it as two Base64 characters and add two `=` characters. If it has 2 bytes, encode it as three Base64 characters and add one `=`. Decoding rules: - Ignore any `=` padding characters at the end. - Process the remaining characters in groups of 4. - Map each Base64 character to its 6-bit value (A–Z: 0–25, a–z: 26–51, 0–9: 52–61, `+`: 62, `/`: 63). - Combine four 6-bit values into a 24-bit number. - Extract bytes from the 24-bit number as in encoding, but only output as many bytes as were originally present. The number of original bytes can be inferred from the number of padding characters (0 padding -> 3 bytes, 1 padding -> 2 bytes, 2 padding -> 1 byte). If there is no padding, assume the last group was full 3 bytes. Edge cases: The empty string should encode to an empty string and decode to an empty string. Your decoder should handle valid input without padding as well (e.g., `TWFu` decodes to `Man`).

Constraints

- Input strings contain only ASCII characters (for encoding). - Decoder input is always a valid Base64 string with correct padding (or no padding when length is a multiple of 4). - Complexity: O(n) time and O(n) space where n is the input length. - Do not use the `base64` module.

Example

>>> base64_encode("Man")
'TWFu'
>>> base64_encode("Ma")
'TWE='
>>> base64_encode("M")
'TQ=='
>>> base64_decode("TWFu")
'Man'
>>> base64_decode("TWE=")
'Ma'
>>> base64_decode("TQ==")
'M'
>>> base64_encode("")
''
>>> base64_decode("")
''
20 points ~30 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `ord()` to get ASCII values and `chr()` to convert back.
For encoding, process in chunks of 3 characters and use bit shifts to combine and split 24-bit groups.
For decoding, build a mapping from Base64 characters to their integer values.
Padding: encode adds `=` for missing bytes; decode can calculate output length from padding.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.