Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def reverseWords(self, s: str) -> str:

lst_words = s.split()
lst_words.reverse()
return ' '.join(lst_words)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def convert(self, s: str, numRows: int) -> str:
"""
Alternative implementation with detailed comments.
"""

# Handle edge cases
if numRows == 1 or numRows >= len(s):
return s

# Initialize rows
rows = [''] * numRows
current_row = 0
going_down = False

# Process each character
for char in s:
# Append character to current row
rows[current_row] += char

# At boundaries, reverse direction
# - At row 0 (top): start going down
# - At row numRows-1 (bottom): start going up
if current_row == 0 or current_row == numRows - 1:
going_down = not going_down

# Update current row based on direction
# going_down=True: increment row (1, 2, 3...)
# going_down=False: decrement row (...3, 2, 1)
current_row += 1 if going_down else -1

# Join all rows into final result
return ''.join(rows)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_21_reverse_words_in_string import Solution

class ReverseWordsInStringTestCase(unittest.TestCase):

def test_first_pattern(self):
solution = Solution()
output = solution.reverseWords(s = "the sky is blue")
target = "blue is sky the"
self.assertEqual(target, output)

def test_second_pattern(self):
solution = Solution()
output = solution.reverseWords(s = " hello world ")
target = "world hello"
self.assertEqual(target, output)

def test_third_pattern(self):
solution = Solution()
output = solution.reverseWords(s = "a good example")
target = "example good a"
self.assertEqual(target, output)
18 changes: 18 additions & 0 deletions tests/test_150_questions_round_22/test_22_zig_zag_round_22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_22_zig_zag import Solution

class ZigZagTestCase(unittest.TestCase):

def test_first_pattern(self):
solution = Solution()
output = solution.convert(s = "PAYPALISHIRING", numRows = 3)
target = "PAHNAPLSIIGYIR"
self.assertEqual(target, output)

def test_second_pattern(self):
solution = Solution()
output = solution.convert(s = "A", numRows = 1)
target = "A"
self.assertEqual(target, output)