Skip to content

Commit 55d517e

Browse files
authored
Merge pull request #1469 from ivan1016017/november28
adding algos
2 parents 12d0675 + 6e6178f commit 55d517e

File tree

8 files changed

+211
-0
lines changed

8 files changed

+211
-0
lines changed
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
16+
len_s = len(s)
17+
18+
for i in range(len_s//2):
19+
20+
if s[i] != s[len_s - 1 - i]:
21+
return False
22+
23+
return True
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from typing import List, Union, Collection, Mapping, Optional, Dict
2+
3+
class Table:
4+
5+
def __init__(self, name:str, columns: int, rows: Dict, row_count: int = 0):
6+
self.name = name
7+
self.columns = columns
8+
self.rows = rows
9+
self.row_count = row_count
10+
11+
class SQL:
12+
13+
def __init__(self, names: List[str], columns: List[int]):
14+
"""
15+
two string arrays, names and columns, both of size n.
16+
17+
The ith table is represented by the name names[i] and contains columns[i] number of columns
18+
"""
19+
self.names = names
20+
self.columns = columns
21+
self.data = {}
22+
23+
for i in range(0, len(self.names)):
24+
self.data[self.names[i]] = Table(
25+
name = self.names[i],
26+
columns=self.columns[i],
27+
rows={}
28+
)
29+
30+
return
31+
32+
33+
def ins(self, name: str, row: List[str]) -> bool:
34+
"""
35+
Inserts row into the table name and returns true.
36+
If row.length does not match the expected number of columns, or name is not a valid table,
37+
returns false without any insertion.
38+
"""
39+
table: Table = self.data.get(name)
40+
if table is None:
41+
return False
42+
if len(row) != table.columns:
43+
return False
44+
table.row_count += 1
45+
table.rows[table.row_count] = row
46+
47+
return True
48+
49+
50+
def rmv(self, name: str, rowId: int) -> None:
51+
"""
52+
Removes the row rowId from the table name.
53+
If name is not a valid table or there is no row with id rowId, no removal is performed.
54+
"""
55+
table: Table = self.data.get(name, None)
56+
if table is None:
57+
return
58+
59+
if rowId in table.rows:
60+
del table.rows[rowId]
61+
62+
return
63+
64+
65+
def sel(self, name: str, rowId: int, columnId: int) -> str:
66+
"""
67+
Returns the value of the cell at the specified rowId and columnId in the table name.
68+
If name is not a valid table, or the cell (rowId, columnId) is invalid, returns "<null>".
69+
"""
70+
null = "<null>"
71+
table: Table = self.data.get(name, None)
72+
if table is None:
73+
return null
74+
75+
if rowId not in table.rows:
76+
return null
77+
78+
try:
79+
return table.rows[rowId][columnId - 1]
80+
except Exception:
81+
return null
82+
83+
84+
def exp(self, name: str) -> List[str]:
85+
"""
86+
Returns the rows present in the table name.
87+
If name is not a valid table, returns an empty array.
88+
Each row is represented as a string, with each cell value (including the row's id) separated by a ",".
89+
"""
90+
table: Table = self.data.get(name, None)
91+
if table is None:
92+
return []
93+
94+
result = []
95+
96+
for row_id, row in table.rows.items():
97+
row_str = ",".join(row)
98+
result.append(f"{row_id},{row_str}")
99+
100+
return result
101+
102+
103+
104+
105+
106+
# Your SQL object will be instantiated and called as such:
107+
# obj = SQL(names, columns)
108+
# param_1 = obj.ins(name,row)
109+
# obj.rmv(name,rowId)
110+
# param_3 = obj.sel(name,rowId,columnId)
111+
# param_4 = obj.exp(name)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import math
4+
5+
class Solution:
6+
def minimumTime(self, jobs: List[int], workers: List[int]) -> int:
7+
8+
jobs.sort()
9+
workers.sort()
10+
11+
return max([math.ceil(n/d) for n,d in zip(jobs, workers)])
12+
13+
14+
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+
4+
class Solution:
5+
def merge(self,
6+
nums1: List[int],
7+
m: int, nums2: List[int],
8+
n: int) -> None:
9+
10+
nums1[m:m+n] = nums2
11+
nums1.sort()
12+
13+
return nums1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
6+
def removeElement(self, nums: List[int], val: int) -> int:
7+
8+
while val in nums:
9+
nums.remove(val)
10+
11+
return len(nums)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.merge_sorted_array import Solution
4+
5+
class MergeSortedArrayTestCase(unittest.TestCase):
6+
7+
def test_merge_sorted_array(self):
8+
solution = Solution()
9+
output = solution.merge(nums1=[1,2,3,0,0,0], m=3, nums2=[2,5,6], n=3)
10+
target = [1,2,2,3,5,6]
11+
for k, v in enumerate(target):
12+
self.assertEqual(output[k], v)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.remove_element import Solution
4+
5+
class RemoveElementTestCase(unittest.TestCase):
6+
7+
def test_remove_element(self):
8+
solution = Solution()
9+
output = solution.removeElement(nums=[3,2,2,3], val=3)
10+
target = 2
11+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)