Unit 2 Flashcards

1
Q

Name: GetOdd

Inputs: A set of natural numbers I = {i<em>1</em>, i<em>2</em>, i<em>3</em>, …, i<em>k</em>}

Outputs: A set of natural numbers O = {o<em>1</em>, o<em>2</em>, o<em>3</em>, …, o<em>j</em>}

Precondition: k > 0

Postcondition:

Which of the following statements could form part of the postcondition of the problem?

  • I is not equal to O
  • o<em>j</em> is not in I
  • For all i<em>i</em> in I, i<em>i</em> is in O
  • For all o<em>i</em> in O, o<em>i</em> is in I
  • j k
  • j k
A
  • For all o<em>i</em> in O, o<em>i</em> is in I
  • j k
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The following structured English specifies an algorithm that is intended to determine whether or not a candidate passes their driving test. The criteria for passing is that the candidate will pass if they make 15 or fewer driving faults and no serious or dangerous faults.

  1. set drivingFault to 16
  2. set seriousFault to 0
  3. set dangerousFault to 0
  4. set result to True
  5. IF seriousFault is greater than 0
  6. set result to False
  7. IF dangerousFault is greater than 0
  8. set result to False
  9. IF drivingFault is greater than 15
  10. set result to False

Assume that the structured English has been transformed into Python code.

Note that the first three lines of code are there to state the performance of a specific candidate, but in a more elaborate version of the program, these might be read in from a database or user input.

Which one of the following statements are correct?

  • result will be False
  • result will be True
  • It is not possible to determine the value of result
  • The Python code implementing this algorithm will crash as the algorithm is faulty
A

result will be True:

The example illustrates a very common type of error in handling selection.

Lines 1 to 3 set values for each of the test fault results, with drivingFault set to 16 and both seriousFault and dangerousFault set to 0.

Line 4 initialises result to True

Lines 5, 7 and 9 show nested if statements. If the value of seriousFault is greater than 0, then the body of that first if statement is entered. In this case, since the value of seriousFault was set to 0, the body of the if statement is not entered, and since all the remaining code is nested inside that if statement, none of it will be executed and result will remain True.

The three selection criteria are actually of equal importance and should not be nested. A correct version of the algorithm is:

  1. set drivingFault to 16
  2. set seriousFault to 0
  3. set dangerousFault to 0
  4. set result to True
  5. IF seriousFault is greater than 0
  6. set result to False
  7. IF dangerousFault is greater than 0
  8. set result to False
  9. IF drivingFault is greater than 15
  10. set result to False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Complete the following truth table:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

3 advantages

What are the possible advantages of the statement of pre- and postconditions?

A
  • Clarity
  • Non-redundancy
  • Simplicity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is defensive programming?

A

Defensive programming is a style of programming where programmers put in checks before calling a function and also within the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the disadvantages to defensive programming?

A

Defensive programming can lead programmers to put in checks everywhere, which leads to slower and less reliable code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Are checks needed when pre- and postconditions have been used?

A

Yes, checks are still required, because otherwise the calling code can’t be sure that it’s meeting the pre-conditions.

However, the use of pre- and postconditions means that programmers can avoid making unnecessary checks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

An ADT called ShoppingList offers the following services to clients:

addItem(x) – Inserts x as the last item

removeItem(x) – Removes the item x from the shopping list

makeFavourite(x) – Notes that the item x is a favourite

removeFromFavourite(x) – Notes that the favourite item x is no longer a favourite

isFavourite(x) – Returns True if x is a favourite, False otherwise

isIn(x) – Returns True if x is on the shopping list, False otherwise

isEmpty() – Returns True if there are no items on the shopping list, False otherwise

size() – Returns the number of items on the shopping list

ShoppingList should not allow duplicate entries in the list.

Based on the service descriptions above, complete the following table, using the labels below:

[Precondition] [Postcondition]

[addItem(x)] [] []

[removeFromFavourite(x)] [] []

[Invariant] []

  • isEmpty()
  • ¬isEmpty()
  • isIn(x)
  • ¬isIn(x)
  • isFavourite(x)
  • ¬isFavourite(x)
  • size() = 0
  • size() >= 0
  • size()
  • size = old size + 1
A

[Precondition] [Postcondition]

[addItem(x)] [¬isIn(x)] [isIn(x)]

[removeFromFavourite(x)] [isFavourite(x)] [¬isFavourite(x)]

[Invariant] [size() >= 0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Consider the following Python function, foo(), which receives as input a list of numbers aList.

def foo(aList):
 a = 7
 b = 5
 n = len(aList)
 for i in range(n):
 for j in range(n):
 a = i \* a
 b = j \* j
 w = i \* j
 v = i + w
 x = v \* v
 for k in range(n):
 w = a \* k + 23
 v = b \* b
 a = w + v

Assuming that the length of the list (n, in this function) is the size of the problem, and taking a step (i.e. the basic unit of computation) to be an assignment statement, choose the appropriate T(n) function and the complexity for foo’s algorithm from the options below.

  • T(n) = 3 + n + 5n2 + 2n3, complexity O(n3)
  • T(n) = 3 + 6n2 + 2n3, complexity O(n3)
  • T(n) = 3 + 2n + 5n2 + 2n3, complexity O(n2)
  • T(n) = 3n + 5n2 + 2n3, complexity O(n)
  • T(n) = 3 + n + 5n2 + 2n3, complexity O(n!)
  • T(n) = 3n + 5n2 + 2n3, complexity O(n3)
  • T(n) = 7n2 + n + 3, complexity O(n2)
A

T(n) = 3 + n + 5n2 + 2n3, complexity O(n3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following functions grows the fastest as n increases?

  • 3n2 + 20n
  • (9n2)/2 + 10n + 100
  • 5n2 + 5n
  • 3n2 + 5
  • 4n2 + 3n + 100
  • n2 + 1000
A

5n2 + 5n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

This question is about carrying out a binary search for the item 22 in this list: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59].

Select one correct statement about the first partition of the list to be searched, and select one correct statement about the last non-empty partition of the list to be searched.

  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the last non-empty partition of this list that will be searched is [19]
  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the first partition of this list that will be searched is [19, 23, 29, 41, 43, 47, 53, 59]
  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the last non-empty partition of this list that will be searched is [23]
  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the first partition of this list that will be searched is [2, 3, 5, 7, 11, 13, 17]
  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the first partition of this list that will be searched is [2, 3, 5, 7, 11, 13, 17, 19]
  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the first partition of this list that will be searched is [23, 29, 41, 43, 47, 53, 59]
A
  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the last non-empty partition of this list that will be searched is [23]
  • Starting with the list [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 41, 43, 47, 53, 59], the first partition of this list that will be searched is [23, 29, 41, 43, 47, 53, 59]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a linear data structure?

A

A linear data structure is a data structure in which items persist in the same position, relative to other elements.

All linear structures can be thought of as having two ends:

  • In the case of a list, the ‘beginning’ and the ‘end’
  • In the case of a stack, the ‘top’ and the ‘base’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a Stack?

A

The Stack ADT is an example of a linear structure, in which items persist in the same position, relative to other elements.

A stack has two ends - the ‘top’ and the ‘base’.

Stacks are LIFO (last-in first-out) structures. Items added to a stack last come out first, so an item’s position in the stack is related to the order in which it was added.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an invariant?

A

An invariant of an ADT is a condition that must apply to every data structure that implements that ADT throughout its lifetime.

The invariant must hold true as soon as the instance has been constructed, and remain true at all subsequent times - an operation should never have the effect of leaving an invariant false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does the binary search algorithm work?

A
  1. Define the search partition to the whole list
  2. Locate the midpoint of the list and check to see if that is the item we are looking for
  3. If not, check whether the midpoint is higher or lower than the item we are looking for
  4. Adjust the start and end of the partition to either the first half or the second half of the original partition - if the midpoint is lower than the item we are looking for, discard the first half, if the midpoint is higher than the item we are looking for, discard the second half
  5. Repeat until search term has been found
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Complete the following truth table:

A
17
Q

What is a loop guard?

A

A loop guard is a Boolean expression that determines whether a loop is entered.

18
Q

What is a client of an ADT?

A

A client of an ADT is any piece of code that uses one of the ADT’s operations, but with no access to the internal workings (as implemented by a data structure).

19
Q

What are the common functions for Big-O, in order of magnitude, according to Miller and Ranum?

A

From smallest to largest magnitude:

  • O(1)
  • O(log n)
  • O(n)
  • O(n log n)
  • O(n2)
  • O(n3)
  • O(2n)
20
Q

What is an initial insight?

A

An initial insight is a vague and abstract idea of the steps necessary to solve a problem.

21
Q

Write a specification (including pre- and postconditions) and initial insight for the following problem:

Find the highest number in a sequence of numbers.

A

Name: FindHighest

Input: A sequence of integers I = (i1, i2, i3, …, in)

Output: An integer h

Precondition: Length of I > 0

Postcondition: h is in I and h >= ik for all ik in I

Assuming input I has been renamed to intSeq and output h has been renamed to highest:

Set highest to the first item in intSeq. Examine each number in intSeq, one by one. If the current number is larger than highest, then set highest to the current number.

22
Q

Calculate the T(n) function and Big-O complexity for the following code, assuming that the unit of computation is the assignment statement:

  1. def someFunction(aList):
  2. n = len(aList)
  3. counterOne = 0
  4. counterTwo = 0
  5. for i in range(n):
  6. counterTwo = counterOne + 1
  7. for j in range(n):
  8. counterTwo = counterTwo + 1
  9. return counterOne + counterTwo
A
  • T(n*) function:
  • T(n*) = 3 + n + n2

Big-O complexity:

O(n2)