Skip to content

Commit

Permalink
Python part-2 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pandiarajan-src committed Jun 10, 2024
1 parent 3e4f280 commit 9cff854
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 0 deletions.
51 changes: 51 additions & 0 deletions ik5_maximum_sub_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Maximum Sum Subarray
Given a list of integers, find the sum of a non-empty subarray that has the largest sum.
Example One
input: [2, -6, 3, 4, -5]
Output: 7
Example Two
input: [-7, -9, -3, -5]
Output:-3
Notes
A subarray is an array composed of a contiguous block of the original array's elements.
Constraints:
1 <= length of the given list <= 105
-104 <= elements of list <= 104
"""
import unittest

def maximum_subarray(nums: list[int]) -> int:
"""
Time Complexity: O(n)
Space Complexity: O(1)
"""
max_sum = nums[0]
current_sum = 0
for num in nums:
if current_sum < 0:
current_sum = 0
current_sum += num
max_sum = max(max_sum, current_sum)
return max_sum

class TestMaximumSubArray(unittest.TestCase):
"""
Test case for the `maximum subarray` problem.
"""

def test_maximum_subarray(self):
"""
Test case for the `maximum subarray` problem.
"""
self.assertEqual(maximum_subarray([2, -6, 3, 4, -5]), 7)
self.assertEqual(maximum_subarray([-7, -9, -3, -5]), -3)

if __name__ == "__main__":
unittest.main()
60 changes: 60 additions & 0 deletions ik6_find_winners_of_election.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Find Winner Of Election
Given a list of votes, containing names of candidates in an election,
find which candidate received maximum number of votes.
If two or more candidate got the same number of votes,
return the lexicographically smaller name.
Example One
input: ["sam", "john", "jamie", "sam"]
Output: sam
Example Two
input: ["sam", "john", "sam", "john"]
Output: john
Notes
Constraints:
length of votes <= 105
length of votes[i] <= 105
number of characters in all votes[i] combined <= 105
"""

import unittest

def find_winner_of_election(votes: list[str]) -> str:
"""
Time Complexity: O(n)
Space Complexity: O(1)
"""
vote_count = {}
for vote in votes:
if vote not in vote_count:
vote_count[vote] = 1
else:
vote_count[vote] += 1
max_vote = max(vote_count.values())
# return min(vote_count, key=vote_count.get)
max_vote_candidates = [candidate
for candidate, v_count in vote_count.items()
if v_count == max_vote
]
return min(max_vote_candidates)


class TestFindWinnerOfElection(unittest.TestCase):
"""
Test case for the `find_winner_of_election` problem.
"""

def test_find_winner_of_election(self):
"""
Test case for the `find_winner_of_election` problem.
"""
self.assertEqual(find_winner_of_election(["sam", "john", "jamie", "sam"]), "sam")
self.assertEqual(find_winner_of_election(["sam", "john", "sam", "john"]), "john")

if __name__ == "__main__":
unittest.main()
117 changes: 117 additions & 0 deletions lc136_single_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""
Given a non-empty array of integers nums,
every element appears twice except for one.
Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,1]
Output: 1
Example 2:
Input: nums = [4,1,2,1,2]
Output: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
1 <= nums.length <= 3 * 104
-3 * 104 <= nums[i] <= 3 * 104
Each element in the array appears twice except for one element which appears only once.
"""
import unittest

def single_number_using_xor(nums: list[int]) -> int:
"""
Returns the single number in the list.
Algorithm used: XOR
Time Complexity: O(n)
Space Complexity: O(1)
"""
result = 0
for num in nums:
result ^= num
return result


def single_number_using_two_pointers(nums: list[int]) -> int:
"""
Returns the single number in the list.
Algorithm used: Loop
Time Complexity: O(n)
Space Complexity: O(1)
"""
if len(nums) == 1:
return nums[0]

nums.sort()
first, second = 0, 1
while second < len(nums):
if nums[first] == nums[second]:
first += 2
if second + 2 < len(nums):
second += 2
else:
return nums[second+1]
continue
else:
return nums[first]
return 0


def single_number_brute_force(arr):
"""
Returns the single number in the list.
Algorithm used: Brute Force
Time Complexity: O(n log n)
Space Complexity: O(1)
"""
arr.sort()
for index in range(0, len(arr), 2):
if index == len(arr)-1:
return arr[index]
if arr[index] != arr[index+1]:
return arr[index]
return 0



class TestSingleNumber(unittest.TestCase):
"""
Test the `single_number` function
"""
def test_single_number_xor(self):
"""
Test the `single_number_using_xor` function
"""
self.assertEqual(single_number_using_xor([2,2,1]), 1)
self.assertEqual(single_number_using_xor([4,1,2,1,2]), 4)
self.assertEqual(single_number_using_xor([1]), 1)

def test_single_number_two_pointers(self):
"""
Test the `single_number_using_two_pointers` function
"""
self.assertEqual(single_number_using_two_pointers([2,2,1]), 1)
self.assertEqual(single_number_using_two_pointers([4,1,2,1,2]), 4)
self.assertEqual(single_number_using_two_pointers([1]), 1)

def test_single_number_brute_force(self):
"""
Test the `single_number_brute_force` function
"""
self.assertEqual(single_number_brute_force([2,2,1]), 1)
self.assertEqual(single_number_brute_force([4,1,2,1,2]), 4)
self.assertEqual(single_number_brute_force([1]), 1)


if __name__ == "__main__":
unittest.main()
75 changes: 75 additions & 0 deletions lc169_majority_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Given na aray nums of size n, return the majority element.
The majority element is the element that appears more than n/2 times.
You may assume that the majority element always exists ni the array.
Example 1:
Input: nums = (3,2,3] Output: 3
Example 2:
Input: nums = (2,2,1,1,1,2,21 Output: 2
Constraints:
• 12 nun. +%01
• -109<=nums[i]<=109
"""

import unittest
# import math


def majority_element_by_sorting(nums: list[int]) -> int:
"""
Given an array nums of size n, return the majority element.
Algorithm: Sorting
Time Complexity: O(nlogn) to sort the object
Space Complexity: O(1) - no new data structures are created
"""
nums.sort()
return nums[len(nums) // 2]


def majority_element_by_hashmap(nums: list[int]) -> int:
"""
Given an array nums of size n, return the majority element.
Algorithm:
Time Complexity: O(n)
The method iterates over each element in the input array nums once.
Space Complexity: O(n)
he size of the hash map is at most n (since there are n unique elements in the array),
"""
hash_map = {}
for num in nums:
if num in hash_map:
hash_map[num] += 1
else:
hash_map[num] = 1
# for num, nums_count in hash_map.items():
# if nums_count >= math.floor(len(nums)/2):
# return num
return max(hash_map, key=hash_map.get)


class TestMajorityElement(unittest.TestCase):
"""
Test the `majority_element` function
"""
def test_majority_element_by_sorting(self):
"""
Test the `majority_element_by_sorting` function
"""
self.assertEqual(majority_element_by_sorting([3, 2, 3]), 3)
self.assertEqual(majority_element_by_sorting([2, 2, 1, 1, 1, 2, 2]), 2)


def test_majority_element_by_hashmap(self):
"""
Test the `majority_element_by_sorting` function
"""
self.assertEqual(majority_element_by_hashmap([3, 2, 3]), 3)
self.assertEqual(majority_element_by_hashmap([2, 2, 1, 1, 1, 2, 2]), 2)


if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit 9cff854

Please sign in to comment.