From 43e871dda929bc5617f34cd27dd4adf56149625a Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 9 Jun 2025 04:28:48 -0600 Subject: [PATCH] adding majority element --- .../round_1/steps_to_squal_strings.py | 11 ++++++++++ .../majority_element.py | 20 +++++++++++++++++++ .../test_majority_element_round_17.py | 17 ++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/my_project/interviews/interview_amazon/round_1/steps_to_squal_strings.py create mode 100644 src/my_project/interviews/top_150_questions_round_17/majority_element.py create mode 100644 tests/test_150_questions_round_17/test_majority_element_round_17.py diff --git a/src/my_project/interviews/interview_amazon/round_1/steps_to_squal_strings.py b/src/my_project/interviews/interview_amazon/round_1/steps_to_squal_strings.py new file mode 100644 index 00000000..06e38743 --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_1/steps_to_squal_strings.py @@ -0,0 +1,11 @@ +def getRemovableIndices(str1, str2): + if len(str1) != len(str2) + 1: + return [-1] + else: + indices = [] + + for i in range(len(str1)): + if str1[:i] + str1[i+1:] == str2: + indices.append(i) + + return indices if indices else [-1] diff --git a/src/my_project/interviews/top_150_questions_round_17/majority_element.py b/src/my_project/interviews/top_150_questions_round_17/majority_element.py new file mode 100644 index 00000000..63cc12d5 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_17/majority_element.py @@ -0,0 +1,20 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def majorityElement(self, nums: List[int]) -> int: + + dic_answer = dict() + len_nums = len(nums) + + for i in range(len_nums): + + if nums[i] not in dic_answer: + dic_answer[nums[i]] = 1 + else: + dic_answer[nums[i]] += 1 + + if dic_answer[nums[i]] > len_nums//2: + return nums[i] + + return -1 \ No newline at end of file diff --git a/tests/test_150_questions_round_17/test_majority_element_round_17.py b/tests/test_150_questions_round_17/test_majority_element_round_17.py new file mode 100644 index 00000000..8943534b --- /dev/null +++ b/tests/test_150_questions_round_17/test_majority_element_round_17.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_17\ +.majority_element import Solution + +class MajorityElementTestCase(unittest.TestCase): + + def test_is_major_element(self): + solution = Solution() + output = solution.majorityElement(nums=[3,2,3]) + target = 3 + self.assertEqual(output, target) + + def test_is_no_major_element(self): + solution = Solution() + output = solution.majorityElement(nums = [1,2,3]) + target = -1 + self.assertEqual(output, target) \ No newline at end of file