Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

pls show me the code. If you don't know how to do this, don't copy other people's wrong answers. Thanks class BitList: def decode....... .

pls show me the code. If you don't know how to do this, don't copy other people's wrong answers. Thanks

class BitList:

def decode.......

image text in transcribedimage text in transcribed

. decode(encoding=' utf-8') This method has a single parameter, the encoding (a str ), and it returns a string: 1. the encoding can only be us-ascii or utf-8 - the default encoding should be utf-8 - consequently, decode can be called with no arguments 2. it returns a string An instance of BitList can be decoded. In the example below, the bits are treated as 7 -bit ASCII :): bits = BitList.from_ints (1,1,0,0,0,0,1) ch = bits.decode('us-ascii') print(ch) \# a This should work if there are multiple characters encoded: new_bit_list = BitList('11000011000001') s = new_bit_list.decode('us-ascii') print(s) \# aA One way to implement this is to: 1. calculate the decimal value from the bits 2. convert the decimal value into a character with chr This is a little tricky, as chunking bits into equivalent lengths isn't adequate: - utf-8 is variable length - the "leading byte" must be examined to determine how many bytes total are required - for example, encountering the byte 11101010 means that there should be three bytes total (there are 31 's) - the continuation bits are all prefixed with 10 - see the slidesotebook on unicode for more details b= BitList(' 11110000100111111001100010000010111000101000001010101100) s=b decode('utf-8') print(s) \#@) Io deal with decoding errors: - if the encoding passed into decode is not us-ascii or utf-8, immediately raise a valueError, instantiated with a message stating that the encoding is not supported - if the leading byte is invalid (see wikipedia's table for for valid bit patterns to start a leading byte), cause a runtime error by throwing an DecodeError exception (the value passed in to the construction of this error should describe that the error was due to an invalid leading byte) - if a continuation byte is invalid (it doesn't start with 10 , for exampe) cause a runtime error by throwing an DecodeError exception (the value passed in to the construction of this error should describe that the error was due to an invalid continuation byte) \# invalid continuation byte b = BitList(' 11110000000111111001100010000010 ) try: except DecodeError: print('error!') \# invalid leading byte b = BitList('10000011'') try: except DecodeError: print('error!')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions