FOP1005 - Fundamentals of Programming Mid Trimester Test
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
FOP1005 - Fundamentals of Programming
Mid Trimester Test
Grading 20% of unit
Unit Name: Fundamentals of Programming
Unit Code: FOP1005
. Attempt all questions.
. Submit the Declaration of Originality along with the test. You can find it on Moodle.
. Coding questions need to be submitted as different python files. An- swer all the theory questions in one file.
. This is an open book test so you can use all the resources (Lecture slides, Practicals and google) to complete the test.
. Zip all the files (including codes, and theory questions) and upload them on Moodle.
. All codes should be executed before submission, any non-executing code will be marked as zero.
. Any code(s) generated with the help of ChatGPT will be considered as academic misconduct.
GENERAL INSTRUCTIONS:
This test consists of Five (5) questions with a total of 100 marks. (making 20% of unit grading).
1. Question 01 (Total: 20 marks): Data types, Loops, Python and Linux.
(a) Modify the following code (Q1PartA.py) to: (05 marks)
index 3, with every character on the even index of entered string as capital.
(e.g.,’Fundamentals of Programming’ would become ’GnImMaRgOrP Fo sLaTnEmAd’). (2)
ii. Give three different methods to duplicate the string entered and print the output. (1)
iii. Print the entered string going backward (using slicing), string from the last element
print every third element and stop at the fifth element in the string.
(e.g., ’FOP1005 - Fundamentals of Programming’ would be ’gmrrfstmn 5’). (2)
#
# Q1PartA.py - Code for Q1 Part A .
#
Misc = input('Enter a string . . . ' )
print( 'Entered String: ' , end = '' )
print(Misc)
# Example for-range loop for printing forward .
# for index in range (0, len(Misc), 1)
# Add your code for Q1.Part A (i) below this line .
10
print()
# Add your code for Q1.Part A (ii) below this line .
# Add your code for Q1.Part A (iii) below this line .
15
# end of file .
# Submit this file as a separate python file .
(b) We have discussed three control structures (i.e., if, while and for. Give an example of when each one is used, and also describe the difference between for and while loops discussing important parts of the loops. (06 marks)
(c) What type of variable will result from each of the following: (05 marks)
(d) Write the Linux command for the following: (04 marks)
i. (you are currently in Prac02/subdir2/subsub1) copy all the files to Prac03/subdir1. (2)
FOP1005
Prac01
Prac02
subdir1
subdir2
subdir1
subdir2
iii. Linux command to rename a directory that has many files and sub-directories in it. (1)
2. Question 02 (Total: 20 marks): Arrays and Plotting.
(a) Initialise and print the numpy arrays to get the required output (Q2PartA.py).
(05 marks)
ii. Use element-wise array function to multiply the elements of the array by 2 (generated
in part(i)) resulting in doubled array . (1)
iii. Use while loop to print all elements of doubled array divisible by 7 and 9 . (2)
(b) Write a program in python (name file as Q2PartB.py) to: (06 marks)
i. Initialise two arrays (Array1 and Array2) from the lists A = [190, 220, 360]
ii. Reverse the order of the elements in Array1 and Array2 using slicing. (2)
iii. Convert Array1 to float and Array2 to string using numpy array initialisation. (2)
(c) Following code (Q2PartC.py) generates three arrays Array1, Array2 and Array3,
modify the code as: (04 marks)
650 (inclusive). (1)
ii. Make a 1 Row two columns subplot showing the first plot as a scatter plot of Array2,
and Array1 and the second plot as a line plot of Array3 and Array1. (2)
iii. Set title (Plot 1/2) and x (X - values) and y (Y - values) labels for both plots and
set colour to red and markers to the triangles for the line plot. (1)
(d) Modify the code (Q2.PartD.py), as per the instructions (see the code comments) to get the required output. (05 marks)
3. Question 03 (Total: 20 marks): Functions/List Comprehension.
(a) Write a function to take a list of integers/floats as an argument and return the
largest of all elements in the list. (03 marks)
(b) Write a function to take three strings as arguments and return a concatenation (with
a space in between and changed order) of all strings (see example). (04 marks) (e.g., ’Curtin’, ’College’, ’Australia’ would be ’Australia Curtin College’)
(c) Describe with examples the difference between ’r’ ,’w’, ’rb’ and ’wb’ modes of file
IOs in python. (05 marks)
(d) Write the list comprehension to generate following: (08 maks)
values greater than or equal to 12.25. (e.g., [12.3, 14.1, 12.25, 12.22, 9.13] becomes
[12.3, 14.1, 12.25]) (2)
ii. The number from -100 to -25 which are multiple of 5 (e.g., [-25, -30, -35, -40, , -100]) (2)
iii. The number from 14 to 39 which are multiple of 2 and 6 (e.g., [18, 24, 30, 36]) (2)
iv. If a list (wordlist) contains some strings create a new list with the second and last
characters of each string in the list capitalised.
(e.g., [’Perth’, ’Australia’, ’curtin’] would be [’PeRtH’, ’AuStraliA’, ’cuRtiN’]) (2)
4. Question 04 (Total: 20 marks): Functions/List Comprehension.
(a) You have an array of integers in Python that you want to resize to double its original length. Write a Python function that takes the array as input and returns the resized array. Your function should use the numpy library to perform the resizing operation.
(see example code Q4PartA.py) (05 marks)
#
# Q4PartA.py - Code for Q4 Part A .
#
# Import required packages (01 mark), below this line .
def double_array_size(array):
# Add code below this line (02 marks)
7
return doubled_array
def main():
original_array = [1 , 2 , 3 , 4 , 5]
# Call the defined function here (01 mark)
print(resized_array)
# Call the main function properly below this line (01 mark)
14
# end of file .
# Submit this file as a separate python file .
(b) Modify to the following code to shift, rotate and crop the image. You can choose
the coordinates of shifting yourself.
(05 marks)
#
# Q4Partb.py - Code for Q4 Part B .
#
# Import required packages (01 mark), below this line .
def EditImage(original_image):
# Add code below this line to shift, rotate and crop (02 marks)
7
return edited_images
def main():
original_image = misc.face(gray=True)
# Call the defined function here (01 mark)
plt.imshow(edited_image, cmap=plt.cm.gray)
plt.show()
# Call the main function properly below this line (01 mark)
15
# end of file .
# Submit this file as a separate python file .
i. To create a copy of a file with a new name. (2)
ii. To change the directory to the parent directory. (2)
iii. To print words, lines and characters of a file without opening it. (2)
iv. To see the contents of a file without opening it. (2)
v. To accept parameters at the command line rather than hard coding them within the
program. (2)
5. Question 05 (Total: 20 marks): Files and grid.
(a) Create a csv file called Values.csv, where each line of the file contains a one word
ValueID and four values separated by commas, like below: (08 marks)
value_1,14.56 ,25.5 ,30.01 ,90.08
value_2,14.46 ,15.9 ,32.11 ,89.34
Write a python function that scans through the file and determines the number of values for each ValueID which are greater than 30.0. The code (Q5PartA.y) should contain a main function and proper function calls.
(b) Explain and show with figures the difference between Von Neumann and Moore neigh-
bourhood (05 marks)
(c) In lecture 05 refers to heat.py and change the code to read the heat values from a file (csv
file) with the image size of 20, 20, as per tutorial 05. (05 marks)
(d) Write an example README file for this Mid Trimester test. (02 marks)
2023-08-17