From a5189619a9da5cdb16a2f3b2f3a230ea9eb77614 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 4 May 2026 04:52:29 -0600 Subject: [PATCH] adding algo --- .../ex_06_rotate_array.py | 15 +++++++++++++++ .../ex_06_rotate_array.ts | 8 ++++++++ .../test_06_rotate_array_round_23.py | 11 +++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_23/ex_06_rotate_array.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_23/ex_06_rotate_array.ts create mode 100644 tests/test_150_questions_round_23/test_06_rotate_array_round_23.py diff --git a/src/my_project/interviews/top_150_questions_round_23/ex_06_rotate_array.py b/src/my_project/interviews/top_150_questions_round_23/ex_06_rotate_array.py new file mode 100644 index 00000000..bc78d905 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_23/ex_06_rotate_array.py @@ -0,0 +1,15 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def rotate(self, nums: List[int], k: int) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + + len_nums = len(nums) + k = k % len_nums + + nums[:] = nums[len_nums-k:] + nums[:len_nums-k] + + return nums diff --git a/src/my_project/interviews_typescript/top_150_questions_round_23/ex_06_rotate_array.ts b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_06_rotate_array.ts new file mode 100644 index 00000000..ea21055c --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_06_rotate_array.ts @@ -0,0 +1,8 @@ +function rotate(nums: number[], k: number): void { + const lenNums = nums.length; + k = k % lenNums; + const rotated = [...nums.slice(lenNums - k), ...nums.slice(0, lenNums - k)]; + for (let i = 0; i < lenNums; i++) { + nums[i] = rotated[i]; + } +}; diff --git a/tests/test_150_questions_round_23/test_06_rotate_array_round_23.py b/tests/test_150_questions_round_23/test_06_rotate_array_round_23.py new file mode 100644 index 00000000..fc48a810 --- /dev/null +++ b/tests/test_150_questions_round_23/test_06_rotate_array_round_23.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_23\ +.ex_06_rotate_array import Solution + +class RotateArrayTestCase(unittest.TestCase): + + def test_rotate_array_i(self): + solution = Solution() + output = solution.rotate(nums = [1,2,3,4,5,6,7], k = 3) + target = [5,6,7,1,2,3,4] + self.assertEqual(output, target) \ No newline at end of file