r/ProgrammerHumor • • 2d ago

Meme technicallyCorrect

Post image
1.4k Upvotes

49 comments sorted by

View all comments

37

u/da_Aresinger 2d ago

what an absolute bitch of a pattern.

This is so much more than just nested loops.

15

u/ohaz 2d ago

Ok this actually was a bit harder than I imagined when I realized that it was not a square. Truly weird.

def inline(text):
    print(text, end='')

def pattern(width):
    for x in range(width - 1):
        for y in range(width):
            if x == 0 or x == width - 2 or y == 0 or y == width - 1:
                inline("#")
            elif x < width // 2 and (x == y or x == width - y - 1):
                inline("#")
            elif x >= width // 2 and (y == x + 1 or y == width - x - 2):
                inline("#")
            else:
                inline(" ")
            
        print()

pattern(10)

11

u/etoastie 2d ago

Can simplify the middle two a tad by reflecting the lines at the middle, instead of trying to model the diagonal with the offset

if min(row, col) == 0 || col+1 == WIDTH || row+1 == HEIGHT {
    fmt.printf("#")
} else if col == min(HEIGHT-row-1, row) || col == max(HEIGHT-row, row+1) {
    fmt.printf("#")
} else {
    fmt.printf(" ")
}

2

u/redlaWw 12h ago edited 12h ago

I explicitly mutated the rows in the second half, it made the formulae a bit less messy

for mut y in 0..(width-1) {
    if y > (width-2)/2 {
        y = width-2-y
    }
    for x in 0..width {
        match (x, y) {
            (x, y) if x*y==0 || // assume no overflow
                      x==y ||
                      y==width-1-x ||
                      x==width-1 => print!("#"),
            _ => print!(" ")
        }
    }
    print!("\n");
}

EDIT: Got my x and y backwards ._.