-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
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
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)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
__init__: (10,), {} | ||
add: (9,), {} | ||
29 | ||
add: (9,), {'another': 10} | ||
29 |
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
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()) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
__init__: (5, 10), {} | ||
area: (), {} | ||
50 | ||
perimeter: (), {} | ||
30 |
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
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)) |
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
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 |
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
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()) |
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
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.7182818284590455 |
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
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.
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
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.8243606352091255 |
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
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) |