Skip to content

Instantly share code, notes, and snippets.

@ilopX
Created August 5, 2023 13:59
Show Gist options
  • Save ilopX/7e7a8126f69f1a0d06a1cf011260d130 to your computer and use it in GitHub Desktop.
Save ilopX/7e7a8126f69f1a0d06a1cf011260d130 to your computer and use it in GitHub Desktop.
pir
#![allow(dead_code)]
use std::iter::repeat;
fn main() {}
fn pir(n: u32) -> String {
let mut x = 1;
(0..n)
.map(|i| next_line(n, i, &mut x))
.collect::<Vec<String>>()
.join("\n")
}
#[inline]
fn next_line(n: u32, i: u32, x: &mut usize) -> String {
line(next_space(n, i), next_x(x))
}
#[inline]
fn next_space(n: u32, i: u32) -> usize {
(n - i - 1) as usize
}
fn next_x(x: &mut usize) -> usize {
let ol_x = *x;
*x += 2;
ol_x
}
#[inline]
fn line(space: usize, x: usize) -> String {
let space_l = repeat(' ').take(space);
let x = repeat('x').take(x);
let space_r = repeat(' ').take(space);
space_l.chain(x)
.chain(space_r)
.collect::<String>()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn text1() {
assert_eq!(
pir(3), [
" x ", //2, 1
" xxx ", //1, 3,
"xxxxx" // 0, 5
].join("\n")
)
}
#[test]
fn text2() {
assert_eq!(
pir(2), [
" x ",
"xxx",
].join("\n")
)
}
#[test]
fn text3() {
assert_eq!(
pir(4), [
" x ",
" xxx ",
" xxxxx ",
"xxxxxxx",
].join("\n")
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment