Professional Documents
Culture Documents
COMP100 - Main 2019
COMP100 - Main 2019
MAIN EXAMINATION
MAY 2019
INSTRUCTIONS:
1. This paper consists of 9 pages (incl. this one).
2. Page 9 contains reference material that you may find useful.
3. Answer all questions.
4. Start the answers to each question on a new page.
5. You may answer the programming questions in pencil.
6. The use of a calculator is NOT allowed.
7. Marks for the programming problems will be awarded for correct solutions and
for efficiency of the code.
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 2
1.1) (3)
sum = 0
for i in range(4,7):
sum += i
if sum % 2 == 0:
continue
print(sum)
1.2) (3)
def mystery(n):
m = 0
while n > 0:
m = 10 * m + n % 10
n = n // 10
return m
print(mystery(345))
1.3) (3)
str1 = "COMP100"
t = len(str1)
for i in range(0,t,2):
print(str1[i],end="-")
print(t)
1.4) (3)
x = 3
y = 4
z = 15
while (z >= x + y):
print(z)
if (z % 3 == 0):
z -= y
else:
z -= x
1.5) (3)
def maxiVal(a, b, large ):
if a > b:
large = a
else:
large = b
return max
large = 0
large = maxiVal(−1,−5,large)
print("Max is :", large)
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 3
1.6) (3)
a = False
b = True
print(a and not a)
print(a and not b)
print(a == b or not b)
print(a != b or not a)
print(a == b)
print(not not a == False)
1.7) (3)
1.8) (3)
number = 15
isPrime = True
if not isPrime:
print(number, "is not prime")
break
else:
print(number, "is prime")
print("The end")
1.9) (3)
b = 0
for a in range(1, 15, 3):
b += a − 1
print(a," ",b)
1.10) (3)
mysterystr(a, b, mystr ):
newstring = ""
for i in range(a,b):
if mystr[i] < 'I':
newstring += "#"
else:
newstring += mystr[i]
return newstring
str1 = "DeFgHiJkL"
str2 = mysterystr(4,7,str1)
print(str2)
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 4
i = 2
while i > 0:
j = 0
while j < 4:
if j % 3 == 0:
print("|",end="")
else:
print("*",end="")
j += 1
i −= 1
2.3 How many times does the outer while loop run? (2)
2.4 Rewrite the above code using a nested for loop structure (4)
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 5
A program was written to convert and display strings entered by the user into UpPeTy
Case. UpPeTy Case is when all the characters of the string alternate between upper
and lowercase. The user indicates he/she would like to stop entering words by using
the sentinel value of “!”
Example:
Enter your string: Hello
HeLlO
Enter your string: The cat jumped over the mat
ThE CaT JuMpEd oVeR ThE MaT
Enter your string: How is the Exam?
HoW Is tHe eXaM?
Enter your string: !
3 strings processed.
The function Reduce accepts a positive integer as input and reduces the number to a
single digit. It reduces a number by adding the individual digits of the number. If the
new number is not a single digit (i.e. it is > 9) then it repeats the process on the new
number. This process continues until a single digit is produced.
An emirp is a prime number whose reversal is also a prime. For example 17 is an emirp
since 71 (the reverse of 17) is also prime.
5.1 Write a function isPrime that takes an integer as a parameter and returns true if
the integer is a prime number. (10)
5.2 Write a function reverse that takes an integer as a parameter and returns the
reverse of the integer. For example, if 17 is input it returns 71. (5)
5.3 Write a program that outputs the first n emirps where n is input by the user.
(5)
Note your code should result in output as close to the examples given below as
possible.
Example 1:
Enter number of emirps: 10
3 5 7 11 13 17 31 37 71 73
Example 2:
Enter number of emirps: 60
3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157 167
179 181 191 199 311 313 337 347 353 359 373 383 389 701 709 727
733 739 743 751 757 761 769 787 797 907 919 929 937 941 953 967
971 983 991 1009 1021 1031 1033 1061
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 8
6.1 Write a function isFunny that returns True if a string passed to it is Funny and
False otherwise.
For example:
Given the string s = lmnop, the ordinal values of the characters are
108,109,110,111,112. sreverse=ponml and the ordinals are 112,111,110,109,108.
The absolute differences of the adjacent elements for both strings are 1,1,1,1, so
the string s is Funny and the function isFunny will return True.
Given the string s = lmop, the ordinal values of the characters are
108,109,111,112. sreverse=poml and the ordinals are 112,111,109,108. The
absolute differences of the adjacent elements for both strings are 1,2,1, so the
string s is Funny and the function isFunny will return True.
6.2 Write code to allow the user to specify how many strings they would like to test
and then for each string the program should output if the string is Funny or not.
You should make use of your function written in 6.1
Note your code should result in output as close to the examples given below as
possible.
Example 1: Example 2:
How many strings to test? 3 How many strings to test? 5
String 1: aceg String 1: xyGh
Is Funny Is Funny
String 2: xYz String 2: abdg
Is Funny Not Funny
String 3: Gold String 3: bCxz
Is Funny Not Funny
String 4: dog
Not Funny
String 5: Hannah
Is Funny
Char ASCII
value
0 48
9 57
A 65
Z 90
a 97
z 122