Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In python My code: import random def scramble(word) : This is the function to scramble 2 characters. It take a single paramater word l=len(word)
In python
My code:
import random def scramble(word) : """This is the function to scramble 2 characters. It take a single paramater word """ l=len(word) p1=0 p2=0 while (p1==p2) : """I added this while loop just so that if the two characters to be swtiched cannot be the same""" """p1 and p2 are the 2 postion of characters to be swithed. randrange will return random values in the given range""" p1=random.randrange(1,l-1) p2=random.randrange(1,l-1) if (word[p1]==word[p2]) : if (l==4) : """If length of word eqal 4 are two and the two characters in between are same then this loop will be infinite; So to avoid this case i added this condition """ break p1=p2 s="" """The following function will create a new scrambled string (append the characters from word to an empty string excpet for the selected position in that case we switch the positions""" for i in range(l) : if (i==p1) : s+=word[p2] elif (i==p2) : s+=word[p1] else : s+=word[i] return s str=input("Enter sentence/phrase :") l=str.split(' ') """str contains the input string and split function is used to split every word into a list of words in list l""" newstr="" """Below code is to call the scramble function for every word whose length is greater than 3 (if length of 3 and below cannot be scrambled) and append the words to a string and print it""" for i in l : if len(i)>3 : newstr+=scramble(i)+ " " else : newstr+=i+" " print(newstr)
Task:
It can only prompt the user one time, how to make it prompt 3 times or more to ask the user enter sentence/phrases?
It is a well-known phenomenon that most people are easily able to read a text whose words have only two characters flipped, provided the first and last letter of each word are Not changed. For example, I dnot gvie a dman for a man taht can olny sepll a wrod one way. (Mrak Taiwn) Write a function scramble(word) that constructs a scrambled version of a given word, randomly flipping two characters other than the first and last one. Then write a program that reads a sentence/phrase and prints the scrambled words. Use these sentences for your test cases: 1. You can't have the cake and eat it too. 2. A gentle answer turns away wrath, but a harsh word stirs up anger. 3. The love of god does not find, but creates, that which is pleasing to it. The love of man comes into being through that which is pleasing to it. (Martin Luther)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