WRITE SOLUTION CODE IN Python 3 for the following question: Starter Code: def me
ID: 3735217 • Letter: W
Question
WRITE SOLUTION CODE IN Python 3 for the following question:
Starter Code:
def merge(listA, listB):
'''
Given 2 lists whose elements are in increasing order,
merge them into one list of elements in increasing order
:param listA: a list in increasing order
:param listB: a list in increasing order
:return: a list with all elements in increasing order
'''
result = []
indexA = 0
indexB = 0
while indexA < len(listA) - 1 and indexB < len(listB) - 1:
# step through the lists one element at a time
# put the smaller element in the result
if listA[indexA] < listB[indexB]:
result.append(listA[indexA])
indexA = indexA + 1
else:
result.append(listA[indexA])
indexB = indexB + 1
# add the remaining elements
if indexA < len(listA):
result.extend(listA[indexA:])
return result
Question:
In starter code, you'll find the function
• merge(): Given 2 lists whose elements are in increasing order, merge them into one list of elements in increasing order.
The code is below. The idea is to step through both lists one element at a time. Compare two elements,
one from each list, and put the smaller into the merged list. If you reach the end of either list, add the rest
of the other list to the end of the merged result. (See attached photo)
This function contains errors. Your task will be to nd the errors using testing. You’ll hand in an explanation of what the errors are, and how you xed them, but you will not hand in the corrected code.
1. Write white-box and black-box tests for the function merge(). You should do this without Python, either on paper, or in a simple document. Don’t worry about running your tests until you have thought about which tests you want. Make sure you have at least 3 black-box tests, and 3 white-box tests, as a minimum. More tests may be useful.
2. Implementyourtestcasesinadocumentnameda8q3_testing.py.Youcanuseeitherplainif-statements (as demonstrated in readings 15.2.4) or a list-of-dictionaries to implement your test driver (as demon- strated in class during chapter 15 exercise 3).
3. Run your tests, and copy/paste the console output to a document named a8q3_output.txt. The console output you hand in should come from the program before you try to x it!
5. In the document a8q3_output.txt, describe the error(s) you found, and how you xed it (them). You do not need to write a lot; a sentence or two for each error is all we want. Write your explanation as if you are explaining the error to a colleague of yours who wrote the function.
What to Hand In
A document containing your testing code as described above.
A document with the output of your testing copy/pasted into it, and the explanations of the errors you found.
Evaluation
Your test-driver for merge() contains at least 6 test cases.
The output of your test driver reveals faults in the given function.
You correctly identifed four errors in merge(), and explained brie y how you xed them.
Explanation / Answer
1.
Black-box testing is a method of software testing that examines the functionality of an application based on the specifications. It is from the user point of view. This type of testing is carried out by testers.
Black box testing as follows
Python Command Output
--------------- -------
print merge([1,2,3],[4,5]) [1,2,3]
print merge([3, 4],[1, 2]) [3,3,4]
print merge([1,2,],[3,4,5]) [1, 2]
print merge([1,2,],[3,4]) [1, 2]
print merge([1,2],[]) [1, 2]
print merge([],[1,2]) []
print merge([],[]) []
So, blackbox testing doesn't meet desired output
White box testing is the software testing method in which internal structure is being known to tester who is going to test the software. Generally, this type of testing is carried out by software developers. In white box testing we print the intermediate output of branchs and loops
White box testing output as follows
-----------------------------------
Finding merge([1, 2, 3],[4, 5]):
appending 0 element ie '1' of listA
Intermediate Result array in while loop:[1]
appending 1 element ie '2' of listA
Intermediate Result array in while loop:[1, 2]
After adding remaining elements of listA: [1, 2, 3]
Returning Merged Result: [1, 2, 3]
Finding merge([3, 4],[1, 2]):
appending 0 element ie '1' of listB
Intermediate Result array in while loop:[3]
After adding remaining elements of listA: [3, 3, 4]
Returning Merged Result: [3, 3, 4]
Finding merge([1, 2],[3, 4, 5]):
appending 0 element ie '1' of listA
Intermediate Result array in while loop:[1]
After adding remaining elements of listA: [1, 2]
Returning Merged Result: [1, 2]
Finding merge([1, 2],[3, 4]):
appending 0 element ie '1' of listA
Intermediate Result array in while loop:[1]
After adding remaining elements of listA: [1, 2]
Returning Merged Result: [1, 2]
Finding merge([1, 2],[]):
After adding remaining elements of listA: [1, 2]
Returning Merged Result: [1, 2]
Finding merge([],[1, 2]):
Returning Merged Result: []
Finding merge([],[]):
Returning Merged Result: []
code for whitebox testing as follows
-------------------------------------
def merge(listA, listB):
'''
Given 2 lists whose elements are in increasing order,
merge them into one list of elements in increasing order
:param listA: a list in increasing order
:param listB: a list in increasing order
:return: a list with all elements in increasing order
'''
result = []
indexA = 0
indexB = 0
print "Finding merge(%s,%s):" % (listA,listB)
while indexA < len(listA) - 1 and indexB < len(listB) - 1:
# step through the lists one element at a time
# put the smaller element in the result
#print "index A %s index %s"%(indexA,indexB)
if listA[indexA] < listB[indexB]:
result.append(listA[indexA])
print "appending %s element ie '%s' of listA "% (indexA, listA[indexA])
indexA = indexA + 1
else:
# result.append(listA[indexA])
result.append(listA[indexA])
print "appending %s element ie '%s' of listB "% (indexB,listB[indexB])
indexB = indexB + 1
print 'Intermediate Result array in while loop:'+ str(result)
# add the remaining elements
if indexA < len(listA):
result.extend(listA[indexA:])
print 'After adding remaining elements of listA: '+ str(result)
#if indexB < len(listB):
#result.extend(listB[indexB:])
print "Returning Merged Result: %s " %result
return result
# White box test code
merge([1,2,3],[4,5])
merge([3,4],[1,2])
merge([1,2,],[3,4,5])
merge([1,2,],[3,4])
merge([1,2],[])
merge([],[1,2])
merge([],[])
5.
From the whitebox test result it is clear that code in else block inside while loop is not correct. Instead of appending element of listB, it is adding element of listA. So the else block should be like this
else:
result.append(listB[indexB])
indexB = indexB + 1
Another issue is that, it is adding remaining elements of listA, but not adding remaining elements of listB, so result array misses some items of listB
So we should add following code before calling 'return result'
if indexB < len(listB):
result.extend(listB[indexB:])
Hence the complete corrected code goes here
-------------------------------------------
def merge(listA, listB):
'''
Given 2 lists whose elements are in increasing order,
merge them into one list of elements in increasing order
:param listA: a list in increasing order
:param listB: a list in increasing order
:return: a list with all elements in increasing order
'''
result = []
indexA = 0
indexB = 0
while indexA < len(listA) - 1 and indexB < len(listB) - 1:
# step through the lists one element at a time
# put the smaller element in the result
if listA[indexA] < listB[indexB]:
result.append(listA[indexA])
indexA = indexA + 1
else:
result.append(listB[indexB])
indexB = indexB + 1
# add the remaining elements
# if indexA is more than indexB add remaining element of listA first
if indexA > indexB:
if indexA < len(listA):
result.extend(listA[indexA:])
if indexB < len(listB):
result.extend(listB[indexB:])
else:
if indexB < len(listB):
result.extend(listB[indexB:])
if indexA < len(listA):
result.extend(listA[indexA:])
return result
Test code returns following for the corrected function
print merge([1,2,3],[4,5])
print merge([3,4],[1,2])
print merge([1,2,],[3,4,5])
print merge([1,2,],[3,4])
print merge([1,2],[])
print merge([],[1,2])
print merge([],[])
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
[1, 2]
[1, 2]
[]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.