Hacker News new | ask | show | jobs
by admin82326 76 days ago
Python programmers! Complete the interpreter for me! THIS IS WHAT THE SOURCE CODE LOOKS LIKE: --------------------------------------------------------------------- "ladder - interpreted programming language" "this is a set (S) {1, 2, 3, 4, 5} "" "this is an amount (A) of the set (S) with an interval (I) [0;5) Ai S[i] "" "this is the product (P) of the sum of the "elements of the set (S) and an amount (A) of the set (S) "with an interval (I) I Pi S[i] + A ------------------------------------------------------------ HERE IS A PIECE OF THE INTERPRETER: ------------------------------------------------------------ import sys

SET = []; INTERVAL = []; SUMMA = 0; PRODUCT = 1;

def set_(line): global SET SET = list(map(int, line[1:-1].split(', '))) print(SET)

def interval_(line): global INTERVAL, SUMMA, PRODUCT interval, iterator, expression = line.split(' ') match interval[0]: case '(': l = int(interval[1])+1 case '[': l = int(interval[1]) match interval[-1]: case ')': r = int(interval[-2]) case ']': r = int(interval[-2])+1 INTERVAL = list(range(l, r)) if iterator: match iterator[0]: case 'A': for i in INTERVAL: SUMMA += SET[i] print(SUMMA) case 'P': for i in INTERVAL: PRODUCT = SET[i] print(PRODUCT)

def comment_(line): if line != '""': print(line) if line[-1] == '"': print()

def parse(line): match line[0]: case '"': comment_(line) case '{': set_(line) case '(' | '[': interval_(line) case 'I': print(sum([s*s for s in [e+sum(SET) for e in SET]])) # :-( case _: print(f'ERROR {line}')

def scan(f): for line in f: line = line.strip() if line: parse(line)

if __name__ == '__main__': f = open(sys.argv[1]); scan(f) f.close() ---------------------------------------------------- RESULT -------------------------------------------------- python ladder.py program.l "ladder - interpreted programming language" "this is a set (S) 1 2 3 4 5 "this is an amount (A) of the set (S) with an interval (I) 15 "this is the product (P) of the sum of the "elements of the set (S) and an amount (A) of the set (S) "with an interval (I) 1630