Skip to content

Commit

Permalink
Homework 11
Browse files Browse the repository at this point in the history
  • Loading branch information
ykirnev committed Dec 8, 2024
1 parent 396316c commit 3abcb12
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 20241203/1/check/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class C(metaclass=dump):
def __init__(self, val):
self.val = val

def add(self, other, another=None):
return self.val + other + (another or self.val)

c = C(10)
print(c.add(9))
print(c.add(9, another=10))
5 changes: 5 additions & 0 deletions 20241203/1/check/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__init__: (10,), {}
add: (9,), {}
29
add: (9,), {'another': 10}
29
14 changes: 14 additions & 0 deletions 20241203/1/check/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Rectangle(metaclass=dump):
def __init__(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

def perimeter(self):
return 2 * (self.width + self.height)

rect = Rectangle(5, 10)
print(rect.area())
print(rect.perimeter())
5 changes: 5 additions & 0 deletions 20241203/1/check/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__init__: (5, 10), {}
area: (), {}
50
perimeter: (), {}
30
11 changes: 11 additions & 0 deletions 20241203/1/check/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Point(metaclass=dump):
def __init__(self, x, y):
self.x = x
self.y = y

def distance_to(self, other_x, other_y):
return ((self.x - other_x) ** 2 + (self.y - other_y) ** 2) ** 0.5

p = Point(0, 0)
print(p.distance_to(3, 4))
print(p.distance_to(6, 8))
5 changes: 5 additions & 0 deletions 20241203/1/check/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__init__: (0, 0), {}
distance_to: (3, 4), {}
5.0
distance_to: (6, 8), {}
10.0
17 changes: 17 additions & 0 deletions 20241203/1/prog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
class dump(type):
def __new__(cls, name, bases, dct):
new_dct = {}
for name, val in dct.items():
if callable(val):
def make(method_name, method):
def fun(self, *args, **kwargs):
print(f"{method_name}: {args}, {kwargs}")
return method(self, *args, **kwargs)
return fun
new_dct[name] = make(name, val)
else:
new_dct[name] = val
return super().__new__(cls, name, bases, new_dct)

exec(sys.stdin.read())
14 changes: 14 additions & 0 deletions 20241203/2/check/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Эта программа вычисляет e
store 100 i
store 1 I
store 1 f

Переменные e и n коварно не объявляем — в них по умолчанию 0

loop: div I f a
add e a e
add n I n
mul f n f
sub i I i
ifge i I loop
out e
1 change: 1 addition & 0 deletions 20241203/2/check/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7182818284590455
5 changes: 5 additions & 0 deletions 20241203/2/check/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
store 5 x
store 3 y
ifgt x y undefined_label
out x
stop
Empty file added 20241203/2/check/2.out
Empty file.
12 changes: 12 additions & 0 deletions 20241203/2/check/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

store 20 i
store 2 I
store 4 f

loop: div I f a
add e a e
add n I n
mul f n f
sub i I i
ifge i I loop
out e
1 change: 1 addition & 0 deletions 20241203/2/check/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.8243606352091255
86 changes: 86 additions & 0 deletions 20241203/2/prog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import math
import sys

def parse_number(val):
try:
return float(val)
except ValueError:
return 0.0
def interpret_assembler(program):
lines = program.splitlines()
coms = []
labels = {}
vars = {}
for i, line in enumerate(lines):
line = line.lstrip()
if not line:
continue
if ":" in line:
label, _, rest = line.partition(":")
labels[label.strip()] = len(coms)
line = rest.lstrip()
if not line:
continue
parts = line.split()
coms.append(parts)
for com in coms:
if com[0].startswith("if") and len(com) == 4:
label = com[3]
if label not in labels:
return
cnt = 0
output = []
while cnt < len(coms):
com = coms[cnt]
op = com[0]
match op:
case "stop":
break
case "store" if len(com) == 3:
val = parse_number(com[1])
vars[com[2]] = val
case op if op in {"add", "sub", "mul", "div"} and len(com) == 4:
src = vars.get(com[1], 0.0)
operand = vars.get(com[2], 0.0)
dest = com[3]
match op:
case "add":
vars[dest] = src + operand
case "sub":
vars[dest] = src - operand
case "mul":
vars[dest] = src * operand
case "div":
vars[dest] = src / operand if operand != 0 else math.inf

case op if op.startswith("if") and len(com) == 4:
src = vars.get(com[1], 0.0)
operand = vars.get(com[2], 0.0)
label = com[3]
match op:
case "ifeq" if src == operand:
cnt = labels[label]
continue
case "ifne" if src != operand:
cnt = labels[label]
continue
case "ifgt" if src > operand:
cnt = labels[label]
continue
case "ifge" if src >= operand:
cnt = labels[label]
continue
case "iflt" if src < operand:
cnt = labels[label]
continue
case "ifle" if src <= operand:
cnt = labels[label]
continue
case "out" if len(com) == 2:
val = vars.get(com[1], 0.0)
output.append(val)
cnt += 1
for val in output:
print(val)
s = sys.stdin.read()
interpret_assembler(s)

0 comments on commit 3abcb12

Please sign in to comment.