1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00

feat(solutions): add print-pyramid/rust/function

This commit is contained in:
Théo LUDWIG 2023-08-02 17:48:31 +02:00
parent 8e97e9f65e
commit 3b6cc97bb5
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
4 changed files with 52 additions and 0 deletions

View 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"

View File

@ -0,0 +1,6 @@
[package]
name = "rust_application"
version = "1.0.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,3 @@
# print-pyramid/rust/function
Created by [@theoludwig](https://github.com/theoludwig) on 2 August 2023.

View 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;
}
}
}