//! Implement functions usint `Iterator` trait struct FindIter<'s, T: Eq> { query: &'s [T], base: &'s [T], curr: usize, } impl Iterator for FindIter<'_, T> { type Item = usize; fn next(&mut self) -> Option { todo!() } } /// Returns an iterator over substring query indexes in the base. pub fn find<'s, T: Eq>(query: &'s [T], base: &'s [T]) -> impl 's + Iterator { FindIter { query, base, curr: 0, } } /// Implement generic fibonacci iterator struct FibIter { // TODO: remove `_marker` and add necessary fields as you want _marker: std::marker::PhantomData, } impl + Copy> FibIter { fn new(first: T, second: T) -> Self { todo!() } } impl Iterator for FibIter where T: std::ops::Add + Copy, { type Item = T; fn next(&mut self) -> Option { todo!() } } /// Returns and iterator over the generic fibonacci sequence starting from `first` and `second`. /// This is a generic version of `fibonacci` function, which works for any types that implements `std::ops::Add` trait. pub fn fib(first: T, second: T) -> impl Iterator where T: std::ops::Add + Copy, { todo!("remove below"); std::iter::empty() } /// Endpoint of range, inclusive or exclusive. #[derive(Debug)] pub enum Endpoint { /// Inclusive endpoint Inclusive(isize), /// Exclusive endpoint Exclusive(isize), } struct RangeIter { // TODO: add necessary fields as you want } impl RangeIter { fn new(endpoints: (Endpoint, Endpoint), step: isize) -> Self { todo!() } } impl Iterator for RangeIter { type Item = isize; fn next(&mut self) -> Option { todo!() } } /// Returns an iterator over the range [left, right) with the given step. pub fn range(left: Endpoint, right: Endpoint, step: isize) -> impl Iterator { todo!("remove below"); std::iter::empty() } /// Write an iterator that returns all divisors of n in increasing order. /// Assume n > 0. /// /// Hint: trying all candidates from 1 to n will most likely time out! /// To optimize it, make use of the following fact: /// if x is a divisor of n that is greater than sqrt(n), /// 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 } impl Iterator for Divisors { type Item = u64; fn next(&mut self) -> Option { todo!() } } /// Returns an iterator over the divisors of n. pub fn divisors(n: u64) -> impl Iterator { Divisors { n, // TODO: you may define additional fields here } }