I used a helper function approach after realizing that the "Grid" needs to be updated in the same manner over and over again
I have received feedback that storing a grid may take too much memory
Would a "mathematical" approach actually be more readable? And would it be something that can be though of within 20ish minutes (interview time)
I have many if statements that are used to exit out of loops to avoid array index errors
Any feedback or criticism is appreciated - nitpick on anything that seems off
- glaring issue is that this doesn't handle edge cases well. For example: "A 2"
https://leetcode.com/problems/zigzag-conversion/
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
row = [''] * (len(s) // 2)
self.s = s
self.numRows = numRows
self.final_grid = []
for i in range(numRows):
self.final_grid.append(list(row))
self.addElements(0, 0)
return "".join(map("".join, self.final_grid))
def addElements(self, count, column):
print(column, "beg")
for i in range(self.numRows):
if count > len(self.s) - 1:
break
self.final_grid[i][column] = self.s[count]
count += 1
for i in range(1, self.numRows - 1):
if count > len(self.s) - 1:
break
self.final_grid[self.numRows - i - 1][column + i] = self.s[count]
count += 1
print(column, "end")
print(column + self.numRows - 1)
if count < len(self.s) - 1:
self.addElements(count, (column + self.numRows - 1))