From bb0d8e7e036469971f34e553e220fc807ea7a7a0 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 9 Dec 2025 04:10:42 -0600 Subject: [PATCH] adding algo --- .../ex_21_reverse_words_in_string.py | 9 +++++ .../ex_22_zig_zag.py | 36 +++++++++++++++++++ ...est_21_reverse_words_in_string_round_22.py | 23 ++++++++++++ .../test_22_zig_zag_round_22.py | 18 ++++++++++ 4 files changed, 86 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_21_reverse_words_in_string.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_22_zig_zag.py create mode 100644 tests/test_150_questions_round_22/test_21_reverse_words_in_string_round_22.py create mode 100644 tests/test_150_questions_round_22/test_22_zig_zag_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_21_reverse_words_in_string.py b/src/my_project/interviews/top_150_questions_round_22/ex_21_reverse_words_in_string.py new file mode 100644 index 00000000..ce06e032 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_21_reverse_words_in_string.py @@ -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) \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_22_zig_zag.py b/src/my_project/interviews/top_150_questions_round_22/ex_22_zig_zag.py new file mode 100644 index 00000000..f7b7da2f --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_22_zig_zag.py @@ -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) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_21_reverse_words_in_string_round_22.py b/tests/test_150_questions_round_22/test_21_reverse_words_in_string_round_22.py new file mode 100644 index 00000000..4b157753 --- /dev/null +++ b/tests/test_150_questions_round_22/test_21_reverse_words_in_string_round_22.py @@ -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) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_22_zig_zag_round_22.py b/tests/test_150_questions_round_22/test_22_zig_zag_round_22.py new file mode 100644 index 00000000..5229ed93 --- /dev/null +++ b/tests/test_150_questions_round_22/test_22_zig_zag_round_22.py @@ -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) +