-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanual_input.py
More file actions
65 lines (54 loc) · 1.53 KB
/
manual_input.py
File metadata and controls
65 lines (54 loc) · 1.53 KB
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
''' Manual DFA
manually input dfa
'''
def dfa():
num_states = 0
dfa = {}
while True:
try:
num_states = int(input('How many states? '))
break
except ValueError:
print('Invalid Input (not a number)')
alphabet_raw = input('What is your alphabet? Type all the symbols in a string: ')
# removes duplicate symbols
alphabet = ''.join(dict.fromkeys(alphabet_raw))
for i in range(0, num_states):
state = 'S' + str(i)
transitions = {}
accepting = False
print('')
if (i == 0):
print('\tState 0 (start state) :')
else:
print('\tState', i, ':')
while True:
raw = input("\t\t\tIs this state accepting? (y/n): ")
if (raw == 'y' or raw == 'n'):
accepting = True if raw == 'y' else False
break
else:
print('\t\t\tInvalid input (must be y or n)')
print('\t\t=> Transitions <=')
for c in alphabet:
while True:
try:
print(
'\t\t\t State ( 0 -',
num_states - 1,
') transitioned to over',
c,
'?'
)
val = int(input('\t\t\t > '))
if (val in range(0, num_states)):
break
else:
print('\t\t\tInvalid Input, choose a number within the given range')
except ValueError:
print('\t\t\tInvalid Input (not a number)')
val_altered = 'S' + str(val)
transitions.update({c: val_altered})
info = (accepting, transitions)
dfa.update({state: info})
return dfa, alphabet