Skip to content

Commit 1a7f3d1

Browse files
committed
adding algo
1 parent 096dce2 commit 1a7f3d1

6 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import defaultdict
4+
5+
class Solution:
6+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
7+
8+
answer = defaultdict(list)
9+
10+
for word in strs:
11+
answer[''.join(sorted(word))].append(word)
12+
13+
return list(answer.values())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution(object):
5+
def isHappy(self, n):
6+
7+
set_answer = set()
8+
9+
while n != 1:
10+
11+
if n in set_answer:
12+
return False
13+
else:
14+
set_answer.add(n)
15+
16+
n = sum([int(c)**2 for c in str(n)])
17+
18+
return True
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_42_group_anagrams import Solution
4+
5+
class GroupAnagramsTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.groupAnagrams(strs = ["eat","tea","tan","ate","nat","bat"])
10+
target = [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.groupAnagrams(strs = [""])
16+
target = [[""]]
17+
self.assertEqual(output, target)
18+
19+
def test_third_pattern(self):
20+
solution = Solution()
21+
output = solution.groupAnagrams(strs = ["a"])
22+
target = [["a"]]
23+
self.assertEqual(output, target)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_43_two_sum import Solution
4+
5+
class TwoSumTestCase(unittest.TestCase):
6+
7+
def test_is_two_sum(self):
8+
solution = Solution()
9+
output = solution.twoSum(nums=[2,7,11,15], target=9)
10+
target = [0,1]
11+
for k, v in enumerate(target):
12+
self.assertEqual(v, output[k])
13+
14+
def test_is_no_two_sum(self):
15+
solution = Solution()
16+
output = solution.twoSum(nums=[2,7,11,15], target=0)
17+
target = []
18+
self.assertEqual(output, target)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_44_happy_number import Solution
4+
5+
class HappyNumberTestCase(unittest.TestCase):
6+
7+
def test_is_happy_number(self):
8+
solution = Solution()
9+
output = solution.isHappy(n=19)
10+
self.assertTrue(output)
11+
12+
def test_is_no_happy_number(self):
13+
solution = Solution()
14+
output = solution.isHappy(n=2)
15+
self.assertFalse(output)

0 commit comments

Comments
 (0)