Assignment 7 Done

This commit is contained in:
static
2024-10-30 04:43:50 +00:00
parent f0339a4ce9
commit 858ef49b59
4 changed files with 150 additions and 40 deletions

View File

@@ -10,7 +10,15 @@ impl<T: Eq> Iterator for FindIter<'_, T> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
todo!()
while self.curr + self.query.len() <= self.base.len() {
let curr = self.curr;
self.curr += 1;
if &self.base[curr..(curr + self.query.len())] == self.query {
return Some(curr);
}
}
None
}
}
@@ -25,13 +33,13 @@ pub fn find<'s, T: Eq>(query: &'s [T], base: &'s [T]) -> impl 's + Iterator<Item
/// Implement generic fibonacci iterator
struct FibIter<T> {
// TODO: remove `_marker` and add necessary fields as you want
_marker: std::marker::PhantomData<T>,
first: T,
second: T,
}
impl<T: std::ops::Add<Output = T> + Copy> FibIter<T> {
fn new(first: T, second: T) -> Self {
todo!()
Self { first, second }
}
}
@@ -42,7 +50,10 @@ where
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
todo!()
let value = self.first;
self.first = self.second;
self.second = self.second + value;
Some(value)
}
}
@@ -53,8 +64,7 @@ pub fn fib<T>(first: T, second: T) -> impl Iterator<Item = T>
where
T: std::ops::Add<Output = T> + Copy,
{
todo!("replace `std::iter::empty() with your own implementation`");
std::iter::empty()
FibIter::new(first, second)
}
/// Endpoint of range, inclusive or exclusive.
@@ -68,12 +78,25 @@ pub enum Endpoint {
}
struct RangeIter {
// TODO: add necessary fields as you want
curr: isize,
step: isize,
last: isize,
}
impl RangeIter {
fn new(endpoints: (Endpoint, Endpoint), step: isize) -> Self {
todo!()
let c = if step > 0 { 1 } else { -1 };
Self {
curr: match endpoints.0 {
Endpoint::Inclusive(v) => v,
Endpoint::Exclusive(v) => v + c,
},
step,
last: match endpoints.1 {
Endpoint::Inclusive(v) => v,
Endpoint::Exclusive(v) => v - c,
},
}
}
}
@@ -81,14 +104,19 @@ impl Iterator for RangeIter {
type Item = isize;
fn next(&mut self) -> Option<Self::Item> {
todo!()
if self.step > 0 && self.curr > self.last || self.step < 0 && self.curr < self.last {
None
} else {
let value = self.curr;
self.curr += self.step;
Some(value)
}
}
}
/// Returns an iterator over the range [left, right) with the given step.
pub fn range(left: Endpoint, right: Endpoint, step: isize) -> impl Iterator<Item = isize> {
todo!("replace `std::iter::empty() with your own implementation`");
std::iter::empty()
RangeIter::new((left, right), step)
}
/// Write an iterator that returns all divisors of n in increasing order.
@@ -100,14 +128,27 @@ pub fn range(left: Endpoint, right: Endpoint, step: isize) -> impl Iterator<Item
/// then n/x is a divisor of n that is smaller than sqrt(n).
struct Divisors {
n: u64,
// TODO: you may define additional fields here
curr: u64,
vec: Vec<u64>,
}
impl Iterator for Divisors {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
todo!()
while self.curr * self.curr <= self.n {
let curr = self.curr;
self.curr += 1;
if self.n % curr == 0 {
if curr * curr != self.n {
self.vec.push(self.n / curr);
}
return Some(curr);
}
}
self.vec.pop()
}
}
@@ -115,6 +156,7 @@ impl Iterator for Divisors {
pub fn divisors(n: u64) -> impl Iterator<Item = u64> {
Divisors {
n,
// TODO: you may define additional fields here
curr: 1,
vec: vec![],
}
}