Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

UNIVERSITY OF KWAZULU-NATAL

SCHOOL OF MATHEMATICS, STATISTICS


& COMPUTER SCIENCE

MAIN EXAMINATION

MAY 2019

COURSE AND CODE


Introduction to Computer Science COMP100P1

DURATION: 3 Hours TOTAL MARKS: 100

INTERNAL EXAMINER: Rosanne Els


INTERNAL MODERATOR: Anban Pillay

THIS EXAM CONSISTS OF 9 PAGES INCLUDING THIS ONE.


PLEASE ENSURE THAT YOU HAVE ALL PAGES.

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

Question 1 - Program Output [30]


Give the exact output of each of the following programs. If there is no output due to
error(s) then explain the error briefly.

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)

mystring = "Exam day"


str1=mystring[1:4]
str2=str1[::-1]
print(str1)
print(str2)

1.8) (3)
number = 15
isPrime = True

for i in range(2, number):


if number % i == 0:
isPrime = False

print("i is", i, end=" : ")

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

Question 2 – for/while loops [10 marks]


Study the following code and answer the questions that follow:

i = 2
while i > 0:
j = 0
while j < 4:
if j % 3 == 0:
print("|",end="")
else:
print("*",end="")
j += 1
i −= 1

2.1 Give the output of the code. (2)

2.2 How many times is the if condition tested? (2)

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

Question 3 - Find the errors [10 marks]

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 “!”

Below is an example of expected output of the program.

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.

Unfortunately some mischievous person has changed the program so it no longer


works as expected. Identify 5 errors. For each error indicate the line number, what the
error is and how BEST to fix it.

def UpPeTy(mystr): #line 1


for i in range(len(mystr)): #line 2
ch = mystr[i]; #line 3
if (i % 2 != 0): #line 4
newstr += ch.upper() #line 5
else: #line 6
newstr += ch.lower() #line 7
print(newstr) #line 8
#line 9
nextword = input("Enter your string: ") #line 10
count = 0 #line 11
while (nextword != "!"): #line 12
count = 1 #line 13
print(UpPeTy(nextword)) #line 14
nextword = input("Enter your string: ") #line 15
print(count,"words processed.") #line 16
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 6

Question 4 – Reduce [15 marks]

Write a Python program that


 reads a list of positive (>0) integer values from the user, terminated by a zero (0).

 passes each value read in as an argument to a function called Reduce,


(described below), that returns a single digit positive integer. i.e. integers
between 1 to 9 inclusive,

 displays the values returned by method Reduce.

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.

For example, given the number 234785,


we have 234785  2+3+4+7+8+5= 29
29  2 + 9 = 11
11  1 + 1 = 2

i.e. Reduce(234785) returns 2.

If a negative number is read in from the user, it must be treated as an error.

Example of program execution:


Enter value: 234785
Answer: 2
Enter value: 123456789
Answer: 9
Enter value: 12
Answer: 3
Enter value: -8
Error
Enter value: 0
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 7

Question 5 - emirps [20 marks]


A prime number is an integer greater than 1 that is not divisible, without remainder, by
any integer except 1 and itself.

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

Question 6: Funny Strings [15 marks]


To determine whether a string is funny, create a copy of the string in reverse e.g.
abc→cba. Iterating through the characters of each string, compare the absolute
difference of the ordinal values of the characters at positions 0 and 1, 1 and 2 and so on
to the end. If the list of absolute differences is the same for both strings, they are funny.
The case of the characters should be ignored. The strings are case insensitive. This
means that we do not distinguish between upper- and lower-case characters.

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

Appendix A: Python Reference


Useful functions in the math module:
University of KwaZulu-Natal, May 2019 Main Examination: COMP100 P1 9

Useful functions in the string module:

Operator precedence table: ASCII codes of some characters:

Char ASCII
value
0 48
9 57
A 65
Z 90
a 97
z 122

You might also like