Using Python3 (Do not use regular expressions, no tuples, no methods of string,
ID: 3858849 • Letter: U
Question
Using Python3 (Do not use regular expressions, no tuples, no methods of string, or def.) .
Write a program called DeleteRuns. this is what the program does: ask user for a string (see prompt in examples below) the input can be any string. Examples: 123 1222223 122223222232222522226 axxxbyybyy 1234554321 hello 1km 1mi 1.5km 23.89mi now is the time if user enters the empty string (i.e. just press enter) the program ends and prints: goodBye! DeleteRuns is supposed to delete all adjacent repeated characters. For example: if input is: 123333 the output is 12 input: 1222342 output: 1342 input: 122233342 output: 142 if after deleting all repeated characters the resulting string has adjacent repeated characters those must be deleted too. For example: input: 1233324 output: 14 after deleting the run of 3s you would get: 1224, which itself contains a run of 2s. The final result must not have any sequence of adjacent repeated characters. Another example: input: a1234554321b output: ab Examples % DeleteRuns.py enter a string: aba aba enter a string: abcd abcd enter a string: abcc ab enter a string: abccc ab enter a string: aabc bc enter a string: aaabc bc enter a string: abbc ac enter a string: abbbc ac enter a string: aabcc b enter a string: aabbcc enter a string: xabcdeedcbay xy enter a string: goodBye!
Explanation / Answer
For a given problem statement, we have to have the functions to avoid repeated code.
Find below script in python 3 as a solution for given program.
DeleteRuns.py
# Utility functions
def TakeList(string):
x = []
for i in string:
x.append(i)
return x
def TakeString(x):
return ''.join(x)
# Recursively remove adjacent duplicates from str and returns new string.
# Previous_removed is a pointer to last removed character.
def RemoveAdjacent(string, Previous_removed):
# If length of string is 1 or 0
if len(string) == 0 or len(string) == 1:
return string
# Remove leftmost same characters and recur for remaining
# string
if string[0] == string[1]:
Previous_removed = ord(string[0])
while len(string) > 1 and string[0] == string[1]:
string = string[1:]
string = string[1:]
return RemoveAdjacent(string, Previous_removed)
# At this point, the first character is definitely different
# from its adjacent. Ignore first character and recursively
# remove characters from remaining string
rem_str = RemoveAdjacent(string[1:], Previous_removed)
# Check if the first character of the rem_string matches
# with the first character of the original string
if len(rem_str) != 0 and rem_str[0] == string[0]:
Previous_removed = ord(string[0])
return (rem_str[1:])
# If remaining string becomes empty and last removed character
# is same as first character of original string.
# This is needed for a string like "acbbcddc"
if len(rem_str) == 0 and Previous_removed == ord(string[0]):
return rem_str
# If the two first characters of str and rem_str don't match,
# append first character of str before the first character of
# rem_str.
return ([string[0]] + rem_str)
def remove(string):
Previous_removed = 0
return TakeString(RemoveAdjacent(TakeList(string), Previous_removed))
# Call Function recursively ubless hit ENTER
while True:
i = input("Input text & hit ENTER to quit: ")
if not i:
print("GoodBye")
break
print (remove(i))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.