Minichan

Topic: Random challenge

Anonymous A started this discussion 5 months ago #128,230

Can somebody write code that takes an n by n matrix starting at the top left corner and numbers the positions in the matrix starting at 0 incrementing by one each time in counter-clockwise spiral order?

boof joined in and replied with this 5 months ago, 1 minute later[^] [v] #1,382,729

Jeeprs

Anonymous C joined in and replied with this 5 months ago, 4 minutes later, 6 minutes after the original post[^] [v] #1,382,732

@OP

> Can somebody write code that takes an n by n matrix starting at the top left corner and numbers the positions in the matrix starting at 0 incrementing by one each time in counter-clockwise spiral order?

Yes.

Anonymous A (OP) replied with this 5 months ago, 16 seconds later, 6 minutes after the original post[^] [v] #1,382,733

@previous (C)
Are you that somebody?

Anonymous A (OP) double-posted this 5 months ago, 18 seconds later, 6 minutes after the original post[^] [v] #1,382,734

I saw this in a leetcode question.

Anonymous C replied with this 5 months ago, 19 minutes later, 26 minutes after the original post[^] [v] #1,382,739

@1,382,733 (A)

> Are you that somebody?

Yes.

Anonymous A (OP) replied with this 5 months ago, 10 minutes later, 36 minutes after the original post[^] [v] #1,382,740

@previous (C)

Ok

Anonymous D joined in and replied with this 5 months ago, 9 minutes later, 45 minutes after the original post[^] [v] #1,382,741

Easy

boof replied with this 5 months ago, 17 minutes later, 1 hour after the original post[^] [v] #1,382,746

I notice that for n there are 2n - 1 direction turns

I notice that the number of numbers to place before turning is n, n - 1, n - 1, n - 2, n - 2, n - 3, n - 3, and so on continuing until n - x is 1 and no further.

Oatmeal Fucker !BYUc1TwJMU joined in and replied with this 5 months ago, 7 hours later, 8 hours after the original post[^] [v] #1,382,790

Just vibecode it, it's the modern age

def counter_clockwise_spiral(n):
    # Create n x n matrix filled with None
    matrix = [[None] * n for _ in range(n)]
    
    # Boundaries
    top, bottom = 0, n - 1
    left, right = 0, n - 1
    
    num = 0  # Start number
    
    while left <= right and top <= bottom:
        # Top to bottom along left column
        for i in range(top, bottom + 1):
            matrix[i][left] = num
            num += 1
        left += 1
        
        # Left to right along bottom row
        for j in range(left, right + 1):
            matrix[bottom][j] = num
            num += 1
        bottom -= 1
        
        # Bottom to top along right column
        if left <= right:
            for i in range(bottom, top - 1, -1):
                matrix[i][right] = num
                num += 1
            right -= 1
        
        # Right to left along top row
        if top <= bottom:
            for j in range(right, left - 1, -1):
                matrix[top][j] = num
                num += 1
            top += 1
    
    return matrix


# Example usage:
n = 5
spiral = counter_clockwise_spiral(n)
for row in spiral:
    print(" ".join(f"{x:2}" for x in row))
[/i][/i]

Oatmeal Fucker !BYUc1TwJMU double-posted this 5 months ago, 3 minutes later, 8 hours after the original post[^] [v] #1,382,791

Spoiler alert it doesn't work

tteh !MemesToDNA joined in and replied with this 5 months ago, 55 minutes later, 9 hours after the original post[^] [v] #1,382,797

@previous (Oatmeal Fucker !BYUc1TwJMU)
Seems to work. What's wrong with it?

I mean Minichan's formatting fucked the code up by trying to close [i] there at the end, lol, but the code (minus those) does run.

(Edited 3 minutes later.)

Oatmeal Fucker !BYUc1TwJMU replied with this 5 months ago, 49 minutes later, 10 hours after the original post[^] [v] #1,382,798

@previous (tteh !MemesToDNA)

I thought it was running clockwise but actually big brain here just forgot how clocks work

Anonymous G joined in and replied with this 5 months ago, 4 days later, 4 days after the original post[^] [v] #1,384,180

Watch PREDATOR and ALIEN
:

Please familiarise yourself with the rules and markup syntax before posting.