My Python 2.7 Code is not working to fully pass all the test cases. Please help
ID: 3875190 • Letter: M
Question
My Python 2.7 Code is not working to fully pass all the test cases. Please help me modify the code to make it pass for all test cases for the below two problems about WHILE LOOPS, below is screenshot of my code for the two problems, the failing test cases in my command prompt, and the test cases themself to check the code against.
Problem 1: Write code that checks every adjacent pair of numbers in a list to see if either is a factor of the other and returns all factor-pairs in order. For example, the list [2, 6, 12, 3, 7, 8, 16, 10] would yield ["2-6", "6-12", "3-12", "8-16"]. You may not use any built-in functions/methods besides len() and .append(). Problem 2: 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.CAUsers Mirza J. B Notepad++ File Edit Sarch View Encoding Language Settings Macro Run Plugins Window i bd 2 pairs (11): def 3 4 string1 = "" result = [] #YOUR CODE GOES HERE (indented) counter 0 while counter 1: 10 if counter >0 if 11 [counter-11811 [counter] 0: stringl -str(l1 [counter]) +'-+ str (11 [counter-1]) result.append (stringl) 12 13 14 if counter0 and 11[counter+1]>0: 11 [counter+11811 [counter] 0: 15 16 str (11 [counter+1]) stringl-str (11[counter])'-' result.append (stringl) 17 18 19 20 21 else: string1- str (11 [counter])'-' + str (11 [counter-11) countercounter + 1 return result 23 Python file length : 1232 lines: 43 Ln: 39 Col: 35 Sel: 010 DoslWindows UTF-8 INS 2:42 PM 112/2018
Explanation / Answer
Problem 1:
You just need to check the current and next element if either is a factor of the other and run the while loop till the second last element. You also need to keep a check on the sign of the element. The mod operation (a mod b) returns 0 is magnitude of a is perfectly divisible by the magintude of b. Hence, |a| is always greater than |b| if a%b=0. But since we need to put the pair in increasing order, we need to check the sign of the first operand (a). If it's negative, it's definitely smaller than 'b'. So it should be kept before 'b' in the output pair. Following is the code segment to do so.
def pairs(l1):
string1="";
result=[];
counter=0;
#run the loop till second last element
while counter<len(l1)-1:
#check current and next element
if l1[counter]%l1[counter+1]==0:
#check if first input in the mod operation is negative
if l1[counter]<0:
#put first input before second input since it's smaller
string1=str(l1[counter])+'-'+str(l1[counter+1])
result.append(string1)
else:
string1=str(l1[counter+1])+'-'+str(l1[counter])
result.append(string1)
elif l1[counter+1]%l1[counter]==0:
#check if first input in the mod operation is negative
if l1[counter+1]<0:
#put first input before second input since it's smaller
string1=str(l1[counter+1])+'-'+str(l1[counter])
result.append(string1)
else:
string1=str(l1[counter])+'-'+str(l1[counter+1])
result.append(string1)
counter=counter+1;
return result
Problem 2:
There are two errors. In line 37, the counter should be incremented by len(l2) and then decremented by 1 so as to place it on the character just next to the substring l2 since the counter is going to get incremented by 1afterwards at line 40. So replace lin 37 with
counter+=len(l2)-1
Then in line 39, a '-1' is erroneously added which is both syntactically and logically incorrect. l1[counter] is of str data type and '1' is of int data type. Replace this line with
result+=l1[counter]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.