Created
May 27, 2019 08:25
-
-
Save aklyk/64fc771115e49c6ed5300ffd9dba9067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def checkComplexNumber(complexNum): | |
state = 'S0' #начальное состояние | |
string = complexNum | |
it = iter(string) | |
isTrue = False | |
while True: | |
try: | |
IN = next(it) | |
except StopIteration: | |
IN = 'EOL' | |
print(state) | |
if state=='S0': #состояние S0 | |
if IN.isdigit(): | |
if int(IN) >=1 and int(IN) <= 9: | |
state='S1' | |
continue | |
else: | |
state='SyntaxError' | |
continue | |
if state=='S1': #состояние S1 | |
if IN.isdigit(): | |
if int(IN) >=0 and int(IN) <= 9: | |
state='S1' | |
continue | |
elif IN == '+' or IN == '-': | |
state='S2' | |
continue | |
else: | |
state='SyntaxError' | |
continue | |
if state=='S2': | |
if IN == 'i': | |
state='S3' | |
continue | |
else: | |
state='SyntaxError' | |
continue | |
if state=='S3': | |
if IN.isdigit(): | |
if int(IN) >=1 and int(IN) <= 9: | |
state='S4' | |
continue | |
else: | |
state='SyntaxError' | |
continue | |
if state=='S4': | |
if IN.isdigit(): | |
if int(IN) >=0 and int(IN) <= 9: | |
state='S4' | |
continue | |
elif IN == 'EOL': | |
state='S0' | |
isTrue = True | |
continue | |
else: | |
state='SyntaxError' | |
continue | |
if state=='SyntaxError': | |
break | |
return isTrue | |
complexNumbers = ['123+i123','1+i1','12-i6'] | |
complexNumbers.append(input('Enter complex to test: ')) | |
for i in complexNumbers: | |
assert checkComplexNumber(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment