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'
import unittest

class EncodingsTestCase(unittest.TestCase):
    def test_utf8_encode(self):
        self.assertEqual(utf8_encode(u'Düsseldorf'), 'D\xc3\xbcsseldorf')
        self.assertEqual(utf8_encode(u'hello'), 'hello')

    def test_ascii_encode(self):
        self.assertEqual(ascii_encode(u'hello'), 'hello')
        self.assertEqual(ascii_encode(u'Hello Düsseldorf'), None)

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