-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysteryTest.py
executable file
·53 lines (43 loc) · 1.45 KB
/
mysteryTest.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
# Use `fuzzer`, `ddmin` and the given `mystery_test`
# to find the minimal length string that causes
# `mystery_test` to return 'FAIL'
#
# Once you've found the string, put it in the `answer`
# variable and submit.
#
answer = "" # put in your answer here
from unit3 import mystery_test
import random
def fuzzer():
string_length = int(random.random() * 1024) # Strings up to 1024 characters long
out = ""
for i in range(0, string_length):
out += chr(int(random.random() * 96 + 32)) # filled with ASCII 32..128
return out
def ddmin(s,test):
n = 2 # Initial granularity
while len(s) >= 2:
start = 0
subset_length = len(s) / n
some_complement_is_failing = False
while start < len(s):
complement = s[:start] + s[start + subset_length:]
if test(complement) == "FAIL":
s = complement
n = max(n - 1, 2)
some_complement_is_failing = True
break
start += subset_length
if not some_complement_is_failing:
if n == len(s):
break
n = min(n * 2, len(s))
return s
while True:
input = fuzzer()
if mystery_test(input) != "PASS":
answer = ddmin(input,mystery_test)
print "============================================"
print "input:",input
print "answer:",answer
break