From 3b6cc97bb52b56cbc1e15891767f0e8423591bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Wed, 2 Aug 2023 17:48:31 +0200 Subject: [PATCH] feat(solutions): add `print-pyramid/rust/function` --- .../solutions/rust/function/Cargo.lock | 7 ++++ .../solutions/rust/function/Cargo.toml | 6 ++++ .../solutions/rust/function/README.md | 3 ++ .../solutions/rust/function/src/main.rs | 36 +++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 challenges/print-pyramid/solutions/rust/function/Cargo.lock create mode 100644 challenges/print-pyramid/solutions/rust/function/Cargo.toml create mode 100644 challenges/print-pyramid/solutions/rust/function/README.md create mode 100644 challenges/print-pyramid/solutions/rust/function/src/main.rs diff --git a/challenges/print-pyramid/solutions/rust/function/Cargo.lock b/challenges/print-pyramid/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/print-pyramid/solutions/rust/function/Cargo.lock @@ -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" diff --git a/challenges/print-pyramid/solutions/rust/function/Cargo.toml b/challenges/print-pyramid/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/print-pyramid/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/print-pyramid/solutions/rust/function/README.md b/challenges/print-pyramid/solutions/rust/function/README.md new file mode 100644 index 0000000..738e62e --- /dev/null +++ b/challenges/print-pyramid/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# print-pyramid/rust/function + +Created by [@theoludwig](https://github.com/theoludwig) on 2 August 2023. diff --git a/challenges/print-pyramid/solutions/rust/function/src/main.rs b/challenges/print-pyramid/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..b1359a3 --- /dev/null +++ b/challenges/print-pyramid/solutions/rust/function/src/main.rs @@ -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; + } + } +}