From 1a7f3d1017ace1c213dc4451a8d4e7a606f50349 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 11 Jan 2026 04:18:32 -0600 Subject: [PATCH] adding algo --- .../ex_42_group_anagrams.py | 13 +++++++++++ .../ex_43_two_sum.py | 16 +++++++++++++ .../ex_44_happy_number.py | 18 +++++++++++++++ .../test_42_group_anagrams_round_22.py | 23 +++++++++++++++++++ .../test_43_two_sum_round_22.py | 18 +++++++++++++++ .../test_44_happy_number_round_22.py | 15 ++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_42_group_anagrams.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_43_two_sum.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_44_happy_number.py create mode 100644 tests/test_150_questions_round_22/test_42_group_anagrams_round_22.py create mode 100644 tests/test_150_questions_round_22/test_43_two_sum_round_22.py create mode 100644 tests/test_150_questions_round_22/test_44_happy_number_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_42_group_anagrams.py b/src/my_project/interviews/top_150_questions_round_22/ex_42_group_anagrams.py new file mode 100644 index 00000000..e5010d1e --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_42_group_anagrams.py @@ -0,0 +1,13 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import defaultdict + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + + answer = defaultdict(list) + + for word in strs: + answer[''.join(sorted(word))].append(word) + + return list(answer.values()) diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_43_two_sum.py b/src/my_project/interviews/top_150_questions_round_22/ex_43_two_sum.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_43_two_sum.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_44_happy_number.py b/src/my_project/interviews/top_150_questions_round_22/ex_44_happy_number.py new file mode 100644 index 00000000..67350d52 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_44_happy_number.py @@ -0,0 +1,18 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution(object): + def isHappy(self, n): + + set_answer = set() + + while n != 1: + + if n in set_answer: + return False + else: + set_answer.add(n) + + n = sum([int(c)**2 for c in str(n)]) + + return True \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_42_group_anagrams_round_22.py b/tests/test_150_questions_round_22/test_42_group_anagrams_round_22.py new file mode 100644 index 00000000..5975987c --- /dev/null +++ b/tests/test_150_questions_round_22/test_42_group_anagrams_round_22.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_42_group_anagrams import Solution + +class GroupAnagramsTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.groupAnagrams(strs = ["eat","tea","tan","ate","nat","bat"]) + target = [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']] + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.groupAnagrams(strs = [""]) + target = [[""]] + self.assertEqual(output, target) + + def test_third_pattern(self): + solution = Solution() + output = solution.groupAnagrams(strs = ["a"]) + target = [["a"]] + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_43_two_sum_round_22.py b/tests/test_150_questions_round_22/test_43_two_sum_round_22.py new file mode 100644 index 00000000..a12354ba --- /dev/null +++ b/tests/test_150_questions_round_22/test_43_two_sum_round_22.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_43_two_sum import Solution + +class TwoSumTestCase(unittest.TestCase): + + def test_is_two_sum(self): + solution = Solution() + output = solution.twoSum(nums=[2,7,11,15], target=9) + target = [0,1] + for k, v in enumerate(target): + self.assertEqual(v, output[k]) + + def test_is_no_two_sum(self): + solution = Solution() + output = solution.twoSum(nums=[2,7,11,15], target=0) + target = [] + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_44_happy_number_round_22.py b/tests/test_150_questions_round_22/test_44_happy_number_round_22.py new file mode 100644 index 00000000..71a68014 --- /dev/null +++ b/tests/test_150_questions_round_22/test_44_happy_number_round_22.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_44_happy_number import Solution + +class HappyNumberTestCase(unittest.TestCase): + + def test_is_happy_number(self): + solution = Solution() + output = solution.isHappy(n=19) + self.assertTrue(output) + + def test_is_no_happy_number(self): + solution = Solution() + output = solution.isHappy(n=2) + self.assertFalse(output) \ No newline at end of file