Algorithm for x-mass

When I started learning Rust (2021.11.03), the first thing I encountered was printing a square and a triangle. I was confused at first. Then, when I understood how to write simple algorithms, I decided to write an algorithm for printing a Christmas tre…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Antonov Mike

When I started learning Rust (2021.11.03), the first thing I encountered was printing a square and a triangle. I was confused at first. Then, when I understood how to write simple algorithms, I decided to write an algorithm for printing a Christmas tree. And in January 2022 I got this:
simple
This is the algorithm

use std::thread;
use std::time::Duration;

fn main() { tannenbaum(7) }

fn tannenbaum(x: i32) {
    let mut top = 0;
    let mut i = 1;

    while top < x/2 {
        for _empty_space in 0..2     { print!(" ") }
        top += 1
    }

    if x%2 == 1                      { print!(" ") }

    println!("A");

    while i <= x {
        for _empty_space in 0..(x-i) { print!(" ") }
        for _left_side   in 0..i     { print!("L") }
        for _middle      in i..i+1   { print!("M") }
        for _right_side  in 0..i     { print!("R") }

        println!();

        thread::sleep(Duration::from_millis(120));
        i += 1
    }

    for _stem in 0..(x-1)            { print!(" ") }

    println!("ШШШ");
}

Try to colorize it using

[dependencies]
colored = "2"

colorized

Happy Holidays


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Antonov Mike


Print Share Comment Cite Upload Translate Updates
APA

Antonov Mike | Sciencx (2022-12-26T04:40:23+00:00) Algorithm for x-mass. Retrieved from https://www.scien.cx/2022/12/26/algorithm-for-x-mass/

MLA
" » Algorithm for x-mass." Antonov Mike | Sciencx - Monday December 26, 2022, https://www.scien.cx/2022/12/26/algorithm-for-x-mass/
HARVARD
Antonov Mike | Sciencx Monday December 26, 2022 » Algorithm for x-mass., viewed ,<https://www.scien.cx/2022/12/26/algorithm-for-x-mass/>
VANCOUVER
Antonov Mike | Sciencx - » Algorithm for x-mass. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/26/algorithm-for-x-mass/
CHICAGO
" » Algorithm for x-mass." Antonov Mike | Sciencx - Accessed . https://www.scien.cx/2022/12/26/algorithm-for-x-mass/
IEEE
" » Algorithm for x-mass." Antonov Mike | Sciencx [Online]. Available: https://www.scien.cx/2022/12/26/algorithm-for-x-mass/. [Accessed: ]
rf:citation
» Algorithm for x-mass | Antonov Mike | Sciencx | https://www.scien.cx/2022/12/26/algorithm-for-x-mass/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.