-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CodeBlock): Added 'rust' to supported languages
- Loading branch information
Showing
3 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
pub type Sequence = [u8; 12]; | ||
|
||
/// Returns the length of a sequence, given by 0 as an empty indicator | ||
pub fn length(sequence: &Sequence) -> usize { | ||
sequence.iter().position(|&x| x == 0).unwrap_or(12) | ||
} | ||
|
||
/// Rotates a sequence from the start index in the given direction. | ||
fn rotate(sequence: &Sequence, start_index: usize, forwards: bool) -> Sequence { | ||
let mut reordered = Sequence::default(); | ||
let sequence_length = length(sequence); | ||
let mut index: usize = start_index; | ||
let mut count: usize = 0; | ||
|
||
while count < sequence_length { | ||
reordered[count] = sequence[index]; | ||
|
||
if forwards { | ||
index = (index + 1) % sequence_length; | ||
} else { | ||
index = (index + sequence_length - 1) % sequence_length; | ||
} | ||
|
||
count += 1; | ||
} | ||
|
||
reordered | ||
} | ||
|
||
/// A symmetrical sequence is one that starting | ||
/// at any index and going in one direction. | ||
/// Whatever path has been taken, can be taken | ||
/// in the opposite direction. | ||
pub fn is_symmetrical(sequence: &Sequence) -> bool { | ||
for i in 0..length(sequence) { | ||
if &rotate(sequence, i, false) == sequence { | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} |