# Coding Challenge

#Coding Challenge

It's very common to participate in coding challenge when you interview for an intern or fulltime position. Today you will try to solve one using the knowledge we have learned so far.

Have fun 😃

# Assignment 22

This is a classic LeetCode (opens new window) problem. Try not to search in the internet for solution.

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Example 1:

Input: s = "()"
Output: true

Example 2:

Input: s = "()[]{}"
Output: true

Example 3:

Input: s = "(]"
Output: false

Example 4:

Input: s = "([)]"
Output: false

Example 5:

Input: s = "{[]}"
Output: true

Create a script named valid_parentheses.py. Inside the script, create a function named is_valid that takes one argument and returns True/False depends on the previous setting. Test your function to see if it works for the 5 examples above.

You can use the following codes for testing

assert is_valid("()") is True
assert is_valid("()[]{}") is True
assert is_valid("(]") is False
assert is_valid("([)]") is False
assert is_valid("{[]}") is True
1
2
3
4
5

You can include more test cases if you prefer.

Sample Solution
def is_valid(s):
    stack = []
    for char in s:
        if char in ["(", "[", "{"]:
            stack.append(char)
        elif char in [")", "]", "}"]:
            if len(stack) == 0:
                return False
            last = stack.pop()
            if char == ")" and last != "(":
                return False
            elif char == "]" and last != "[":
                return False
            elif char == "}" and last != "{":
                return False

    return len(stack) == 0


assert is_valid("()") is True
assert is_valid("()[]{}") is True
assert is_valid("(]") is False
assert is_valid("([)]") is False
assert is_valid("{[]}") is True
assert is_valid("") is True
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

If you like these types of challenge, feel free to register an account and practice on your own. There's another famous similar site, HackerRank (opens new window).

# Assignment 23

Create a script named undo.py. And copy/paste the following content. Finish the five functions (add, subtract, multiply, divide, and undo) so that the script would pass the assertions.





 




 




 




 




 










































value = 0
stack = []


def add(x):
    global value
    pass


def subtract(x):
    global value
    pass


def multiply(x):
    global value
    pass


def divide(x):
    global value
    pass


def undo():
    global value
    pass


assert value == 0

add(1)
assert value == 1

add(2)
assert value == 3

undo()
assert value == 1

subtract(1)
assert value == 0

subtract(2)
assert value == -2

multiply(2)
assert value == -4

undo()
assert value == -2

undo()
assert value == 0

add(10)
assert value == 10

divide(2)
assert value == 5

divide(5)
assert value == 1

undo()
assert value == 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

You can include more test cases if you prefer.

Sample Solution
value = 0
stack = []


def add(x):
    global value
    value += x
    stack.append(("add", x))


def subtract(x):
    global value
    value -= x
    stack.append(("subtract", x))


def multiply(x):
    global value
    value *= x
    stack.append(("multiply", x))


def divide(x):
    global value
    value /= x
    stack.append(("divide", x))


def undo():
    global value
    if len(stack) > 0:
        operation, x = stack.pop()
        if operation == "add":
            value -= x
        elif operation == "subtract":
            value += x
        elif operation == "multiply":
            value /= x
        elif operation == "divide":
            value *= x


assert value == 0

add(1)
assert value == 1

add(2)
assert value == 3

undo()
assert value == 1

subtract(1)
assert value == 0

subtract(2)
assert value == -2

multiply(2)
assert value == -4

undo()
assert value == -2

undo()
assert value == 0

add(10)
assert value == 10

divide(2)
assert value == 5

divide(5)
assert value == 1

undo()
assert value == 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79