In: Computer Science
Sovle with python 3.8 please.
1, Write a function called same_without_ends that has two string parameters. It should return True if those strings are equal WITHOUT considering the characters on the ends (the beginning character and the last character). It should return False otherwise. For example, "last" and "bask" would be considered equal without considering the characters on the ends. Don't worry about the case where the strings have fewer than three characters. Your function MUST be called same_without_ends. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition!
2, Write a function called replace_es_with_threes that has a single string parameter. It should return a version of the string where all instances of the letter e have been replaced by the digit 3.
Your function MUST still be called replace_es_with_threes. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition!
Here is the answers in Python:
1)
def same_without_ends(a,b):
l=0
if len(a) == len(b):#chacks if both the string
is of same length
i=len(a)-1
for j in
range(1,i):#loops from 1 to i-1
if a[j]==b[j]:#checks if the letters are the same
l=l+1
if l==(i-1): #checks if all the letters are compared
return True
else:
return False
else:
return False
If you print the function calling statement, it prints true or false.
2)
def replace_es_with_threes(a):
b=a.replace('e','3')#replace(old,new), this
funtion replaces the occurance of old with new in the string and
returns the modified string
return b
If you give 'three' as input it returns 'thr33' as the output.