Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON 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

PYTHON

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.

image text in transcribed

Part II: Run-length Decoding (5 points) Now write the function rld() that performs a run-length decoding of a string that was encoded using the rle() function described above. The functions arguments are a non-empty string to decode and the character to use as the flag character. The input string consists of a properly-formatted run-length encoding, and the flag character is drawn from the set {#, $, &, *}. You may assume that the input string and the flag character are both valid.

image text in transcribed

Part III: The Look-and-Say Sequence (5 points) In mathematics, the look-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, . . . To generate a member of the sequence from the previous member, read off the digits of the previous member, counting the number of digits in groups of the same digit. For example: 1 is read off as one 1 or 11 11 is read off as two 1s or 21 21 is read off as one 2, then one 1 or 1211 1211 is read off as one 1, one 2, then two 1s or 111221 111221 is read off as three 1s, two 2s, then one 1 or 312211 Write the function look say() that takes an integer giving which term of the look-and-say sequence we want, starting from 0, and returns a string containing that term. Term #0 is 1, term #1 is 11, term #2 is 21, and so on. Your function must return the correct string for any non-negative integer argument. No matter how long the numbers become, the only digits that will appear are 1, 2 and 3.

image text in transcribed

Part IV: From Human-readable Time Intervals to Date Components (5 points) Write the function readable interval() that takes a string consisting of a human-readable time interval and converts it into a list of six integers, given in this order: [# of years, # of months, # of days, # of hours, # of minutes, # of seconds]. The input values stored within the string can be quite scrambled and consist of multiple, repeated time components (days, months, etc.), as shown in the examples below. If a time component appears more than once, the values are be added. The time units might be given in singular or plural form (e.g., month and months are both valid). There will always be at least (but not necessarily exactly) one space between the tokens (i.e., components) of the string.

image text in transcribed

Part V: Poker Hands (5 points) Write the function poker hand() that takes a list of exactly five Card objects as an argument, analyzes the list, and returns one of the following strings that describes the hand: Straight (five cards when rearranged will form a run of five consecutive cards) Four of a kind (four or five cards of the same rank) Three of a kind (exactly three cards of the same rank) One pair (at least one pair of the same rank, but not four cards of the same rank) High card (no cards of identical rank that also do not form a straight) The string High card is returned when no pair, triple or quadruple is found in the list of cards. The returned string may be capitalized any way you wish, but no characters (including spaces) may otherwise be added or changed. If two pairs are present in the hand, the function should simply return One pair. If a pair of one rank and a three-of-a-kind of another rank are present in the hand, the function should simply return Three of a kind. The Card class that you will find in homework3.py is the same class we studied in lecture. Use it to complete this part of the assignment. You may add additional instance variables or methods to the class if you want.

image text in transcribed

Examples: Function Call Return Value rle GGGGG ft') G5 rle XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK', k 'XYZ A6GGT C6TTT Al 4KK' rle ABCCDEF GGG s') ABCCDEF GGG' ABCCDEF SG4 rle ABCCDEF GGGG', rle XYZAAAAAAGGTCCCCCCTTTaAAAAAAAAAAAAAKK', ERROR' rle ('XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK', 'ERROR' Note that the quotation marks displayed in the return values are there to emphasize that the return values are strings. You should not add quotation marks to your return values. Examples: Function Call Return Value rle GGGGG ft') G5 rle XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK', k 'XYZ A6GGT C6TTT Al 4KK' rle ABCCDEF GGG s') ABCCDEF GGG' ABCCDEF SG4 rle ABCCDEF GGGG', rle XYZAAAAAAGGTCCCCCCTTTaAAAAAAAAAAAAAKK', ERROR' rle ('XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK', 'ERROR' Note that the quotation marks displayed in the return values are there to emphasize that the return values are strings. You should not add quotation marks to your return values

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions