mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add print-pyramid/rust/function
This commit is contained in:
parent
8e97e9f65e
commit
3b6cc97bb5
7
challenges/print-pyramid/solutions/rust/function/Cargo.lock
generated
Normal file
7
challenges/print-pyramid/solutions/rust/function/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "rust_application"
|
||||
version = "1.0.0"
|
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust_application"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,3 @@
|
||||
# print-pyramid/rust/function
|
||||
|
||||
Created by [@theoludwig](https://github.com/theoludwig) on 2 August 2023.
|
36
challenges/print-pyramid/solutions/rust/function/src/main.rs
Normal file
36
challenges/print-pyramid/solutions/rust/function/src/main.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
let mut pyramid_type = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut pyramid_type)
|
||||
.expect("Failed to read `stdin` line.");
|
||||
pyramid_type = pyramid_type.trim().to_string();
|
||||
let mut height = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut height)
|
||||
.expect("Failed to read `stdin` line.");
|
||||
let height: usize = height
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Failed to convert `height` as an `usize`.");
|
||||
|
||||
let mut step = if pyramid_type == "normal" { 1 } else { height };
|
||||
while (pyramid_type == "normal" && step <= height) || (pyramid_type == "reverse" && step != 0) {
|
||||
let number_of_stars = (step * 2) - 1;
|
||||
let total_number_of_locations = (height * 2) - 1;
|
||||
let total_number_of_spaces = total_number_of_locations - number_of_stars;
|
||||
let number_of_spaces_on_each_side = total_number_of_spaces / 2;
|
||||
println!(
|
||||
"{}{}{}",
|
||||
" ".repeat(number_of_spaces_on_each_side),
|
||||
"*".repeat(number_of_stars),
|
||||
" ".repeat(number_of_spaces_on_each_side)
|
||||
);
|
||||
if pyramid_type == "normal" {
|
||||
step = step + 1;
|
||||
} else {
|
||||
step = step - 1;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user