Please use a WHILE loop to solve each of the two problems below. The test cases
ID: 3875292 • Letter: P
Question
Please use a WHILE loop to solve each of the two problems below. The test cases to check the code against are also provided below the parameters and answer template. Please use Python 2.7 Language
Write code that takes two strings from the user, replaces all instances of the second string with box in the first. For example, happymondayhappyday and happy yields the result boxmondayboxday. We will guarantee the second string will be less than four characters long. You may not use the built-in replace function.
#PROBLEM 1
def pairs(l1):
list1 = l1
return 0
#YOUR CODE GOES HERE (indented)
return l1
#END YOUR CODE
#PROBLEM 2
def replaceAll(l1,l2):
string1 = l1
string2 = l2
#YOUR CODE GOES HERE (indented)
return string1
#END YOUR CODE
Write code that takes two strings from the user, replaces all instances of the second string with box in the first. For example, happymondayhappyday and happy yields the result boxmondayboxday. We will guarantee the second string will be less than four characters long. You may not use the built-in replace function.
e Chegg Study l Guic C Secure G chegg Customer Se Chegg Study l Guic X Chegg Study I Gu x e Chegg Studyl Gu https://cs.gmu.edu/~kdobolyì/sparc/Chapter-5/tests4v6 sample.bt Chegg Study | Guic × yg https://cs mu edu x : Apps FCPS . FCPSnet-FCPS 24-7 Learning G FCPS Google Apps G celebrating Robert K pairs l pairs [2] pairs [1,2] pairs [5,15,2,-4,3,6] CS112 (SPARC) 5-15, '-4-2', 3-6'1 pairs [5,1,2,-4,3,6] 1-5, 1-2', '-4-2', 3-61 pairs [2,6,12,3,7,8,16,10] '2-6*, 6-12', 3-12, '8-16'] replaceAll bird c bird replaceAll bird i replaceAll bird ir replaceAll birdird ir replaceAll birdeeeirde ird bboxeeeboxe replaceAll birdeeeirede ird bboxeeeirede 12:41 PM il n/2018Explanation / Answer
#PROBLEM 1:
def pairs(l1):
list1=l1
#YOUR CODE GOES HERE (indented)
temp=[];
ctr=0; #counter
#run the while loop till second last element
while ctr<len(list1)-1:
#apply mod operation on current and next element
if list1[ctr+1]%list1[ctr]==0:
#check if first input in the mod operation is negative
#put smaller value first
if list1[ctr+1]>0:
temp.append(str(list1[ctr])+'-'+str(list1[ctr+1]))
else:
temp.append(str(list1[ctr+1])+'-'+str(list1[ctr]))
elif list1[ctr]%list1[ctr+1]==0:
#check if first input in the mod operation is negative
#put smaller value first
if list1[ctr]>0:
temp.append(str(list1[ctr+1])+'-'+str(list1[ctr]))
else:
temp.append(str(list1[ctr])+'-'+str(list1[ctr+1]))
ctr=ctr+1;
l1=temp;
return l1
#END YOUR CODE
#PROBLEM 2:
def replaceAll(l1,l2):
string1=l1
string2=l2
#YOUR CODE GOES HERE (indented)
temp=""
ctr=0 #counter initialized to 0
while ctr<len(string1):
#compare the substring in first input starting from current character with second input
if string1[ctr:ctr+len(string2)]==string2[:]:
#add 'box' to result
temp=temp+'box'
#increment counter
ctr=ctr+len(string2)
else:
#add the current character to result
temp=temp+string1[ctr]
#increment counter
ctr=ctr+1
string1=temp
return string1
#END YOUR CODE