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,23 @@
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,22 @@
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,46 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import deque, defaultdict

class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
# Build graph: adjacency list with weights
graph = defaultdict(dict)

for (dividend, divisor), value in zip(equations, values):
graph[dividend][divisor] = value
graph[divisor][dividend] = 1.0 / value

def bfs(start: str, end: str) -> float:
# Check if variables exist in graph
if start not in graph or end not in graph:
return -1.0

# If start equals end and it exists in graph
if start == end:
return 1.0

# BFS to find path from start to end
queue = deque([(start, 1.0)]) # (node, accumulated_product)
visited = {start}

while queue:
node, product = queue.popleft()

# Check all neighbors
for neighbor, weight in graph[node].items():
if neighbor == end:
return product * weight

if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, product * weight))

return -1.0

# Process all queries
results = []
for dividend, divisor in queries:
results.append(bfs(dividend, divisor))

return results
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] {
// Build graph: adjacency list with weights
const graph = new Map<string, Map<string, number>>();

for (let i = 0; i < equations.length; i++) {
const [dividend, divisor] = equations[i];
const value = values[i];

if (!graph.has(dividend)) {
graph.set(dividend, new Map<string, number>());
}
if (!graph.has(divisor)) {
graph.set(divisor, new Map<string, number>());
}

graph.get(dividend)!.set(divisor, value);
graph.get(divisor)!.set(dividend, 1.0 / value);
}

function bfs(start: string, end: string): number {
// Check if variables exist in graph
if (!graph.has(start) || !graph.has(end)) {
return -1.0;
}

// If start equals end and it exists in graph
if (start === end) {
return 1.0;
}

// BFS to find path from start to end
const queue: [string, number][] = [[start, 1.0]]; // [node, accumulated_product]
const visited = new Set<string>([start]);

while (queue.length > 0) {
const [node, product] = queue.shift()!;

// Check all neighbors
const neighbors = graph.get(node)!;
for (const [neighbor, weight] of neighbors.entries()) {
if (neighbor === end) {
return product * weight;
}

if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push([neighbor, product * weight]);
}
}
}

return -1.0;
}

// Process all queries
const results: number[] = [];
for (const [dividend, divisor] of queries) {
results.push(bfs(dividend, divisor));
}

return results;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22.ex_86_clone_graph import Solution, Node
from src.my_project.interviews.top_150_questions_round_22\
.ex_86_clone_graph import Solution, Node
from typing import Optional, List


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_87_evaluate_division import Solution
from typing import Optional, List


class EvaluateDivisionTestCase(unittest.TestCase):

def test_example_1(self):
"""
Example 1: Basic division chain
Given: a / b = 2.0, b / c = 3.0
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
return: [6.0, 0.5, -1.0, 1.0, -1.0]
"""
solution = Solution()
equations = [["a", "b"], ["b", "c"]]
values = [2.0, 3.0]
queries = [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]]
expected = [6.0, 0.5, -1.0, 1.0, -1.0]

result = solution.calcEquation(equations, values, queries)
self.assertEqual(result, expected)

def test_example_2(self):
"""
Example 2: Multiple variables and chains
Given: a / b = 1.5, b / c = 2.5, bc / cd = 5.0
queries are: a / c = ?, c / b = ?, bc / cd = ?, cd / bc = ?
return: [3.75, 0.4, 5.0, 0.2]
"""
solution = Solution()
equations = [["a", "b"], ["b", "c"], ["bc", "cd"]]
values = [1.5, 2.5, 5.0]
queries = [["a", "c"], ["c", "b"], ["bc", "cd"], ["cd", "bc"]]
expected = [3.75, 0.4, 5.0, 0.2]

result = solution.calcEquation(equations, values, queries)
self.assertEqual(result, expected)

def test_example_3(self):
"""
Example 3: Single equation with various queries
Given: a / b = 0.5
queries are: a / b = ?, b / a = ?, a / c = ?, x / y = ?
return: [0.5, 2.0, -1.0, -1.0]
"""
solution = Solution()
equations = [["a", "b"]]
values = [0.5]
queries = [["a", "b"], ["b", "a"], ["a", "c"], ["x", "y"]]
expected = [0.5, 2.0, -1.0, -1.0]

result = solution.calcEquation(equations, values, queries)
self.assertEqual(result, expected)