Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is what I have done import string def reverse_fun(char_list): i=0 name_key=[] print(translated_string) while i 5.12 LAB: Reverse Only Letters Programming Objectives Given a string

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

This is what I have done

import string def reverse_fun(char_list): i=0 name_key=[] print(translated_string) while i 5.12 LAB: Reverse Only Letters Programming Objectives Given a string S, return the reversed string where all characters that are not a letter stay in the same place, and all letters reverse their positions Example 1: Input: "ab-cd" Output: "dc-ba" Example 2: Input "a-bC-dEf-ghIj" Output: "j-Ih-gfE-dcba" Example 3 Input: "Testing-ICS31UCI.ICS code-Q!" Output: "Qedoles-CII31ecus.cIg-ntse-T!" Note: 1. S.length 100 2. 33c=S[i].ASCI!code 122 3. S doesn't contain\or Technical Details A brief sketch of a working program is described below First, you have to translate the original string into a dictionary, for example, when "abcd is inputted, a helper function def str2dict (input-string): should output {0: "a": 1:-, 2:, 3: 'd') Second, figure out helper function def ischarLetter(input_char):. To simplify implementation, membership operator in' combined with python's built-in list of letters is highly recommended. For example: which characters are not'letters by constructing another import string print ("a" in string.asciiletters) # print out True print("?" in string.asc11-letters) # print out False - To reverse the index for letter characters, iterate through the dictionary and record their original characters (keys) and indexes (values) into two parallel lists. Using the same string as Example 1, the original dictionary representing the string ab-cd' is translated string- (0: "a", 1: "b", 2: "-", 3: "c", 4: "d" } the "value list of letter characters is (See hint 4) letter only values ["a", "b", "c", "d"] - - the "key" list of letter characters is (See hint 4) letter_only_keys-[0, 1, 3, 4] Note that letter_only_values and letter_only_keys are parallel lists. Use list reverse0 method to reverse the index list and construct a letter only dictionary with indexes reversed letter reversed {4:"a", 3: "b", 1: "c", 0: "d") = - Use dictionary update0 method to update original "translated string" dictionary translated_string.update (letter_reversed) # The update() method updates the dictionary with the elements from the another dictionary object or from an iterable of key/value pairs. translated_string becomes translated_string becomes Finally, you have a dictionary keyed by expected output indexes and their original characters associated with in the values. Print the letter- reversed string after sorting the dictionary by "keys Hints 1. to sort a dictionary by key, use following statement ) # try it out in IDLE and sortedbyvalue = sorted (translatedstring. tems(), key-lambda kv: kv see how it works - - - 2. You may need to use str Joino method. Documentation available here: https://docs python.org 3. You may need to use dict.items0 method. Try this for k, v in some_dictionary.itemsO: library stat pes html#stron print (k, v) 4. List comprehension may be a good idea in processing letter_only_keys and letter_only_values 1: Compare output ^ Output differs. See highlights below. Input ab-cd Your output Expected output dc-ba 2: Compare output Output differs. See highlights below. pt a-bc-dEf-ghIj Your output Expected output j-Ih-gfE-dcba 3: Compare output ^ Output differs. See highlights below. Input Testing-ICs31 UCI. ICS code-Q! Your output Expected output Redoles-CI131ecus.cigentse-T

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

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions