Lab Test 1
Lab Test 1
1 Instructions
• This is an closed-book test: you may not consult any written material, books or the
internet.
• Any form of communication with anyone other than test invigilators will be considered
cheating. This includes the sharing of files, emailing and chatting.
• No cellphones are permitted in the test venue.
• There is a strict deadline on submissions. Late submissions will not be accepted.
• There are six questions in total. Answering all six correctly will earn you 100%.
• Your solutions should work for any valid input for a given question, not just the sample
inputs that accompany each question.
• To submit your programs, log onto Moodle at http://courses.ms.wits.ac.za and submit
under the appropriate heading.
• You may submit as many times as you like. Only the last solution will be counted.
• You may only ask test invigilators for question clarifications, and not for code debugging
help.
• You may submit questions in any order you wish, but the questions are ordered roughly
from easiest to hardest.
• If you have a problem when submitting your code to Moodle, ask an invigilator for help.
• If the marker is taking a long time to evaluate your submission, do not wait. Move on to
the next question.
1
2
Question 1: Circle Area (10 Marks)
Write a Python program that accepts a single real-valued number r — the radius of a circle —
22
and outputs the area of the circle. Use as an approximation to π.
7
Input
Input is a single real-valued number r ≥ 0.
Output
22
Output the area of the circle: πr2 , using the approximation π ≈ .
7
Sample Output
Sample Input #1 Sample Input #2
7 1
3
Question 2: Palindrome (20 Marks)
A palindrome is a word that reads the same backwards as forwards (such as racecar). The
pseudocode below represents an algorithm that accepts a string as input and determines whether
the string is a palindrome. Implement the corresponding Python program.
Input
Input consists of a single word. You may assume that the word contains only lower-case letters
(no numbers or punctuation), and contains at least one letter.
Output
Output true if the word is a palindrome. Otherwise, output false
4
Question 3: Geometric Mean (30 Marks)
In mathematics, the geometric mean is a mean or average, which indicates the central tendency
or typical value of a set of numbers by using the product of their values (as opposed to the
arithmetic mean which uses their sum).
The geometric mean is defined as the nth root of the product of n numbers, i.e., for a set of
numbers x1 , x2 , . . . , xn , the geometric mean is defined as
1
(x1 × x2 × . . . × xn ) n
Write a Python program that accepts many real numbers as input, and outputs their geometric
mean.
Input
The first line of input is an integer n. n real-valued numbers follow, each on their own line.
Output
Print out the geometric mean of the n numbers.
Sample Output
Sample Input #1 Sample Input #2
2 3
2 1.5
8 2
9.0
5
Question 4: Name Shortening (20 Marks)
In a telephone directory, the names of streets and suburbs are shortened by removing all vowels
(a, e, i, o, u), unless the vowel appears at the beginning of a word, in which case it is kept.
Write a Python program that will input an address and automatically shorten it in this manner.
Punctuation and numbers must be kept unchanged, as must any capitalisation of letters.
Input
Input consists of a single line of text, containing an address. The line may contain letters, digits,
punctuation and spaces.
Output
Output the shortened version of the address.
Sample Input
13 Observatory Road
Sample Output
13 Obsrvtry Rd
6
Question 5: Sentence Reversal (15 Marks)
Write a Python program that reads in a sentence and outputs the words of the sentence in
reverse order. For example, if the sentence is Bob loves dogs, then the output should be dogs
loves Bob.
Input
Input consists of a single sentence. The sentence will contain only letters and spaces (no punc-
tuation or digits).
Output
Output the words of the sentence in reverse order.
Sample Input
Hello Bob
Sample Output
Bob Hello
7
Question 6: Ants* (5 Marks)
An army of ants walk on a horizontal pole of length N cm, each with a constant speed of 1
cm/s. When a walking ant reaches any end of the pole, it immediately falls off. When two ants
meet they turn back and start walking in opposite directions. At the start, we know the original
positions of ants on the pole, but we do not know the direction they are facing! Given only the
starting positions, write a program that will compute the earliest and the latest possible times
needed for all ants to fall off the pole, given that every ant can be facing either left or right to
begin with.
Input
The first line of input is a single integer N , the length of the pole. Then next line is an integer
K, specifying the number of ants. K integers follow, each on their own line, where each integer
specifies the starting location of an ant.
Output
On their own line, print out a) the earliest possible times when all the ants fall off the pole, and
b) the latest possible such time.
Sample Input
214
7
11
12
7
13
176
23
191
Sample Output
38
207