-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.py
97 lines (79 loc) · 2.92 KB
/
validator.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
lang = open('lang.txt', 'r').read().strip()
import re
code = ''
if lang == 'python3' or lang == 'pypy':
pattern = re.compile(r"(while|for|if|switch\s*\()")
if pattern.search(code):
print("Error: The code contains a while/for/if/switch statement.")
exit(171)
#check user used abs and min and max functions
pattern = re.compile(r"(abs|min|max|eval|exec)\s*\(")
# check user used ** operator
if '**' in code:
print('Error: ** operator is not allowed')
exit(171)
# check user used lambda
pattern = re.compile(r"\\blambda\\b")
if pattern.search(code):
print('Error: Lambda functions are not allowed')
exit(171)
# check user used def and class
pattern = re.compile(r"\\b(def|class)\\b")
if pattern.search(code):
print('Error: Defining functions and classes are not allowed')
exit(171)
# check user used yield and return
pattern = re.compile(r"\\b(yield|return)\\b")
if pattern.search(code):
print('Error: Returning and yielding are not allowed')
exit(171)
pattern = re.compile("(\\[|\\]|\\{|\\})")
if pattern.search(code):
print('Error: Arrays and dictionaries are not allowed')
exit(171)
if lang == 'cpp':
with open('code.txt', 'r') as f:
code = f.read()
pattern = re.compile(r"(while|if|for|switch)\s*\(")
if pattern.search(code):
print('Error: Loops and conditions are not allowed')
exit(171)
pattern = re.compile("#include\\s+<(?!iostream)[^>]+>")
if pattern.search(code):
print('Error: The code includes a library other than iostream.')
exit(171)
#check user used abs, min, max
pattern = re.compile(r"(abs|min|max|fabs)\s*\(")
if pattern.search(code):
print('Error: Restricted built-in function used')
exit(171)
# check user used goto
pattern = re.compile(r"\\bgoto\\b")
if pattern.search(code):
print('Error: goto is not allowed')
exit(171)
# check user used compare operators
pattern = re.compile(r"\\b(==|!=|<=|>=|<|>)\\b")
if pattern.search(code):
print('Error: Comparing operators are not allowed')
exit(171)
# check user used array and dictionary
pattern = re.compile("(\\[|\\]|\\{|\\})")
if pattern.search(code):
print('Error: Arrays and dictionaries are not allowed')
exit(171)
# check user used bitwise operators
pattern = re.compile("(&|\\||\\^|~)")
if pattern.search(code):
print('Error: Bitwise operators are not allowed')
exit(171)
# check user used logical operators
pattern = re.compile("(\\band\\b|\\bor\\b|\\bnot\\b)")
if pattern.search(code):
print('Error: Logical operators are not allowed')
exit(171)
# check user used && and ||
pattern = re.compile("(\\|\\||&&)")
if pattern.search(code):
print('Error: Logical operators are not allowed')
exit(171)