235k views
4 votes
Assume you need to test a function named inOrder. The function inOrder receives three int arguments and returns true if and only if the arguments are in non-decreasing order: that is, the second argument is not less than the first and the third is not less than the second. Write the definition of driver function testInOrder whose job it is to determine whether inOrder is correct. So testInOrder returns true if inOrder is correct and returns false otherwise. . For the purposes of this exercise, assume inOrder is an expensive function call, so call it as few times as possible!

1 Answer

3 votes

Answer:

  1. def testInOrder(a1, a2, a3):
  2. aList = [a1, a2, a3]
  3. if(min(aList) != a1 ):
  4. return False
  5. if(max(aList) != a3):
  6. return False
  7. return True
  8. def inOrder(n1, n2, n3):
  9. print(testInOrder(n1, n2, n3))
  10. inOrder(2, 4, 5)
  11. inOrder(4, 3, 6)
  12. inOrder(4, 3, 2)
  13. inOrder(3, 5, 1)

Step-by-step explanation:

Firstly we create a driver function testInOrder that takes 3 inputs, a1, a2, and a3 (Line 1).

Since the value of a1 to a3 must be in ascending order so we can check if the a1 and a3 are the minimum and maximum number among the three, respectively. To do so, put a1, a2 and a3 into a list (Line 2). Next, we can use the min function to check if the minimum number is a1 (Line 3). If not return False (Line 4).

We can use the similar way to check if the maximum number of list is a3. If not return False (Line 6-7).

If both of the if conditions are not met, return True (Line 9).

We can test our driver function by repeatedly call the inOrder function with different sets of arguments (Line 14 - 17). We shall get the output as follows:

True

False

False

False

User Evgeny Rodionov
by
6.9k points