Added test files
This commit is contained in:
parent
8587825317
commit
d5745bb25f
117
calc.py
Normal file
117
calc.py
Normal file
@ -0,0 +1,117 @@
|
||||
NUMBER_TOKEN = 0
|
||||
PLUS_TOKEN = 1
|
||||
MINUS_TOKEN = 2
|
||||
TIMES_TOKEN = 3
|
||||
DIVIDE_TOKEN = 4
|
||||
LEFT_BRACKET_TOKEN = 5
|
||||
RIGHT_BRACKET_TOKEN = 6
|
||||
WHITESPACE_TOKEN = 7
|
||||
EOF_TOKEN = 8
|
||||
|
||||
program = input("Please enter an expression to evaluate: ")
|
||||
tokens = []
|
||||
|
||||
i = 0
|
||||
while i < len(program):
|
||||
if program[i].isdigit():
|
||||
tmp_number = 0
|
||||
while i < len(program) and program[i].isdigit() :
|
||||
tmp_number *= 10
|
||||
tmp_number += ord(program[i]) % 48
|
||||
i += 1
|
||||
tokens.append([NUMBER_TOKEN, tmp_number])
|
||||
i -= 1
|
||||
elif program[i] == "+":
|
||||
tokens.append([PLUS_TOKEN])
|
||||
elif program[i] == "-":
|
||||
tokens.append([MINUS_TOKEN])
|
||||
elif program[i] == "*":
|
||||
tokens.append([TIMES_TOKEN])
|
||||
elif program[i] == "/":
|
||||
tokens.append([DIVIDE_TOKEN])
|
||||
elif program[i] == "(":
|
||||
tokens.append([LEFT_BRACKET_TOKEN])
|
||||
elif program[i] == ")":
|
||||
tokens.append([RIGHT_BRACKET_TOKEN])
|
||||
elif program[i] == " ":
|
||||
tokens.append([WHITESPACE_TOKEN])
|
||||
else:
|
||||
print("Invalid charatcer: " + i)
|
||||
i += 1
|
||||
tokens.append([EOF_TOKEN])
|
||||
|
||||
ast = []
|
||||
NUMBER_AST = 0
|
||||
PLUS_AST = 1
|
||||
MINUS_AST = 2
|
||||
TIMES_AST = 3
|
||||
DIVIDE_AST = 4
|
||||
NEGATE_AST = 5
|
||||
|
||||
index = 0
|
||||
|
||||
def current ():
|
||||
return tokens[index][0]
|
||||
|
||||
def eat(token):
|
||||
global index
|
||||
if current() == token:
|
||||
index += 1
|
||||
else:
|
||||
print("Invalid token")
|
||||
|
||||
def eat_whitespace():
|
||||
while current() == WHITESPACE_TOKEN:
|
||||
eat (WHITESPACE_TOKEN)
|
||||
|
||||
def factor():
|
||||
node = []
|
||||
eat_whitespace()
|
||||
if current() == NUMBER_TOKEN:
|
||||
node = [NUMBER_AST, tokens[index][1]]
|
||||
eat (NUMBER_TOKEN)
|
||||
elif current() == LEFT_BRACKET_TOKEN:
|
||||
eat (LEFT_BRACKET_TOKEN)
|
||||
node = expr()
|
||||
eat (RIGHT_BRACKET_TOKEN)
|
||||
else:
|
||||
eat (MINUS_TOKEN)
|
||||
node = [NEGATE_AST, factor()]
|
||||
eat_whitespace()
|
||||
return node
|
||||
def term():
|
||||
node = factor()
|
||||
while current() in [TIMES_TOKEN, DIVIDE_TOKEN]:
|
||||
if current () == TIMES_TOKEN:
|
||||
eat (TIMES_TOKEN)
|
||||
node = [TIMES_AST, node, factor()]
|
||||
else:
|
||||
eat (DIVIDE_TOKEN)
|
||||
node = [DIVIDE_AST, node, factor()]
|
||||
return node
|
||||
def expr():
|
||||
node = term()
|
||||
while current() in [PLUS_TOKEN, MINUS_TOKEN]:
|
||||
if current() == PLUS_TOKEN:
|
||||
eat (PLUS_TOKEN)
|
||||
node = [PLUS_AST, node, term()]
|
||||
else:
|
||||
eat (MINUS_TOKEN)
|
||||
node = [MINUS_AST, node, term()]
|
||||
return node
|
||||
|
||||
def interpret (ast):
|
||||
if ast[0] == NUMBER_AST:
|
||||
return ast[1]
|
||||
elif ast[0] == PLUS_AST:
|
||||
return interpret(ast[1]) + interpret(ast[2])
|
||||
elif ast[0] == MINUS_AST:
|
||||
return interpret(ast[1]) - interpret(ast[2])
|
||||
elif ast[0] == TIMES_AST:
|
||||
return interpret(ast[1]) * interpret(ast[2])
|
||||
elif ast[0] == DIVIDE_AST:
|
||||
return interpret(ast[1]) / interpret(ast[2])
|
||||
elif ast[0] == NEGATE_AST:
|
||||
return -interpret(ast[1])
|
||||
|
||||
print("Your answer: " + str(interpret(expr())))
|
29
cipher.py
Normal file
29
cipher.py
Normal file
@ -0,0 +1,29 @@
|
||||
print ("Welcome to bybb's Python Caesar Cipher Program!")
|
||||
option = input("Do you want to? 'encode' or 'decode'")
|
||||
while not option in ["encode", "decode"]:
|
||||
option = input("Do you want to? 'encode' or 'decode'")
|
||||
|
||||
cipher_text = input("Cipher Text: ")
|
||||
rot = int(input("Rotation Amount: "))
|
||||
|
||||
ciphered_string = ""
|
||||
|
||||
if option == "encode":
|
||||
for i in cipher_text:
|
||||
if i.isalpha():
|
||||
if ord(i) > 90:
|
||||
ciphered_string = ciphered_string + chr(((ord(i) % 97 + rot) % 26) + 97)
|
||||
else:
|
||||
ciphered_string = ciphered_string + chr(((ord(i) % 65 + rot) % 26) + 65)
|
||||
else:
|
||||
ciphered_string = ciphered_string + i
|
||||
else:
|
||||
for i in cipher_text:
|
||||
if i.isalpha():
|
||||
if ord(i) > 90:
|
||||
ciphered_string = ciphered_string + chr(((ord(i) % 97 - rot) % 26) + 97)
|
||||
else:
|
||||
ciphered_string = ciphered_string + chr(((ord(i) % 65 - rot) % 26) + 65)
|
||||
else:
|
||||
ciphered_string = ciphered_string + i
|
||||
print("Your ciphered string is: " + ciphered_string)
|
5
options.py
Normal file
5
options.py
Normal file
@ -0,0 +1,5 @@
|
||||
x = input("Your options are: add / subtract")
|
||||
while not x in ["add", "subtract"]:
|
||||
x = input("Your options are: add / subtract")
|
||||
|
||||
print("You chose: " + x[0])
|
Loading…
x
Reference in New Issue
Block a user