Week 02 Laboratory Exercises
Objectives
- To practice using Rust's basic collection tyoes
- Exposure to simple derive macros
- Understand and use Rust's error handling philosopy, and the try trait
Activities To Be Completed
The following is a list of all the activities available to complete this week...
- Tribonacci
- My Caesar
- To Upper (Investigating strings!)
- Collections
- Data analysis
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
Create a new directory for this lab called lab02
,
change to this directory,
and fetch the provided code for this week
by running these commands:
mkdir lab02 cd lab02 6991 fetch lab 02
Or, if you're not working on CSE, you can download the provided code as a tar file.
Exercise:
Tribonacci
In this task, you will write a function that computes a variable number of Tribonacci numbers , and also their sum.
Your program should read a single integer, n
,
from the command-line arguments,
and subsequently compute the first
n
Tribonacci numbers,
along with their sum.
If no command-line arguments are given,
you should assume the value of n
to be 10.
Invalid input should be handled by returning a custom
error message "Please enter a valid size!"
through the struct TribonacciError
,
and then subsequently exiting the program.
Your program need only be able to calculate from
n = 3
to
n = 145
inclusive.
Note that n = 145
produces very large
numbers that increase far past the width of a
i64
type.
You may want to (strongly) consider using one of
i128
or u128
instead,
both of which can fit the largest number in that
sequence.
6991 cargo run -- 4 Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/tribonacci 4` Values: [1, 1, 1, 3] Sum: 6 6991 cargo run -- 10 Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/tribonacci 10` Values: [1, 1, 1, 3, 5, 9, 17, 31, 57, 105] Sum: 230 6991 cargo run -- -1 Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/tribonacci -1` Error: Please enter a valid size
When you think your program is working,
you can use autotest
to run some simple automated tests:
6991 autotest
When you are finished working on this exercise, you must submit your work by running give
:
6991 give
You must run give
before Week 3 Wednesday 21:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give
must be entirely your own.
Exercise:
My Caesar
In this activity, your task is to write a program that encrypts a message using a Caesar cipher.
The program should read in a single command-line argument,
which is the number of positions to shift each letter (i32
).
If there is no command-line argument,
or if the argument is not a number,
the program should use a default shift of 5
.
The program should then iterate over each line of standard input, shifting each letter by the specified number of positions. The shifted line should then be printed to standard output.
However, only letters of the alphabet
(i.e. 'A'..='Z'
, 'a'..='z'
)
should be shifted.
All other characters should be printed as-is.
Shifted letters should wrap around the alphabet, so that 'A' shifted by 1 becomes 'B', 'Z' shifted by 1 becomes 'A', 'y' shifted by 3 becomes 'b'.
If the provided shift is negative, you should treat the shift as operating in the opposite direction.
For example, 'A' shifted by -1 becomes 'Z', 'Z' shifted by -1 becomes 'Y', 'b' shifted by -3 becomes 'X'.
A shift of 0
should
not modify the input text at all.
For example:
6991 cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/my_caesar` hello! Shifted ascii by 5 is: mjqqt! Dr. Taylor Swift is the worlds greatest musician! Shifted ascii by 5 is: Iw. Yfdqtw Xbnky nx ymj btwqix lwjfyjxy rzxnhnfs! 6991 cargo run -- 10 Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/my_caesar` And I know it's long gone and 👋 That magic's not here no more 😭🫀🇧🇷🇪🇦🇰 Shifted ascii by 10 is: Kxn S uxyg sd'c vyxq qyxo kxn 👋 Drkd wkqsm'c xyd robo xy wybo 😭🫀🇧🇷🇪🇦🇰 T_T crying Shifted ascii by 10 is: D_D mbisxq
When you think your program is working,
you can use autotest
to run some simple automated tests:
6991 autotest
When you are finished working on this exercise, you must submit your work by running give
:
6991 give
You must run give
before Week 3 Wednesday 21:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give
must be entirely your own.
Exercise:
To Upper (Investigating strings!)
This activity is an investigation of Rust's strings!
You have been provided a simple crate,
to_upper,
which does not compile!
Your task is to modify to_upper/src/main.rs
so that it takes in a single command-line argument
and converts the appropriate characters to uppercase.
It may help to RTFC!
6991 cargo run -- "hello world!!!" Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/to_upper 'hello world'\!''\!''\!''` arg = "hello world!!!" upp = "HELLO WORLD!!!" 6991 cargo run -- "uwu🥺👉👈" Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/to_upper 'uwu🥺👉👈'` arg = "uwu🥺👉👈" upp = "UWU🥺👉👈"
When you think your program is working,
you can use autotest
to run some simple automated tests:
6991 autotest
When you are finished working on this exercise, you must submit your work by running give
:
6991 give
You must run give
before Week 3 Wednesday 21:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give
must be entirely your own.
Exercise:
Collections
- Sequences
Vec
VecDeque
LinkedList
- Maps
HashMap
BTreeMap
- Sets
HashSet
BTreeSet
- Misc.
BinaryHeap
In this activity, we'll be exploring the performance of first three
(Vec
, VecDeque
, and LinkedList
),
and HashMap
collections.
For each of the four collections above, your code should:
- Create a new instance of the collection.
- Add some
MAX_ITER
elements to it. - Remove
MAX_ITER
elements from it. - Time each of the above operations.
Before you start, please write down some of your expectations about what you think will be the most performant collection for these operations.
The analysis of Vec
and VecDeque
has already been completed for you in the provided code.
This exercise also contains a theory section. At the end of your main function, you should write a comment that answers the following questions:
- Which collection type was the fastest for adding and removing elements?
- Why do you think this was the case?
- Is there any significant difference between
Vec
andVecDeque
deletion?- If so, why? If not, why not?
- When would you consider using
VecDeque
overVec
? - When would you consider using
LinkedList
overVec
? - Did the results suprise you? Why or why not?.
When you are finished working on this exercise, you must submit your work by running give
:
6991 give
You must run give
before Week 3 Wednesday 21:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give
must be entirely your own.
Exercise:
Data analysis
In this exercise, you are taking on the role of a prospective data analyst and have gotten your hands on a dataset of (fictional!) CSE enrolments.
The dataset is in the file enrolments.psv
and
each row contains the following columns (in order):
- UNSW Course Code
- UNSW Student Number
- Name
- UNSW Program
- UNSW Plan
- WAM
- UNSW Session
- Birthdate
- Sex
Each row of data represents one enrolment. One student may have multiple rows, as they may be enrolled in multiple courses.
You've been asked to process the data, and output answers to the following questions:
- How many unique students are there?
- What is the most common course (with how many students)?
- What is the least common course (with how many students)?
- What is the average WAM of all students in CSE (to 2 decimal places)?
- Given a specific student number, output their details?
Note that you can assume that there will be no two equally most-common or equally least-common courses in the dataset -- i.e. there will always be exactly one answer.
6991 cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.01s Running `target/debug/data_analysis` Number of students: 8065 Most common course: COMP1511 with 953 students Least common course: BIOM9002 with 1 students Average WAM: 61.94 Enter a student id: 5231508 == STUDENT FOUND == Name: Xu, Michael Rahul Program: 7003/1 Plan: COMPSS WAM: 75.09 Session: 22T2 Birthdate: 20010410 Sex: M
If the given student is not found, your program should print an error:
6991 cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.50s Running `target/debug/data_analysis` Number of students: 8065 Most common course: COMP1511 with 953 students Least common course: BIOM9002 with 1 students Average WAM: 61.94 Enter a student id: abcd == NOT FOUND ==
When you are finished working on this exercise, you must submit your work by running give
:
6991 give
You must run give
before Week 3 Wednesday 21:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give
must be entirely your own.
Submission
When you are finished each exercise make sure you submit your work by running give
.
You can run give
multiple times.
Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Week 3 Wednesday 21:00:00 to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
Automarking will be run several days after the submission deadline,
using test cases different to those autotest
runs for you.
(Hint: do your own testing as well as running autotest
.)
After automarking is run you can view your results here.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
6991 classrun -sturec