Skip to content

Instantly share code, notes, and snippets.

@K-C-DaCosta
Last active August 26, 2022 02:39
Show Gist options
  • Select an option

  • Save K-C-DaCosta/712a6fc38083d53f3bafe47c75b2b529 to your computer and use it in GitHub Desktop.

Select an option

Save K-C-DaCosta/712a6fc38083d53f3bafe47c75b2b529 to your computer and use it in GitHub Desktop.
cycle triangle solution
#[test]
fn triangle_test() {
cycle_triangle(10);
}
fn cycle_triangle(base: i32) {
// if base even we know final column is 2 so -> (base -2*k) = 2
// if base odd we know final column is 1 so -> (base -2*k) = 1
// solving for k we get 'steps' which is number of subtractions needed to to get to final
// column length
let steps = if base % 2 == 0 {
(base - 2) / 2
} else {
(base - 1) / 2
};
let h = base as usize;
let w = steps as usize + 1;
let mut row_list = (0..base).map(|_| String::new()).collect::<Vec<_>>();
let mut count = 0;
for x in 0..w{
for y in 0..h-2*x{
row_list[y+x].push( ((count%26) as u8 + b'A') as char );
count+=1;
}
}
for row in row_list{
println!("{}",row);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment