In: Computer Science
Can you please tell me, what would be the correct way of doing it?
def updateRepresentation(blank, secret, letter):
"""
This function replaces the appropriate underscores with the
guessed
letter.
Eg. letter = 't', secret = "tiger", blank = "_i_er" --> returns
"ti_er"
Paramters: blank, secret are strings
letter is a string, but a single letter.
Returns: a string
"""
#TODO -- complete this function so that it produces a new
string
#from blank with letter inserted into the appropriate
locations.
#For example:
# letter = 't', secret = "tiger", blank = "_i_er" --> newString
"ti_er"
#newString should be returned.
#hint:
#iterate through each character of secret by index position
# check to see if letter = current character at index
position
# if yes, add this letter to newString
# if no, add the letter from blank found at index position
#leave this line (and use the variable newString in your
code
newString = ""
for i in range(len(secret)):
if letter == secret[i]:
newString= newString + letter
else:
blank= blank[:i] + letter
return newString
CODE:
OUTPUT:
Raw_code:
#function defintion of updatedRepresentation with
parameters(blank,secret,letter)
def updateRepresentation(blank,secret,letter):
#declaring a newString
newString=""
#for loop is iterating with range of length of the secret
message
for i in range(len(secret)):
#if the letter is found in secret message store the index
value
if(letter==secret[i]):
index=i
#replacing the letter with blank(_) in the appropriate index
blank=blank[:index]+letter+blank[index+1:]
#copying the blank string in newString
newString=blank
#returning the newString
return newString
#entering a single letter
letter=input("Enter a single letter:")
#entering a secret message
secret=input("Enter a secret message:")
#enter a blank message that means entering the secret message with
some blanks(_) in the replace of some characters
blank=input("Enter the blank message which should be like secret
with some blank spaces(use underscore for blanks):")
#calling the updateRepresentation with parameters
of(blank,secret,letter) and storing the returned string in
blank
blank=updateRepresentation(blank,secret,letter)
#printing the updated blank string
print("The updated string is:",blank)
************For any queries comment me in the comment box **************