Solutions will be available when this assignment is resolved, or after a few failing attempts.

Encoding assignments

IMPORTANT: These assignments will be evaluated using Python 2

For this assignment you'll have to implement the following functions:


utf8_encode receives a unicode string and returns a bytes string encoded using UTF-8. Example:

assert utf8_encode(u'Düsseldorf') == 'D\xc3\xbcsseldorf'
assert utf8_encode(u'hello') == 'hello'


ascii_encode receives a unicode string and returns a bytes string encoded using ASCII. If the unicode string can't be completely encoded, None should be returned.

assert ascii_encode(u'hello') == 'hello'
assert ascii_encode(u'Hello Düsseldorf') == None


is_ascii_encoded receives a bytes string and returns True if the bytes could have been encoded[1] using ASCII. Example:

ascii_str = u'hello'.encode('ascii')
utf8_str = u'Düsseldorf'.encode('utf-8')

assert is_ascii_encoded(ascii_str) is True
assert is_ascii_encoded(utf8_str) is False

[1] You can never be 100% sure of the encoding of a file. Maybe you can decode it using ASCII, but the original encoding could have possibly been different. Example:

# UTF-8 is an ASCII compatible encoding.
u"hello".encode("utf-8").decode('ascii')  # u'hello'

Test Cases


def utf8_encode(unicode_str): pass def ascii_encode(unicode_str): pass def is_ascii_encoded(bytes_str): pass