Skip to content

Commit cbe8774

Browse files
committed
Adding challenge 6.
1 parent 08ebf4a commit cbe8774

File tree

8 files changed

+161
-0
lines changed

8 files changed

+161
-0
lines changed

p_6_zigzag_conversion/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# p_6_zigzag_conversion
2+
3+
🔗 https://leetcode.com/problems/zigzag-conversion/description/?source=submission-noac
4+
5+
## Leetcode - 6 - Zigzag conversion
6+
7+
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
8+
9+
![alt text](image.png)
10+
11+
![alt text](image-1.png)
12+
13+
And then read line by line: "PAHNAPLSIIGYIR"
14+
15+
Write the code that will take a string and make this conversion given a number of rows:
16+
17+
string convert(string s, int numRows);
18+
19+
20+
Example 1:
21+
22+
Input: s = "PAYPALISHIRING", numRows = 3
23+
Output: "PAHNAPLSIIGYIR"
24+
25+
Example 2:
26+
27+
Input: s = "PAYPALISHIRING", numRows = 4
28+
Output: "PINALSIGYAHRPI"
29+
Explanation:
30+
31+
![alt text](image-2.png)
32+
33+
Example 3:
34+
35+
Input: s = "A", numRows = 1
36+
Output: "A"
37+
38+
39+
Constraints:
40+
41+
1 <= s.length <= 1000
42+
s consists of English letters (lower-case and upper-case), ',' and '.'.
43+
1 <= numRows <= 1000

p_6_zigzag_conversion/image-1.png

2.48 KB
Loading

p_6_zigzag_conversion/image-2.png

1.11 KB
Loading

p_6_zigzag_conversion/image.png

1.04 KB
Loading

p_6_zigzag_conversion/src/__init__.py

Whitespace-only changes.

p_6_zigzag_conversion/src/main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# https://github.com/badprog/badprog_leetcode_python
2+
3+
################
4+
# import
5+
import sys
6+
import pathlib
7+
8+
# Adding the parent directory of this file to the sys.path
9+
sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
10+
11+
from src.solution import Solution
12+
from typing import List
13+
14+
################
15+
#
16+
def main():
17+
print("Hello from Badprog, Leetcode challenge 6 and Python :D")
18+
solution = Solution()
19+
# s = "PAYPALISHIRING"
20+
s = "AB"
21+
num_rows = 1
22+
result = solution.convert(s, num_rows)
23+
print("result = {}", result)
24+
25+
################
26+
#
27+
if __name__ == "__main__":
28+
main()
29+

p_6_zigzag_conversion/src/solution.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# https://github.com/badprog/badprog_leetcode_python
2+
3+
from typing import List
4+
5+
############################
6+
#
7+
class Solution:
8+
############################
9+
#
10+
def convert(self, s: str, numRows: int) -> str:
11+
#
12+
if numRows == 1:
13+
return s
14+
15+
#
16+
vec_row: List[str] = [""] * numRows
17+
step = 0
18+
cur_row = 0
19+
20+
for index, element in enumerate(s):
21+
# print(f"index: {index}, element: {element}")
22+
vec_row[cur_row] += element
23+
24+
if cur_row == 0:
25+
step = 1
26+
elif cur_row == numRows - 1:
27+
step = -1
28+
29+
cur_row += step
30+
31+
#
32+
# print(f"vec_row: {vec_row}")
33+
final_str = ''.join(vec_row)
34+
35+
#
36+
return final_str
37+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# https://github.com/badprog/badprog_leetcode_python
2+
3+
############################
4+
# import
5+
import sys
6+
import pathlib
7+
8+
# Adding the parent directory of this file to the sys.path
9+
sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
10+
11+
import pytest
12+
from src.solution import Solution
13+
14+
def test_standard_case_numRows_3():
15+
solution = Solution()
16+
assert solution.convert("PAYPALISHIRING", 3) == "PAHNAPLSIIGYIR"
17+
18+
def test_standard_case_numRows_4():
19+
solution = Solution()
20+
assert solution.convert("PAYPALISHIRING", 4) == "PINALSIGYAHRPI"
21+
22+
def test_single_row():
23+
solution = Solution()
24+
assert solution.convert("A", 1) == "A"
25+
26+
def test_short_string_numRows_2():
27+
solution = Solution()
28+
assert solution.convert("AB", 2) == "AB"
29+
30+
def test_string_numRows_2():
31+
solution = Solution()
32+
assert solution.convert("ABCDE", 2) == "ACEBD"
33+
34+
def test_numRows_greater_than_string_length():
35+
solution = Solution()
36+
assert solution.convert("ABC", 5) == "ABC"
37+
38+
def test_empty_string():
39+
solution = Solution()
40+
assert solution.convert("", 3) == ""
41+
42+
def test_zigzag_numRows_3():
43+
solution = Solution()
44+
assert solution.convert("ABCDEFGHI", 3) == "AEIBDFHCG"
45+
46+
def test_zigzag_numRows_5():
47+
solution = Solution()
48+
assert solution.convert("ABCDEFGHIJK", 5) == "AIBHJCGKDFE" # Fixed expected output
49+
50+
def test_special_characters():
51+
solution = Solution()
52+
assert solution.convert("Hello,World!", 3) == "Horel,ol!lWd" # Fixed expected output

0 commit comments

Comments
 (0)