Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a Python implementation of the classes Const, Square, Inc, and Print that allows for the chaining syntax you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Chain: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Const(Chain): pass class Square: def __call__(self, value): return Chain(value ** 2) class Inc: def __call__(self, value): return Chain(value + 1) class Print: def __call__(self, value): print(value) return Chain(value) # Example usage Const(5) >> Square() >> Inc() >> Inc() >> Print() |
1 | Const(5) >> Square() >> Inc() >> Inc() >> Print() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Chain: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Const(Chain): pass class Square: def __call__(self, value): return Chain(value ** 2) class Inc: def __call__(self, value): return Chain(value + 1) class Print: def __call__(self, value): print(value) return Chain(value) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class Chain: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Const(Chain): pass class Square: def __call__(self, value): return Chain(value ** 2) class Inc: def __call__(self, value): return Chain(value + 1) class Print: def __call__(self, value): print(value) return Chain(value) answer = Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print() expected = 0 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
625
Traceback (most recent call last):
File "/usr/src/app/main.py", line 31, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got <__main__.Chain object at 0x7f5f1469f760> instead of 0
Testing if the previous output contains the string 625
: True
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a Python implementation of the classes Const, Square, Inc, and Print that allows for the chaining syntax you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Const: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Square: def __call__(self, value): return Operation(value ** 2) class Inc: def __call__(self, value): return Operation(value + 1) class Print: def __call__(self, value): print(value) class Operation: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) # Example usage Const(5) >> Square() >> Inc() >> Inc() >> Print() |
1 | Const(5) >> Square() >> Inc() >> Inc() >> Print() |
1 | 27
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Const: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Square: def __call__(self, value): return Operation(value ** 2) class Inc: def __call__(self, value): return Operation(value + 1) class Print: def __call__(self, value): print(value) class Operation: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | class Const: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Square: def __call__(self, value): return Operation(value ** 2) class Inc: def __call__(self, value): return Operation(value + 1) class Print: def __call__(self, value): print(value) class Operation: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) answer = Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print() expected = 0 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
625
Traceback (most recent call last):
File "/usr/src/app/main.py", line 34, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got None instead of 0
Testing if the previous output contains the string 625
: True
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a Python implementation of the classes Const, Square, Inc, and Print that allows for the chaining syntax you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Const: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Square: def __call__(self, value): return Operation(value ** 2) class Inc: def __call__(self, value): return Operation(value + 1) class Print: def __call__(self, value): print(value) return value class Operation: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) |
1 | Const(5) >> Square() >> Inc() >> Inc() >> Print() |
1 | 27
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Const: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Square: def __call__(self, value): return Operation(value ** 2) class Inc: def __call__(self, value): return Operation(value + 1) class Print: def __call__(self, value): print(value) return value class Operation: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class Const: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) class Square: def __call__(self, value): return Operation(value ** 2) class Inc: def __call__(self, value): return Operation(value + 1) class Print: def __call__(self, value): print(value) return value class Operation: def __init__(self, value): self.value = value def __rshift__(self, other): return other(self.value) answer = Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print() expected = 0 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
625
Traceback (most recent call last):
File "/usr/src/app/main.py", line 35, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got 625 instead of 0
Testing if the previous output contains the string 625
: True
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a Python implementation of the classes Const, Square, Inc, and Print that allows for the chained operation you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Operation: def __init__(self, value=None): self.value = value def __rshift__(self, other): return other(self.value) class Const(Operation): def __init__(self, value): super().__init__(value) class Square(Operation): def __call__(self, value): return Operation(value ** 2) class Inc(Operation): def __call__(self, value): return Operation(value + 1) class Print(Operation): def __call__(self, value): print(value) return Operation(value) # Example usage Const(5) >> Square() >> Inc() >> Inc() >> Print() |
1 | Const(5) >> Square() >> Inc() >> Inc() >> Print() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Operation: def __init__(self, value=None): self.value = value def __rshift__(self, other): return other(self.value) class Const(Operation): def __init__(self, value): super().__init__(value) class Square(Operation): def __call__(self, value): return Operation(value ** 2) class Inc(Operation): def __call__(self, value): return Operation(value + 1) class Print(Operation): def __call__(self, value): print(value) return Operation(value) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Operation: def __init__(self, value=None): self.value = value def __rshift__(self, other): return other(self.value) class Const(Operation): def __init__(self, value): super().__init__(value) class Square(Operation): def __call__(self, value): return Operation(value ** 2) class Inc(Operation): def __call__(self, value): return Operation(value + 1) class Print(Operation): def __call__(self, value): print(value) return Operation(value) answer = Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print() expected = 0 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
625
Traceback (most recent call last):
File "/usr/src/app/main.py", line 32, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got <__main__.Operation object at 0x7fdf19eaf6d0> instead of 0
Testing if the previous output contains the string 625
: True
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a python class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a Python implementation of the classes `Const`, `Square`, `Inc`, and `Print` that allows for the chained operation you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Operation: def __init__(self, value=None): self.value = value def __rshift__(self, other): if isinstance(other, Operation): return other(self.value) return other def __call__(self, value): self.value = value return self class Const(Operation): def __init__(self, value): super().__init__(value) class Square(Operation): def __call__(self, value): return super().__call__(value ** 2) class Inc(Operation): def __call__(self, value): return super().__call__(value + 1) class Print(Operation): def __call__(self, value): print(value) return super().__call__(value) # Usage Const(5) >> Square() >> Inc() >> Inc() >> Print() |
1 | Const(5) >> Square() >> Inc() >> Inc() >> Print() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Operation: def __init__(self, value=None): self.value = value def __rshift__(self, other): if isinstance(other, Operation): return other(self.value) return other def __call__(self, value): self.value = value return self class Const(Operation): def __init__(self, value): super().__init__(value) class Square(Operation): def __call__(self, value): return super().__call__(value ** 2) class Inc(Operation): def __call__(self, value): return super().__call__(value + 1) class Print(Operation): def __call__(self, value): print(value) return super().__call__(value) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | class Operation: def __init__(self, value=None): self.value = value def __rshift__(self, other): if isinstance(other, Operation): return other(self.value) return other def __call__(self, value): self.value = value return self class Const(Operation): def __init__(self, value): super().__init__(value) class Square(Operation): def __call__(self, value): return super().__call__(value ** 2) class Inc(Operation): def __call__(self, value): return super().__call__(value + 1) class Print(Operation): def __call__(self, value): print(value) return super().__call__(value) answer = Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print() expected = 0 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
625
Traceback (most recent call last):
File "/usr/src/app/main.py", line 38, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got <__main__.Print object at 0x7f6fc2043730> instead of 0
Testing if the previous output contains the string 625
: True