GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added 2 new solutions · danasmile/coding-problems@05e74e3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Other
    • basic_calculator.py
  • Trees
    • same_tree.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Other/basic_calculator.py‎

Copy file name to clipboard
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
Basic Calculator
3+
4+
Implement a basic calculator to evaluate a simple expression string.
5+
The expression string may contain open '(' and closing parentheses ')',
6+
the plus '+' or minus sign '-', non-negative integers and empty spaces ' '.
7+
8+
Input: '(1+(4+5+2)-3)+(6+8)'
9+
Output: 23
10+
11+
Input: ' 2-1 + 2 '
12+
Output: 3
13+
14+
=========================================
15+
Start from the first character and respect the math rules. When brackets come, go inside the brackets
16+
and compute the inner result, after that continue with adding or subtracting.
17+
Time Complexity: O(N)
18+
Space Complexity: O(K) , much less than N (the deepest level of brackets)
19+
'''
20+
21+
22+
############
23+
# Solution #
24+
############
25+
26+
def basic_calculator(s):
27+
return calculate(s, 0)[0]
28+
29+
def calculate(s, i):
30+
sign = 1 # 1 means '+' and -1 means '-'
31+
res = 0
32+
num = 0
33+
34+
while i < len(s) and s[i] != ')':
35+
if s[i] >= '0' and s[i] <= '9':
36+
# find the whole number
37+
num = num * 10 + int(s[i])
38+
elif s[i] == '(':
39+
# calculate inside the brackets
40+
brackets = calculate(s, i + 1)
41+
res += brackets[0] * sign
42+
i = brackets[1] # continue from the new i
43+
elif s[i] != ' ':
44+
# add the previous number using the old sign
45+
res += num * sign
46+
num = 0
47+
48+
if s[i] == '-':
49+
sign = -1
50+
elif s[i] == '+':
51+
sign = 1
52+
53+
i += 1
54+
55+
res += num * sign
56+
return (res, i)
57+
58+
59+
###########
60+
# Testing #
61+
###########
62+
63+
# Test 1
64+
# Correct result => 23
65+
print(basic_calculator('(1+(4+5+2)-3)+(6+8)'))
66+
67+
# Test 2
68+
# Correct result => 3
69+
print(basic_calculator(' 2-1 + 2 '))
Collapse file

‎Trees/same_tree.py‎

Copy file name to clipboard
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
Same Tree
3+
4+
Given two binary trees, write a function to check if they are the same or not.
5+
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
6+
7+
Input: 1 1
8+
/ \ / \
9+
2 3 2 3
10+
Output: True
11+
12+
Input: 1 1
13+
/ \
14+
2 2
15+
Output: False
16+
17+
=========================================
18+
Traverse both trees in same time and if something isn't equal, return False.
19+
Time Complexity: O(N)
20+
Space Complexity: O(N) , because of the recursion stack (but this is if the tree is one branch), O(LogN) if the tree is balanced.
21+
'''
22+
23+
24+
############
25+
# Solution #
26+
############
27+
28+
# import TreeNode class from tree_helpers.py
29+
from tree_helpers import TreeNode
30+
31+
def is_same_tree(p, q):
32+
if (p is None) and (p == q):
33+
return True
34+
35+
if (p is None) or (q is None):
36+
return False
37+
38+
if p.val != q.val:
39+
return False
40+
41+
# check left
42+
if not is_same_tree(p.left, q.left):
43+
return False
44+
45+
# check right
46+
if not is_same_tree(p.right, q.right):
47+
return False
48+
49+
return True
50+
51+
52+
###########
53+
# Testing #
54+
###########
55+
56+
# Test 1
57+
# Correct result => True
58+
p = TreeNode(1, TreeNode(2), TreeNode(3))
59+
q = TreeNode(1, TreeNode(2), TreeNode(3))
60+
print(is_same_tree(p, q))
61+
62+
# Test 2
63+
# Correct result => False
64+
p = TreeNode(1, TreeNode(2))
65+
q = TreeNode(1, None, TreeNode(2))
66+
print(is_same_tree(p, q))

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.