ccps506 Lab4 Rust
ccps506 Lab4 Rust
Given a reference to an input array, this function should count the number of “peaks” in
the array. A peak is defined as an element flanked on either side by elements smaller
than itself. The first and last elements of an array can still be considered peaks, if the
single element adjacent to it is smaller.
This function accepts a reference to an array of integers as input and returns a vector
where runs of identical integer values have been reduced to a single instance of that
integer. For example, for the input array:
[ 1, 1, 2, 4, 5, 5, 5, 5, 7, 9, 9, 1, 1 ]
The remove_runs function would return the Vector:
< 1, 2, 4, 5, 7, 9, 1 >
Notice that we are not simply removing all duplicate elements! If there are two runs of
1, then each of those runs should be distilled to a single 1. We may or may not cover
Vectors in class, but they are comparable to any other Vector type in any other
language. Check out the documentation for information on using them:
https://doc.rust-lang.org/std/vec/struct.Vec.html
3) fn count_and_remove_primes(items: &mut [u32]) -> u32
This function accepts a mutable reference to an array. It should iterate through the
array and determine if each number is prime. If the number is prime, set that element
to 0. Return the count of primes that were found and set to zero in this way.
In the game of chess, a rook can move anywhere along its row or column.
Given an n-by-n chessboard (parameter n) and an array of tuples representing row and
column positions of the rooks (parameter rooks), this function must return the number
of squares on the board that are safe from the rooks.
Note 1: Row and column indexes for the rooks start at 0. If the chessboard was 8x8,
then valid row and column indexes would be 0 to 7.
Note 2: You may assume the row and column indexes of each rook are legal given the
size of the chessboard. For example, if the chessboard is 8x8, no rook’s row or column
index will be off the board (>7).
In the same main.rs source file, you will implement a custom data type called Poly. This
type is nothing more than a struct containing a vector of integers:
struct Poly {
coeffs: Vec<i32>,
}
This vector represents a series of polynomial coefficients, where the index of the vector is the
degree of that term. For example, the vector <1, 2, 3> represents the polynomial 1x^0 + 2x^1
+ 3x^2. You will implement several operations over Poly, each of which is described below.
The + operator:
The sum of two Polys is defined component-wise. That means if a, b are two polynomials then
we define c = a + b as c.coeffs[i] = a.coeffs [i] + b.coeffs[i].
This method should be able to handle Polys of different lengths. When adding two Polys of
length m and n, where m > n, the resulting list should be of length m and the higher elements
of the shorter list is assumed to be zero. For example, for the following a, b:
a = <3,2,2,1,4>
b = <2,4,1>
a+b = <5,6,3,1,4>
The – operator:
Like the + operator, but this time performing element-wise subtraction. For example, for the
above a, b we have:
a-b = <1,-2,1,1,4>
The * operator:
Multiplication between two Polys is performed the same way polynomial multiplication
would be performed mathematically. Realize that the length of the resulting Poly will be
longer, because multiplying the highest degree terms together results in a degree that is the
sum of the two. E.g. a2 * b3 = (ab)5.
Thus, the earlier example evaluates as follows. Work it out on paper to convince yourself.
a = <3,2,2,1,4>
b = <2,4,1>
a*b = <6,16,15,12,14,17,4>
Multiplication can also be performed between a polynomial and an integer. In this case,
simply multiply that integer by each element in the polynomial.
The == operator:
Two Polynomial are considered equal if their vectors are equal (elements are the same).
String representation:
A Polynomial should be printed in reverse order, so the highest degree coefficient comes first.
The polynomial:
<3,2,2,1,4>
should have the string representation
“4x^4+x^3+2x^2+2x+3”
Implementation Details
To help you get started, template code snippets are given below for each operation (these
are in the cargo source file as well). In your main.rs source file, your job is to fill in these impl
blocks. You must also use the following namespaces:
use std::fmt;
use std::ops;
use std::cmp;
impl ops::Add<Poly> for Poly {
type Output = Poly;
fn add(self, other: Poly) -> Poly {
// Your + code here
}
}
Submission
Labs are to be completed and submitted individually. Submit your main.rs source file
containing all completed functions on D2L, under the submission for Lab 4.