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,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,23 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import re

class Solution:
def isPalindrome(self, s: str) -> bool:

# To lowercase
s = s.lower()

# Remove non-alphanumeric characters
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)

# Determine if s is palindrome or not

len_s = len(s)

for i in range(len_s//2):

if s[i] != s[len_s - 1 - i]:
return False

return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from typing import List, Union, Collection, Mapping, Optional, Dict

class Table:

def __init__(self, name:str, columns: int, rows: Dict, row_count: int = 0):
self.name = name
self.columns = columns
self.rows = rows
self.row_count = row_count

class SQL:

def __init__(self, names: List[str], columns: List[int]):
"""
two string arrays, names and columns, both of size n.

The ith table is represented by the name names[i] and contains columns[i] number of columns
"""
self.names = names
self.columns = columns
self.data = {}

for i in range(0, len(self.names)):
self.data[self.names[i]] = Table(
name = self.names[i],
columns=self.columns[i],
rows={}
)

return


def ins(self, name: str, row: List[str]) -> bool:
"""
Inserts row into the table name and returns true.
If row.length does not match the expected number of columns, or name is not a valid table,
returns false without any insertion.
"""
table: Table = self.data.get(name)
if table is None:
return False
if len(row) != table.columns:
return False
table.row_count += 1
table.rows[table.row_count] = row

return True


def rmv(self, name: str, rowId: int) -> None:
"""
Removes the row rowId from the table name.
If name is not a valid table or there is no row with id rowId, no removal is performed.
"""
table: Table = self.data.get(name, None)
if table is None:
return

if rowId in table.rows:
del table.rows[rowId]

return


def sel(self, name: str, rowId: int, columnId: int) -> str:
"""
Returns the value of the cell at the specified rowId and columnId in the table name.
If name is not a valid table, or the cell (rowId, columnId) is invalid, returns "<null>".
"""
null = "<null>"
table: Table = self.data.get(name, None)
if table is None:
return null

if rowId not in table.rows:
return null

try:
return table.rows[rowId][columnId - 1]
except Exception:
return null


def exp(self, name: str) -> List[str]:
"""
Returns the rows present in the table name.
If name is not a valid table, returns an empty array.
Each row is represented as a string, with each cell value (including the row's id) separated by a ",".
"""
table: Table = self.data.get(name, None)
if table is None:
return []

result = []

for row_id, row in table.rows.items():
row_str = ",".join(row)
result.append(f"{row_id},{row_str}")

return result





# Your SQL object will be instantiated and called as such:
# obj = SQL(names, columns)
# param_1 = obj.ins(name,row)
# obj.rmv(name,rowId)
# param_3 = obj.sel(name,rowId,columnId)
# param_4 = obj.exp(name)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import math

class Solution:
def minimumTime(self, jobs: List[int], workers: List[int]) -> int:

jobs.sort()
workers.sort()

return max([math.ceil(n/d) for n,d in zip(jobs, workers)])



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

class Solution:
def merge(self,
nums1: List[int],
m: int, nums2: List[int],
n: int) -> None:

nums1[m:m+n] = nums2
nums1.sort()

return nums1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:

def removeElement(self, nums: List[int], val: int) -> int:

while val in nums:
nums.remove(val)

return len(nums)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.merge_sorted_array import Solution

class MergeSortedArrayTestCase(unittest.TestCase):

def test_merge_sorted_array(self):
solution = Solution()
output = solution.merge(nums1=[1,2,3,0,0,0], m=3, nums2=[2,5,6], n=3)
target = [1,2,2,3,5,6]
for k, v in enumerate(target):
self.assertEqual(output[k], v)
11 changes: 11 additions & 0 deletions tests/test_150_questions_round_22/test_remove_element_round_22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.remove_element import Solution

class RemoveElementTestCase(unittest.TestCase):

def test_remove_element(self):
solution = Solution()
output = solution.removeElement(nums=[3,2,2,3], val=3)
target = 2
self.assertEqual(output, target)