LabTest1 1018
LabTest1 1018
1 Instructions
• This is a 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: Hello, human! (10 Marks)
Write a Python program that accepts a person’s name, and produces the following output:
Hello <name>
Input
Input consists of a single line containing a name.
Output
Output the required text, in the exact format specified.
Sample Input
Bob
Sample Output
Hello Bob
3
Question 2: GCD (20 Marks)
The pseudocode below represents an algorithm for calculating the greatest common divisor
(GCD) of two integers a and b. Implement the corresponding Python program.
Input
Input consists of two integers a and b, separated by a single line. You may assume that a and b
are both positive integers.
Output
Print the greatest common divisor of a and b.
Sample Input
24
54
Sample Output
6
4
Question 3: String Reversal (30 Marks)
Write a Python program that reads in a string and outputs the string in reverse.
Input
Input consists of a single line containing a string. The string may contain spaces.
Output
Output the reversed string.
Sample Input
Hello Bob
Sample Output
boB olleH
5
Question 4: Second Max (20 Marks)
Everyone knows that first is the worst, second is the best. Write a Python program that accepts a
series of integers and outputs the second largest number.
Input
Input consists of a series of integers, one per line. You can assume that at least two numbers
will always be provided. The end of the input will be signalled by the number −1. When you
receive a −1, your program should then output the correct answer and terminate. Note that −1
should be completely ignored and so should not be taken into consideration when performing
the calculation.
Output
Print the second largest number received
Sample Input
1
2
3
-1
Sample Output
2
6
Question 5: String of Vowels (15 Marks)
Given a sentence that could contain letters, spaces, punctuation or digits, your task is to write a
Python program that determines the longest consecutive sequence of vowels in the string.
Note that in performing this calculation, spaces, digits and punctuation should be ignored, which
means that the longest sequence could stretch across multiple words. For example, the longest
sequence of vowels in the sentence “You are happy” is “oua” (You are), while the longest se-
quence in the sentence “I have 9 apples” is “ea” (have 9 apples)
Input
Input consists of a single line of text. This line could contain upper and lower case letters,
spaces, digits or punctuation.
Output
Output the length of the longest consecutive sequence of vowels in the string. If the sentence
does not contain a vowel, output 0.
Sample Input
You like mayonnaise but I LOVE aioli!
Sample Output
4
7
Question 6: Stock Trading* (5 Marks)
Imagine we are looking to buy and sell stocks in a company. We are given a list of integers,
which represents the price of the stock over several days. We wish to compute the maximum
profit that could be obtained by buying and selling the stock as many times as we want.
However, we are not allowed to engage in multiple transactions simultaneously (i.e., we must
sell the stock before we buy again, and we cannot sell and buy on the same day).
To make things trickier, there is a selling fee that we must pay each time we sell the stock.
For example, imagine the transaction fee is 2, and the stock prices for days 0–5 are as follows:
1,3,2,8,4,9
The total profit is then ((8 − 1) − 2) + ((9 − 4) − 2) = 8 where because we sold twice, we paid
the transaction fee of 2 twice.
Input
The first line of input is an integer representing the selling fee. This value is always non-negative.
The remaining lines each consist of a single integer, which represents the price of the stock at
a given day. The stock price is always greater than 0. The end of the input will be signalled by
the number −1. When you receive a −1, your program should then output the correct answer
and terminate. Note that −1 should be completely ignored and so should not be taken into
consideration when performing the calculation.
Output
Output the maximum profit that can be made by buying and selling the stock.
Sample Output
Sample Input #1 (as in the example) Sample Input #2
2 3
1 1
3 3
2 7
8 5
4 10
9 3
-1 -1