Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with this python question Part I: Run-length Encoding (5 points) Run-length encoding is a simple compression scheme best used when a data-set consists

need help with this python question

Part I: Run-length Encoding (5 points)

Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 As. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded $K13BCC$D15$K5MNUUU$G5 assuming in this case that $ is the flag character.

For the sake of this problem we will assume that the input strings to be encoded contain only uppercase letters from the Latin alphabet and that no run is longer than 99 characters long. Flag characters will be chosen only from the set {#, $, &, *}. Note that single letters (M), runs of two letters (CC), and runs of three letters (UUU) are not encoded, as doing so does not save memory or actually compress the data. Do you see why that is the case? Write the function rle() that takes a non-empty string to encode and a character to use as the flag character. The function returns the run-length encoded argument string. If the string to encode contains any characters except uppercase letters, the function should return the string ERROR (without the quotation marks). If the flag character is not one of the symbols in the set {#, $, &, *}, the function should return the string ERROR. Consider using a while-loop instead of a for-loop to iterate over the string. Start with an empty string (which will eventually contain the returned result) and keep a counter for the number of identical characters you find in each run. When a run ends, append (i) the flag character, (ii) the count, and (iii) the character itself to the result. Remember that only runs of length 4 or greater should be encoded; single characters, pairs and triples should simply be appended to the result.

class Card: suit_sym = {0: '\u2663', 1: '\u2666', 2: '\u2665', 3: '\u2660'} rank_sym = {0: '2', 1: '3', 2: '4', 3: '5', 4: '6', 5: '7', 6: '8', 7: '9', 8: '10', 9: 'J', 10: 'Q', 11: 'K', 12: 'A'} def __init__(self, n): self._id = n def rank(self): return self._id % 13 def suit(self): return self._id // 13 def __repr__(self): return Card.rank_sym[self.rank()] + Card.suit_sym[self.suit()] def __lt__(self, other): return self._id < other._id def __eq__(self, other): return self._id == other._id def rle(string, flag): #this is the function. return None 

#here are some testing examples:

Examples: Function Call Return Value

rle(GGGGG, #) #G5

rle(XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK, *) XYZ*A6GGT*C6TTT*A14KK

rle(ABCCDEFGGG, $) ABCCDEFGGG

rle(ABCCDEFGGGG, $) ABCCDEF$G4

rle(XYZAAAAAAGGTCCCCCCTTTaAAAAAAAAAAAAAKK, *) ERROR

rle(XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK, !) 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

Recommended Textbook for

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions