Skip to content

Commit bb0d8e7

Browse files
committed
adding algo
1 parent 5203cf3 commit bb0d8e7

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def reverseWords(self, s: str) -> str:
6+
7+
lst_words = s.split()
8+
lst_words.reverse()
9+
return ' '.join(lst_words)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def convert(self, s: str, numRows: int) -> str:
6+
"""
7+
Alternative implementation with detailed comments.
8+
"""
9+
10+
# Handle edge cases
11+
if numRows == 1 or numRows >= len(s):
12+
return s
13+
14+
# Initialize rows
15+
rows = [''] * numRows
16+
current_row = 0
17+
going_down = False
18+
19+
# Process each character
20+
for char in s:
21+
# Append character to current row
22+
rows[current_row] += char
23+
24+
# At boundaries, reverse direction
25+
# - At row 0 (top): start going down
26+
# - At row numRows-1 (bottom): start going up
27+
if current_row == 0 or current_row == numRows - 1:
28+
going_down = not going_down
29+
30+
# Update current row based on direction
31+
# going_down=True: increment row (1, 2, 3...)
32+
# going_down=False: decrement row (...3, 2, 1)
33+
current_row += 1 if going_down else -1
34+
35+
# Join all rows into final result
36+
return ''.join(rows)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_21_reverse_words_in_string import Solution
4+
5+
class ReverseWordsInStringTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.reverseWords(s = "the sky is blue")
10+
target = "blue is sky the"
11+
self.assertEqual(target, output)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.reverseWords(s = " hello world ")
16+
target = "world hello"
17+
self.assertEqual(target, output)
18+
19+
def test_third_pattern(self):
20+
solution = Solution()
21+
output = solution.reverseWords(s = "a good example")
22+
target = "example good a"
23+
self.assertEqual(target, output)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_22_zig_zag import Solution
4+
5+
class ZigZagTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.convert(s = "PAYPALISHIRING", numRows = 3)
10+
target = "PAHNAPLSIIGYIR"
11+
self.assertEqual(target, output)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.convert(s = "A", numRows = 1)
16+
target = "A"
17+
self.assertEqual(target, output)
18+

0 commit comments

Comments
 (0)