Skip to content

Commit a2d0302

Browse files
authored
Merge pull request #1549 from ivanpenaloza/february15
adding algo
2 parents 4fd0edc + ec493f1 commit a2d0302

5 files changed

Lines changed: 84 additions & 0 deletions

File tree

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def countNodes(self, root: Optional[TreeNode]) -> int:
12+
13+
if not root:
14+
return 0
15+
else:
16+
return self.countNodes(root.left) + self.countNodes(root.right) + 1
17+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function countNodes(root: TreeNode | null): number {
4+
if (!root) {
5+
return 0;
6+
} else {
7+
return countNodes(root.left) + countNodes(root.right) + 1;
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_77_count_complete_tree_nodes import Solution, TreeNode
5+
6+
7+
class CountNodesTestCase(unittest.TestCase):
8+
9+
def test_count_none(self):
10+
solution = Solution()
11+
tree = None
12+
output = solution.countNodes(root=tree)
13+
self.assertEqual(0, output)
14+
15+
16+
def test_count_non_empty_tree(self):
17+
solution = Solution()
18+
tree = TreeNode(1, TreeNode(2), TreeNode(3))
19+
output = solution.countNodes(root=tree)
20+
self.assertEqual(3, output)

0 commit comments

Comments
 (0)