Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

define a coroutine function 'linear equation' which takes two arguments 'a' and 'b'

any coroutine derived from linear_equatuin should be capable of taking a number as


input and evaluating the expression a*(x**2)+b
the coroutine after evaluating the expression should print the message "Expression,
3*x^2+4, with x being 6 equals 112"
def linear_equation(a, b):
while True:
x = yield
e = a*(x**2)+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+'
equals '+str(e))

if __name__ == "__main__":
a = float(input())

b = float(input())

equation1 = linear_equation(a, b)

next(equation1)

equation1.send(6)
***************************
define a decorator 'coroutine_decorator' which can decorate any corouting function
the decorator must create the coroutine, call next on it and return the coroutine
that is ready for accepting any input
for ex
@coroutine_decorator
def linear_equation(a, b):
....
e1 = linear_equation(3, 4)

e1.send(6)

# Define 'coroutine_decorator' below


def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper

# Define coroutine 'linear_equation' as specified in previous exercise


@coroutine_decorator
def linear_equation(a, b):
while True:
x = yield
e = a*(x**2)+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+'
equals '+str(e))

if __name__ == "__main__":
a = float(input())

b = float(input())
equation1 = linear_equation(a, b)

equation1.send(6)
**********************
Define coroutine 'linear_equation' and coroutine_decorator functions as requested
in previous test case
define a coroutine function numberParser which is capable of converting the passed
input into an integer and also sends the integers to two linear equation coroutines
- 'equation1' and 'equation2'
equation1 represents linear equation coroutine with a =3 and b=4
equation2 represents linear equation coroutine with a=2 and b=-1

# Define the function 'coroutine_decorator' below


def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper

# Define the coroutine function 'linear_equation' below


@coroutine_decorator
def linear_equation(a, b):
while True:
x = yield
e = a*(x**2)+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+'
equals '+str(e))

# Define the coroutine function 'numberParser' below


@coroutine_decorator
def numberParser():
equation1 = linear_equation(3, 4)
equation2 = linear_equation(2, -1)
# code to send the input number to both the linear equations
while True:
x = yield
equation1.send(x)
equation2.send(x)

def main(x):
n = numberParser()
n.send(x)

if __name__ == "__main__":
x = float(input())

res = main(x);

You might also like