repl.it
@rayhu007/

screening

Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
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
from flask import Flask
app = Flask('app')

@app.route('/')
def hello_world():
  return 'Hello, World!'

app.run(host='0.0.0.0', port=8080)

starting=["(","[","{"]
ending=[")","]","}"]
# if the starting symbol is not closed, don't eval
# if a starting symbol is not closed, then if a start and closing paire is found, we should not eval
# if there is a closing symbol, even it doesn't matchm we shall eval
# if there is no outstanding symbol, it shall eval
# if the closing symbol without matching start, it shall eval

def should_we_eval(code): # -> bool
  countcurly=0
  countsquare=0 
  countpren=0
  count=0
  for i in code:
    if i in "(":
      countpren+=1
    elif i in ")":
      countpren-=1
    if i in "[":
      countsquare+=1
    elif i in "]":
      countsquare-=1
    if i in "{":
      countcurly+=1
    elif i in "}":
      countcurly-=1
    if i in starting:
      count+=1
    elif i in ending:
      count-=1
    if count<0:
      return True
    elif not(count>0) and countpren==0 or countcurly==0 or countsquare==0 :
      return True
  if count==0:
    return True
  return False


def check(code, expected):
  if should_we_eval(code) == expected:
    print("PASS", code)
  else:
    print("FAIL", code, "expected", str(expected))

check("hey(", False)
check("hey", True)
check("hey()", True)
check("))(", True)
check(")((", True)
check("))((", True)

check("[]", True)
check("{}", True)
check("[{}", False)
check("[}", True)

check("{[}", True)
check("([)", True)
check("([}", True)
check("{[}][", True)

check("{{[}]", True)
?