From 1f0ddff8dc749fc31d402ae03f63c2486fb022af Mon Sep 17 00:00:00 2001 From: LL-creator <72474486+LL-creator@users.noreply.github.com> Date: Fri, 16 Oct 2020 20:26:01 -0700 Subject: [PATCH 1/2] Create 0006_Zig_Zag_Conversion.py This is the Zig Zag conversion algorithm from Leetcode. It converts a string into a ZigZag block of text. --- LeetCode/0006_Zig_Zag_Conversion.py | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 LeetCode/0006_Zig_Zag_Conversion.py diff --git a/LeetCode/0006_Zig_Zag_Conversion.py b/LeetCode/0006_Zig_Zag_Conversion.py new file mode 100644 index 0000000..682e7bc --- /dev/null +++ b/LeetCode/0006_Zig_Zag_Conversion.py @@ -0,0 +1,37 @@ +class Solution: + def __init__(self, string, rows): + self.rows = rows + self.string = string + self.list = [] + + def convert(self) -> list: + l_index = len(self.string) - 1 + offset = 2*self.rows - 2 + self.list = [] + for i in range(self.rows): + self.list.append(" ") + for i in range(self.rows): + for j in range(i): + self.list[i] += " " + + for x in range(0, l_index + 1, offset): + for j in range(len(self.list)): + + if (x + j <= l_index and j != self.rows - 1): + self.list[j] += self.string[x + j] + for d in range(1, offset - 2 * j, 1): + self.list[j] += " " + + if (x + offset - j <= l_index): + if ((j != 0)): + self.list[j] += self.string[x + (offset - j)] + for d in range(1, 2 * j, 1): + self.list[j] += " " + for i in range(self.rows): + for j in range(i): + self.list[i] += " " + + def show(self): + for i in self.list: + print(i) + From 338d673167231b36264d2b31a5a5a214d7123e9d Mon Sep 17 00:00:00 2001 From: LL-creator <72474486+LL-creator@users.noreply.github.com> Date: Sun, 18 Oct 2020 09:26:41 -0700 Subject: [PATCH 2/2] Update 0006_Zig_Zag_Conversion.py --- LeetCode/0006_Zig_Zag_Conversion.py | 68 ++++++++++++++++------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/LeetCode/0006_Zig_Zag_Conversion.py b/LeetCode/0006_Zig_Zag_Conversion.py index 682e7bc..a86d43b 100644 --- a/LeetCode/0006_Zig_Zag_Conversion.py +++ b/LeetCode/0006_Zig_Zag_Conversion.py @@ -1,37 +1,43 @@ class Solution: - def __init__(self, string, rows): - self.rows = rows - self.string = string - self.list = [] + def __init__(self, string, rows): + self.rows = rows + self.string = string + self.list = [] - def convert(self) -> list: - l_index = len(self.string) - 1 - offset = 2*self.rows - 2 - self.list = [] - for i in range(self.rows): - self.list.append(" ") - for i in range(self.rows): - for j in range(i): - self.list[i] += " " + def convert(self, W = False) -> list: + """ + the w is for if you want your zig zag to look like + this: \ /\ / or this: | /| / + \ / \ / | / | / + \/ \/ |/ |/ + """ + l_index = len(self.string) - 1 + offset = 2 * self.rows - 2 + self.list = [] + for i in range(self.rows): + self.list.append(" ") + if(W != False): + for i in range(self.rows): + for j in range(i): + self.list[i] += " " - for x in range(0, l_index + 1, offset): - for j in range(len(self.list)): + for x in range(0, l_index + 1, offset): + for j in range(len(self.list)): - if (x + j <= l_index and j != self.rows - 1): - self.list[j] += self.string[x + j] - for d in range(1, offset - 2 * j, 1): - self.list[j] += " " + if (x + j <= l_index and j != self.rows - 1): + self.list[j] += self.string[x + j] + for d in range(1, offset - 2 * j, 1): + self.list[j] += " " - if (x + offset - j <= l_index): - if ((j != 0)): - self.list[j] += self.string[x + (offset - j)] - for d in range(1, 2 * j, 1): - self.list[j] += " " - for i in range(self.rows): - for j in range(i): - self.list[i] += " " - - def show(self): - for i in self.list: - print(i) + if (x + offset - j <= l_index): + if ((j != 0)): + self.list[j] += self.string[x + (offset - j)] + for d in range(1, 2 * j, 1): + self.list[j] += " " + for i in range(self.rows): + for j in range(i): + self.list[i] += " " + def show(self): + for i in self.list: + print(i)