0% found this document useful (0 votes)
10 views8 pages

24CLC10 Final

Uploaded by

sonloc17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

24CLC10 Final

Uploaded by

sonloc17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Final test Fundamentals of Programming CSC10012

Final test
75 minutes

1 Reverse Linked List (4 points)


Given the head of a singly linked list, reverse the list, and return the reversed list.
1 struct Node {
2 int data ;
3 Node * pnext ;
4 };

Example 1

• Input: head = [1,2,3,4,5]

• Output: [5,4,3,2,1]

Example 2

• Input: head = [1,2]

• Output: [2,1]

University of Science Faculty of Information Technology Page 1


Final test Fundamentals of Programming CSC10012

Example 3

• Input: head = []

• Output: []

Constraints:

• The number of nodes in the list is the range [0, 5000].

• −5000 <= N ode.data <= 5000

Hint: A linked list can be reversed iteratively or recursively. Could you implement both? You can
choose one way to implement in this exam.

2 Building Database (4 points)


Given a text file named data.csv which contains information below:

• id

• firstname

• lastname

• email

• email2

• profession

First, you need to define structure as follow:


1 struct Record {
2 int id ;
3 std :: string firstname ;
4 std :: string lastname ;
5 std :: string email ;
6 std :: string email2 ;
7 std :: string profession ;
8 };

Then, you use I/O libraries like <fstream> to open the file, and then process its content line by
line. Function prototype is defined as:

University of Science Faculty of Information Technology Page 2


Final test Fundamentals of Programming CSC10012

1 int number_records = 50;


2

3 Record * readCSV ( const std :: string & filename , int number_records ) {


4 Record * records = new Record [ number_records ];
5 // YOUR CODE HERE
6 return records ;
7 }
8

9 int main () {
10 const std :: string filename = " data . csv " ;
11 std :: vector < Record > records = readCSV ( filename ) ;
12

13 for ( const auto & record : records ) {


14 std :: cout << " ID : " << record . id
15 << " , First Name : " << record . firstname
16 << " , Last Name : " << record . lastname
17 << " , Email : " << record . email
18 << " , Email2 : " << record . email2
19 << " , Profession : " << record . profession << std :: endl ;
20 }
21

22 return 0;
23 }

Hint: You may need to read file line by line as


1 std :: string line ;
2 while ( std :: getline ( stream , line ) ) ...

Pass each line to a listingstream and read the fields:


1 std :: istringstream s ( line ) ;
2 std :: string field ;
3 while ( getline (s , field , ’ , ’) ) ...

3 1-Dimensional Convolutional (2 points)


In deep learning, convolutional layers have been major building blocks in many deep neural net-
works. The easiest way to understand a convolution is by thinking of it as a sliding window
function applied to a matrix. In this problem, you will implement in the case of kernel size equals
to 2.

University of Science Faculty of Information Technology Page 3


Final test Fundamentals of Programming CSC10012

Now let’s see how it work in the case of kernel size equals to 1. Assume that the value in our kernel
(also known as “weights”) is “2”, we will multiply each element in the input vector by 2, one after
another until the end of the input vector, and get our output vector. The size of the output vector
is the same as the size of the input.

First, we multiply 1 by the weight, 2, and get “2” for the first element. Then we shift the kernel
by 1 step, multiply 2 by the weight, 2 to get “4”. We repeat this until the last element, 6, and
multiply 6 by the weight, and we get “12”. This process produces the output vector.

The different sized kernel will detect differently sized features in the input and, in turn, will result
in different sized feature maps. Let’s look at another example, where the kernel size is 1x2, with
the weights “2”. Like before, we slide the kernel across the input vector over each element. We
perform convolution by multiply each element to the kernel and add up the products to get the
final output value. We repeat this multiplication and addition, one after another until the end of
the input vector, and produce the output vector.

University of Science Faculty of Information Technology Page 4


Final test Fundamentals of Programming CSC10012

First, we multiply 1 by 2 and get “2”, and multiply 2 by 2 and get “2”. Then we add the two
numbers, 2 and 4, and we get “6”–that is the first element in the output vector. We repeat the
same process until the end of the input vector and produce the output vector.

With given function prototype, your task is completing the function conv1D with kernel size 2.
1 int * conv1D ( const int * input , int inputSize , const int * kernel , int kernelSize ,
int & outputSize ) {
2 // Calculate the size of the output
3 outputSize = inputSize - kernelSize + 1;
4 // YOUR CODE HERE
5 }
6

7 int main () {
8 int input [] = {1 , 2 , 3 , 4 , 5};
9 int kernel [] = {2 , 2};
10 int inputSize = sizeof ( input ) / sizeof ( input [0]) ;
11 int kernelSize = sizeof ( kernel ) / sizeof ( kernel [0]) ;
12

13 int outputSize = 0;
14

University of Science Faculty of Information Technology Page 5


Final test Fundamentals of Programming CSC10012

15 try {
16 int * result = conv1D ( input , inputSize , kernel , kernelSize , outputSize ) ;
17

18 std :: cout << " Convolved Output : " ;


19 for ( int i = 0; i < outputSize ; ++ i ) {
20 std :: cout << result [ i ] << " " ;
21 }
22 std :: cout << std :: endl ;
23

24 delete [] result ;
25 } catch ( const std :: exception & e ) {
26 std :: cerr << e . what () << std :: endl ;
27 }
28

29 return 0;
30 }

Requirement: You need to use pointer to complete this task.

4 Levenshtein distance (2 points)


In information theory and computer science, the Levenshtein distance is a metric for measuring
the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance
between two strings is defined as the minimum number of edits needed to transform one string into
the other, with the allowable edit operations being insertion, deletion, or substitution of a single
character.

Example
The Levenshtein distance between “kitten” and “sitting” is 3, since the following three edits
change one into the other, and there isn’t a way to do it with fewer than three edits:

1. kitten sitten (substitution of ‘k’ with ‘s’)

2. sitten sittin (substitution of ‘e’ with ‘i’)

3. sittin sitting (insert ‘g’ at the end).

Given the pseudocode of Levenshtein distance as below, your task is implement this function in
C++.

University of Science Faculty of Information Technology Page 6


Final test Fundamentals of Programming CSC10012

1 int L e ve n s ht e i nD i s ta n c e ( char s [1.. m ] , char t [1.. n ])


2 // d is a table with m +1 rows and n +1 columns
3 declare int d [0.. m , 0.. n ]
4

5 for i from 0 to m
6 d [i , 0] := i
7 for j from 0 to n
8 d [0 , j ] := j
9

10 for i from 1 to m
11 for j from 1 to n
12 {
13 if s [ i ] = t [ j ] then cost := 0
14 else cost := 1
15 d [i , j ] := minimum (
16 d [i -1 , j ] + 1 , // deletion
17 d [i , j -1] + 1 , // insertion
18 d [i -1 , j -1] + cost // substitution
19 )
20 }
21

22 return d [m , n ]

University of Science Faculty of Information Technology Page 7


Final test Fundamentals of Programming CSC10012

Regulations
Please follow these regulations:

• Please arrive at the room 15 minutes early to settle in and carefully check your computer
(keyboard, mouse, network, IDEs). You are allowed to use any IDE.

• During the test, do not use the internet or mobile phone.

• After completing the test, check your submission before and after uploading to Moodle.

• Before leaving, delete your code, turn off the computer, and arrange the chairs properly.

• Prohibited libraries: <set>, <unordered_set>, <map>, <unordered_map>, <algorithm>,


<list>, <stack>, <queue>.

Your source code must be contributed in the form of a compressed file and named your submission
according to the format StudentID.zip. Here is a detail of the directory organization:

StudentID
Exercise 1.cpp
Exercise 2.cpp
Exercise 3.cpp
Exercise 4.cpp

The end.

University of Science Faculty of Information Technology Page 8

You might also like