rotation

Crates.iorotation
lib.rsrotation
version0.1.1
sourcesrc
created_at2021-08-27 03:30:37.425242
updated_at2021-08-27 03:30:37.425242
descriptionRotate your list clockwise and anti-clockwise
homepage
repositoryhttps://github.com/Inoshy/rust-book-helper/tree/master/problems/rotation
max_upload_size
id442880
size6,297
Tausif (tausifcreates)

documentation

https://docs.rs/rotation

README

rotation

The idea for more efficient algorithm was derived from this stack overflow answer.

To quote from the answerer, 'Reversing three times is simplest but moves every element exactly twice, takes O(N) time and O(1) space. It is possible to circle shift an array moving each element exactly once also in O(N) time and O(1) space.'

For more info on how it adds up together, please refer to the answer (and other answers as well).

This crate exposes 2 apis, namely rotate_clock function that you will use to rotate your list clockwise, and rotate_anticlock for vice versa. below 2 examples are given to demonstrate how to use them.

Quick Start

use rotation::interface as rotator;

fn main() {
    let mut list = [1, 2, 3, 4, 5];

    let spin = 2;

    // rotate clockwise
    let rotated = rotator::rotate_clock(&mut list, spin);

    println!("{:?}", rotated); // [4, 5, 1, 2, 3];
 
    // rotate anti clockwise
    let rotated = rotator::rotate_anticlock(&mut list, spin);

    println!("{:?}", rotated); // [3, 4, 5, 1, 2];
}
Commit count: 0

cargo fmt