please show step and code! python question! Lab Question 1 (Shortening sentences
ID: 3665694 • Letter: P
Question
please show step and code!
python question!
Lab Question 1 (Shortening sentences) [4 points]: To speed up texting your friends, you are thinking of leaving out all vowels. a. Write a Python function shorten (s) that takes an arbitrary string and removes all uppercase and lowercase vowels: shorten ("What is the airspeed velocity of an unladen swallow? Wht as th r spa v cty f n nldn SW. ll shorten shorten AEIOU ael. Ou shorten 'xAxExIxoxUYaYeri YoYuY') b. You find out that the resulting words are sometime incomprehensible and like to improve your method by removing only those vowels that directly follow another letter. This means keeping the vowel at the beginning of a word. Write a Python function shortenPlus (s) that takes an arbitrary string and removes some vowels shorten Plus What is the airspeed velocity of an unladen swallow?') Wht is th arspa v loty of an unlan swyllw? shorten Plus shorten Plus Cu 'A' shorten Plus xAxExIxoxUYa Yee Yi YoYuY shorten Plus ('x Ax Ex Ix Ox UY ay eY iY oY uY') x Ax Ex Ix Plus A E I U a e i o u shorten A E I O U a e i o u Place both functions in the file 01 you rmacid.pyExplanation / Answer
code:
vowel="aeiouAEIOU"
def shorten(s):
returnstring=""
for c in s:
if not c in vowel:
returnstring=returnstring + c
return returnstring
def shortenplus(s):
prev=' '
returnstring=""
for c in s:
if not c in vowel or prev==' ':
returnstring=returnstring + c
prev=c
return returnstring
print shortenplus("xAxExIxOxUYaYeYiYoYuY")
print shorten("xAxExIxOxUYaYeYiYoYuY")
Explanation: First define the string "aeiouAEIOU" to check whether the given character is vowel or not
For the function shorten just define the string to be returned (named returnstring)and travese the input string, if the character in not vowel then only put character into the "returnstring" otherwise skip that character.
At the end just return the string "returnstring",
For the function shorten just define the string to be returned (named returnstring)and travese the input string, if the character in not vowel then simply put character into the "returnstring" or if the character is not vowel then check the preceding character(for that i took one variable "prev" to store previous character) if it is space then put the character into the "returnstring" variable otherwise skip the character.
At the end just return the string "returnstring",
if you have any doubt then please comment below and plese don't forget to give me the feedback
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.