i am having difficulty with how to use index() and len() function to set up a ar
ID: 3727513 • Letter: I
Question
i am having difficulty with how to use index() and len() function to set up a array and search the element that i wanted and how to replace the element to what i want it to be
Internet slang has entered (relatively) widespread usage, evolving from its roots as an obfuscation technique into a form of communication used principally to express dissatisfaction with the performance of a teammate or to caption photographs of domesticated animals. For this assignment, you will write a program for something resembling a 1337 (i.e., leetspeak) translator, that will receive a single sentence as a string argument and produce a string return value for the equivalent sentence in 1337. If you are not familiar with this "language", read more at https://en.wikipedia.org/wiki/Leet. For this assignment, you need to write a series of functions. Download (and rename) the "starting_point for_assignment 5.py" file from cuLearn, and add the required functionality: Write a main function that prompts the user to type a string and then remove all punctuation marks and replace all lowercase letters with their uppercase equivalent (using calls to the functions described below that you will write). Display this text to the user and then ask o ..if the user wants to replace phrases? o ...if the user wants to replace words? o .if the user wants to replace letters and, if so, what letters? Depending upon how the user responds to each question, you would then call some or all of the functions described below (that you will write), printing the result after each step. After printing the final result you must ask the user if they would like to translate another string and, if the answer is yes, loop back to the input prompt. (n.b., Do NOT call the main () function again to achieve this repetition - use a loop.) Write a function to replace at least four different phrases (of two or three words) witlh established acronyms (e.g, replace "BY THE WAY" with "BTW") of your choosing This function must take only the string to be modified as an argument and must return the string with the replacements. You may not use any built-in find, replace, encode, or translate functions, butyou may write your own version of these functions if you wish, and you may use the indexing operator (i.e., the square brackets), the slicing operator (i.e., the colon), the "in" operator, and the len) function.Explanation / Answer
The quesion is asking for python code and hence we can directly use python's "string" library.
For the first part of the question, you can use the below piece of code to take the
input from the user, remove all punctuation marks and replace all lowercase letters
with uppercase equivalents.
________________________________________________________________________________________________________________________
#Getting the input from the user
userInput = raw_input("Please enter a string to modify")
#Replacing all punctuations
re.sub('[%s]' % re.escape(string.punctuation), '', s) #For this you will have to import the libraries 'string' and 're'
#Changing the user input to uppercase
uppercaseUserInput = userInput.upper()
print(uppercaseUserInput)
________________________________________________________________________________________________________________________
For the second part of the question, the below code can be used :
________________________________________________________________________________________________________________________
#Take the user's replace phrase like this
phraseReplace = raw_input("Enter phrase to be replaced")
newPhrase = raw_input("Enter new phrase to replace ")
#replace from original string and print
print uppercaseUserInput.replace(phraseReplace,newPhrase,len(uppercaseUserInput.split()))
#The last parameter above is to provide the maximum number of replaces to be done in the input string
#Here we pass the maximum number of words in the string
________________________________________________________________________________________________________________________
EDIT :
Below is what you might be looking for :
___________________________________________________________________________________________________
#In main get the inputString and pass it to a function called replaceWithNewText
inputString = raw_input("Please enter a string to modify") #Assume the input string is "By the way, I am going out for dinner tonight!"
#To remove all punctuations, import the string library and use the below
for letter in string.punctuation:
inputString= inputString.replace(letter,"")
#To change to uppercase if you can use the join function. This modifies the ascii value of each letter in the string
input = ''.join([chr(ord(char) - 32) for char in str_data if ord(char) >= 65])
print replaceWithNewText(input)
___________________________________________________________________________________________________
#The replace function
def replaceWithNewText(input):
phraseReplace = raw_input("Enter phrase to be replaced")
newPhrase = raw_input("Enter new phrase to replace ")
replacePhrase = phraseReplace.split()
replacePhraseLen = replacePhrase.len()
parsedInput = input.split()
ipsize = parsedInput.len()
newinput = ""
i=0
while(i<ipsize):
if(parsedInput[i] == replacePhrase[0]):
if(shouldReplace(parsedInput[i:i+replacePhraseLen]),replacePhrase):
newInput = newInput + replacePhrase
else:
newInput = newInput + parsedInput[i]
else:
newInput = newInput + parsedInput[i]
i++
return newInput
#Below function returns true if the two input arrays are same
def shouldReplace(input,replace):
i=0
flag = true
while(i<replacePhrase.len()):
if(input[i] != replace[i]):
flag = false
i++
return flag
___________________________________________________________________________________________________
Please be noted that the above is to replace only one of the user input. This can be extended to multiple user inputs too.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.