diff --git a/calc.py b/calc.py new file mode 100644 index 0000000..36e7b30 --- /dev/null +++ b/calc.py @@ -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()))) diff --git a/cipher.py b/cipher.py new file mode 100644 index 0000000..0ed509d --- /dev/null +++ b/cipher.py @@ -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) \ No newline at end of file diff --git a/options.py b/options.py new file mode 100644 index 0000000..b58979a --- /dev/null +++ b/options.py @@ -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]) \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..e511247 --- /dev/null +++ b/test.py @@ -0,0 +1 @@ +print("Hello, World") diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..644c3ba --- /dev/null +++ b/test2.py @@ -0,0 +1,20 @@ +var = 0 +def outer(): + global var + var = 1 + def inner(): + global var + var = 2 + print("inner:", str(var)) + inner() + print("outer:", str(var)) +outer() +print("main:", str(var)) + +var = 5 +def a(): + def b(): + return var + return b() + +print(str(a())) \ No newline at end of file