0% found this document useful (0 votes)
8 views8 pages

coms1022a_test2 (1)

The document is a class test for the course Introduction to Algorithms & Programming (COMS1022A) dated April 10, 2024. It consists of four questions covering topics such as Python variable types, function behavior, error identification, and algorithm efficiency. The test is closed-book, lasts 60 minutes, and contains a total of 25 marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

coms1022a_test2 (1)

The document is a class test for the course Introduction to Algorithms & Programming (COMS1022A) dated April 10, 2024. It consists of four questions covering topics such as Python variable types, function behavior, error identification, and algorithm efficiency. The test is closed-book, lasts 60 minutes, and contains a total of 25 marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to Algorithms & Programming

(COMS1022A)
Class Test 1

10 April 2024

Name: Memo Student Number:

For marking purposes only

Question 1
Question 2
Question 3
Question 4
Total

Instructions
• Answer all questions in pen. Do not write in pencil.

• This test consists of 8 pages. Ensure that you are not missing any pages.

• This is a closed-book test: you may not consult any written material or notes.

• You are allocated 60 minutes to complete this test.

• There are 4 questions and 25 marks available.

• Ensure your cellphone is switched off.

1
COMS1022A Introduction to Algorithms and Programming 10 April 2024

Question 1 [12 Marks]


1. For each description of what the variable should store, select the Python variable
type that is most suitable: [2]

(a) Whether a student has achieved a passing grade bool

(b) A single letter of the alphabet str

(c) The number of students enrolled in a class int

(d) A student’s name str

[0.5 each]
2. What is the difference between a dynamically typed language and a statically typed
language? Provide an example that demonstrates Python’s dynamic typing abili-
ties. [2]

Dynamic: the type of a variable can change over time; static: variable types are fixed

forever

e.g.

x = "string" followed by x = 9 is valid (first x holds a string, then an integer)

[1 mark for difference, 1 for appropriate example]


3. What is the difference between the = operator and the == operator? [1]

= is assignment

== is an equality check [0.5 each]

4. List two advantages of using functions in one’s code. [2]

Any two:

eases maintenance, easy to read, easy to distribute labour, easy to reuse, etc

[1 mk each]

Page 2 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024

5. Alice wrote some Python code, but when she tried to execute it, the interpreter dis-
played the error: TypeError: unsupported operand type(s) for +:
’int’ and ’str’. Give one potential cause for the error. [1]

Alice attempted to add a string to an integer e.g. "The answer is " + 4 [1 mk]

6. Mary has written code that stores all the first names of students in her class in
a container. However, after adding everyone’s name to the container, when she
computes its length she finds that there are fewer names than people in the class.
What container has she likely used? Provide a reason for your answer. [2]

A set. If there are peopel sharing names, then the set will drop duplicates, meaning

that the count is less than the true class size

1mk for set, 1 for reason

7. Compute the result of the following Python expressions: [2]

(a) 32/4 ∗ ∗2/8 + 5 5.25

(b) 20//3 + 4%3 7

(c) bool(0) False

(d) 5 > 3 or 10 < 5 and not 2 == 3 True

0.5 each

Page 3 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024

Question 2 [5 Marks]
1. Consider the function below:

1 def f(b, a):


2 if a >= b:
3 if b > 15:
4 print("A")
5 elif b != 15:
6 print("B")
7 else:
8 print("C")
9 elif b > a:
10 print("D")

What does the function print to screen when we invoke f as follows: [2]

(a) f(14, 15) B


D
(b) f(15, 14) [0.5 each]
(c) f(16, 17) A

(d) f(15, 16) C

2. How many times does line 2 execute in the code below? [1]

1 for i in range(5, -5, -1):


2 print(i)

10
3. How many times does line 10 execute in the code below? [2]

1 i = 2
2 while i < 5:
3 i = i + 1
4 j = 1
5 while j < i:
6 if j % i != 0:
7 j = j + 1
8 else:
9 j = j + 2
10 print(j)

9 2mk

Page 4 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024

Question 3 [4 Marks]
1. Bob has written some code to determine if the user has entered a number that
is positive, negative or zero, but there are a number of errors that will prevent it
from executing successfully. Identify and suggest a way to fix each error. [2]

1 number = int(input())
2 if number == 0:
3 print("You entered zero")
4 else if number > 0:
5 print("You entered a positive number")
6 else number < 0:
7 print("You entered a negative number")

XXXXXXXXXXX
Line 2: need ==

Line 4: else if is not Python. It should be elif

Line 6: should be just else (or elif)

[1mk each]

2. Eve has written a function that accepts a list of names and returns a dictionary that
associates each name with how many times it appears in the list. However, there
are two errors in her code that will prevent it from executing successfully. Identify
and suggest a way to fix each error. [2]

1 def count(names):
2 counts = dict()
3 N = len(names)
4 for i in range(N + 1):
5 name = names[i]
6 if name not in counts:
7 counts[name] = counts[name] + 1
8 else:
9 counts[name] = 1
10 return counts

Line 4: loop should go to N, not N+1

Line 6: the not in is the wrong way round, it should be if name in counts

[1 mk each]

Page 5 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024

Question 4 [4 Marks]
1. Bob and Alice are attempting to solve the following problem: given an unordered
list of integers (that we assume to be all unique) and a target integer value, find
all pairs of integers that sum up to the target value. For example, the list [4, 1,
6, 3] with target value 7 has two such pairs: [1, 6] and [4, 3]. Note that the order
of the pairs is not important.
Alice has written the following function to solve the problem:

1 def find_pairs_alice(nums, target):


2 count = 0
3 pairs = list()
4 for i in range(len(nums)):
5 for j in range(i + 1, len(nums)):
6 if nums[i] + nums[j] == target:
7 pairs.append([nums[i], nums[j]])
8 return pairs

On the other hand, Bob’s attempt at a solution looks as follows:

1 def find_pairs_bob(nums, target):


2 pairs = list()
3 # create a set containing all the values in nums
4 num_set = set(nums)
5 for num in nums:
6 complement = target - num
7 if complement in num_set:
8 pairs.append([num, complement])
9 return pairs

(a) Assuming that there are N elements in the list nums, how many times is
the if-statement (line 6) in the function find pairs alice executed? The
following identity may be helpful in your calculation: [2]
N
X N (N + 1)
i=
2
i=1

When i is 0, inner loop runs (N-1) times. When i=1, inner loop runs (N-2)

times, ... when i=N-1, loop runs 0 times. So total times inner loop (and hence

line 6 runs is SUM from 0 to (N-1) of i = N(N-1) / 2

Page 6 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024

(b) As the size of the list nums grows larger and larger, whose function do you
think will compute an answer faster? Motivate your answer, making reference
to your solution from (a). [2]

Alice's if statement will run in total N(N-1)/2 times, whereas Bob's if

statement will run N times. If we plot these two functions, one is quadratic

and one is linear. And as N grows, Alice's function wil be much larger

than Bob's, which means more if statements are being executed, more

work is being done and so it is likely slower than Bob's

Page 7 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024

This page intentionally left blank for extra space

Page 8 of 8

You might also like