Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -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 []
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions tests/test_150_questions_round_22/test_43_two_sum_round_22.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions tests/test_150_questions_round_22/test_44_happy_number_round_22.py
Original file line number Diff line number Diff line change
@@ -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)