Question
I am creating a PYTHON program that asks the user for input string that will convert that string to run-length encoded. I have to use
I am creating a PYTHON program that asks the user for input string that will convert that string to run-length encoded. I have to use a compressed format where a single character has no count. For example, if a user inputs "abbbcc" the output would be "ab3c2" instead of "a1b3c2."
Then, I would have to check if the run-length string contains numbers. If it does, I would have to decode it because it is in RLE format. Strings that have no encoding should be checked before. The decode function is separate from encoding. For example, if a user inputs "a2bc3" the output would be "aabccc". Note: Notice that the 'b' in the example should count as 1 since it is in the right RLE format.
The next step is to enhance the program by making "#" symbol as an escape sequence, indicating that the following number is a number character rather than an encoding count. This will follow the escape sequence definition. Then, a pair of # symbols (##) to indicate a # character. This change should allow any input sequence to be encoded.
***I will be showing the code I have so far below. I need help trying to meet the assigned assignments above. Here is what I know I need help on my code: Code breaks when I input "a2bc2" because the 'b' is not counting as b1. I need help trying to implement that under decode Recognize whether the user's input is to call encode or decode. The code should throw an error when the string input numbers. The implementation with the escape sequence. I need help with that the most. If there's a way you can help me with the program would be AMAZING
"""PROGRAM BELOW""" import sys def get_string_from_user(): print("Enter string of alphabetical characters:") print("(or pressto quit):") user_input = input() if user_input == '': return None else: return user_input def encode_string(user_input): if not user_input: return " " else: last_str = user_input[0] max_index = len(user_input) count = 1 while count < max_index and last_str == user_input[count]: count += 1 if count == 1: result = last_str else: result = last_str + str(count) return result + encode_string(user_input[count:]) def decode_string(user_input): if not user_input: return "" else: char = user_input[0] num = user_input[1] if num.isdigit(): result = char * int(num) else: result = char * int(num) return result + decode_string(user_input[2:]) def encode_or_decode(user_input): ZERO = "0" try: if user_input.isalpha(): print("Encoded format: %s " % encode_string(user_input)) elif user_input.isnumeric(): raise ValueError elif user_input.isalnum() and user_input not in ZERO: print("Decoded Format: " + '' .join(decode_string(user_input)) + ' ') else: raise ValueError except ValueError: if user_input == "": return None else: print(" ValueError: input me be in run-length encode or decoded form ") def main(): try: while True: user_input = get_string_from_user() if user_input == None: break encode_or_decode(user_input) except ValueError: print(" ValueError Information:") print("Error:", sys.exc_info()[1]) print("File: ", sys.exc_info()[2].tb_frame.f_code.co_filename) print("Line: ", sys.exc_info()[2].tb_lineno) main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started