easy +8 pts

Encode Base64 String

Implement a base64 encoder without using the base64 module.

Implement the function `encode_base64(s: str) -> str` that returns the Base64 encoding of the input string `s`. You must implement the encoding algorithm yourself and **must not** use the `base64` module or any other encoding module (e.g., `binascii`). The input string should be encoded to bytes using UTF-8 before encoding to Base64. The output must follow standard Base64 with padding with `=`, using the standard alphabet `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`. For example, `encode_base64('Hello')` should return `'SGVsbG8='`. Your solution will be tested with arbitrary strings, including empty strings and non-ASCII characters (e.g., 'é', '😀'). The tests expect exactly the standard Base64 encoding.

Constraints

Input length: 0 ≤ len(s) ≤ 10,000. The function should handle any Unicode string. Time complexity: O(n), where n is the byte length of the UTF-8 encoding of the input.

Example

>>> encode_base64('Hello')
'SGVsbG8='
>>> encode_base64('Python')
'UHl0aG9u'
>>> encode_base64('')
''
8 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

First convert the input string to bytes using UTF-8 encoding.
Process the bytes in chunks of 3; each chunk yields 4 Base64 characters.
For padding, two bytes left produce one `=` and one byte left produce two `=` at the end.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.