COMP1005 Exam Sem1 2017
COMP1005 Exam Sem1 2017
Department of Computing
EXAMINATION
End of Semester 1, 2017
6
Materials
7
none
8
Calculator
9
No calculators are permitted in this exam
10
Instructions to Students 11
Student to attempt all questions. Answers to be written in blue or black pen in the 12
supplied exam booklet.
13
14
15
16
17
18
Total ________
End of Semester 1, 2017
COMP1005 Fundamentals of Programming
c) (6 marks). How would you modify bucket2.py to allow for deletion of an entry, based on
its position in the list? You should modify the prompts, and check the index is valid before
deleting. (Hint: use del bucket[x] to delete item x+1)
auspops.py
pops = {'New South Wales': 7757843, 'Victoria' : 6100877,
'Queensland' : 4860448, 'South Australia' : 1710804,
'Western Australia' : 2623164, 'Tasmania': 519783,
'Northern Territory' : 245657,
'Australian Capital Territory': 398349}
print(pops['Tasmania'])
e) (4 marks). Provide code to modify auspops.py to print out each state and population on a
separate line and then print the total population
Page 1 of 7
End of Semester 1, 2017
COMP1005 Fundamentals of Programming
f) (2 marks). What would the output of the program regtest.py be if it was given the input file
phone.txt.
regtest.py phone.txt
fileobj = open('phone.txt')
data = fileobj.readlines()
g) (3 marks). Provide three changes that would be required for regtest.py to completely
and specifically match each of the number formats in the file.
Page 2 of 7
End of Semester 1, 2017
COMP1005 Fundamentals of Programming
a) (6 marks) The program below, prettyface.py, displays an image of a critter. Write some
additional code to:
i) create a new image, cropped_face, which crops the image by 100 pixels
on each side, and display the new image
ii) create a new image, pixel_face, which slices the image show every 20th
pixel, and display the new image
prettyface.py
import matplotlib.pyplot as plt
from scipy import ndimage
from scipy import misc
face = misc.face(gray=True)
plt.imshow(face, cmap=plt.cm.gray)
plt.show()
resizeme.py
import numpy as np
data = np.zeros((2,2,2))
data[0,1,0] = 1
data[1,0,1] = 2
print(data)
data.resize(1,8)
print(data)
c) (4 marks). Gridding algorithms use neighbouring cells to calculate the next value for the
site (centre cell). With a diagram, show the difference between von Neumann
neighbourhoods and Moore neighbourhoods
Page 3 of 7
End of Semester 1, 2017
COMP1005 Fundamentals of Programming
d) (7 marks). The following code creates three arrays: t, t2 and t3. Modify the code to:
multilineplot.py
import numpy as np
import matplotlib.pyplot as plt
Page 4 of 7
End of Semester 1, 2017
COMP1005 Fundamentals of Programming
a) (4 marks) List two (2) benefits of using Pandas tables as compared to using numpy
arrays.
b) (10 marks). Describe what each line of the following code, weather.py, does - given the
input file march4.csv.
weather.py march4.csv
import matplotlib.pyplot as plt "Minimum temperature (C)",
import pandas as pd "Maximum temperature (C)","9am
Temperature (C)","3pm Temperature
df = pd.read_csv('march4.csv') (C)"
20.1,37.7,28.2,37.2
print(df.head()) 22.9,29.9,24.1,25.1
print(df.tail()) 20.3,35.2,26.5,32.6
print(df.describe()) […]
12.9,24.9,16.9,24.3
plt.figure() 12.1,28.0,18.6,27.5
df.plot() 9.6,29.9,20.7,29.5
plt.show()
c) (6 marks). The following revised weather.py code adds a new column, temprange.
Provide code to:
df = pd.read_csv('march4.csv')
df = df.assign(temprange = df['Maximum temperature (C)'] -
df['Minimum temperature (C)'])
plt.figure()
df.plot()
plt.show()
Page 5 of 7
End of Semester 1, 2017
COMP1005 Fundamentals of Programming
c) (4 marks). Given the following script, myexp.sh, called as: bash myexp.sh exp1
myexp.sh
#!/bin/bash
mkdir $1
cd $1
cp ~/code/hello.py .
mkdir output
d) (5 marks) What is the output of the following code, commandline.py, when called as:
commandline.py
import sys
print(sys.argv)
fileobj = open(sys.argv[1])
numlines = int(sys.argv[2])
for i in range(numlines):
line = fileobj.readline().strip()
print('Line ' + str(i) + ' is: ' + line)
Page 6 of 7
End of Semester 1, 2017
COMP1005 Fundamentals of Programming
a) (14 marks). The following questions relate to the BankAccount class below:
i) (4 marks) Identify and name the included class variables and instance
variables. Indicate if they are class or instance variables.
ii) (7 marks) Add two (2) instance methods called deposit() and
withdraw() which increase and decrease the balance of the account.
Make sure the withdraw() method doesn't allow the class to go into
overdraft.
b) (6 marks). Two approaches to sharing code are publishing notebooks and publishing
packages. Describe each approach (4 marks) and the situations each is suited to (2
marks).
END OF EXAMINATION
Page 7 of 7