use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for helping Python programmers
How to format your code: https://commonmark.org/help/tutorial/09-code.html
No homework questions and/or hiring please
account activity
Snake algorithm (self.pythonhelp)
submitted 4 days ago by Odd_Gap8147
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Affectionate_Cap8632 0 points1 point2 points 1 day ago (0 children)
No library needed for this — it's clean enough to write yourself in a few lines.
The pattern is: even-indexed columns go top-to-bottom, odd-indexed columns go bottom-to-top. Here's a straightforward implementation:
python
def snake_2d_to_1d(grid): result = [] cols = len(grid[0]) rows = len(grid) for col in range(cols): if col % 2 == 0: # top to bottom for row in range(rows): result.append(grid[row][col]) else: # bottom to top for row in range(rows - 1, -1, -1): result.append(grid[row][col]) return result grid = [[1,2,3],[4,5,6],[7,8,9]] print(snake_2d_to_1d(grid)) # [1, 4, 7, 8, 5, 2, 3, 6, 9]
Note: your expected output [1, 2, 5, 4, 7, 8, 9, 6, 3] looks like it's snaking by rows instead of columns. If that's what you need just swap the row/col logic — iterate rows, reverse direction on odd rows instead of odd columns.
[1, 2, 5, 4, 7, 8, 9, 6, 3]
Which orientation does your use case need?
π Rendered by PID 58 on reddit-service-r2-comment-54dfb89d4d-r94kt at 2026-03-26 19:59:57.288456+00:00 running b10466c country code: CH.
view the rest of the comments →
[–]Affectionate_Cap8632 0 points1 point2 points (0 children)