Assignment: You need to write two Python functions, mcompute() and lcompute(). W
ID: 3788002 • Letter: A
Question
Assignment: You need to write two Python functions, mcompute() and lcompute(). When used, such as by the program below: print (mcompute("sayhello", 30, 20)) print (mcompute("add", mcompute("sub", 70, 40), 20)) print (mcompute("sub", 30, 20)) print (mcompute("loop", 1, 5)) print (mcompute("equalOrLarger", 10, 5)) print (mcompute("equalOrLarger", 5, 5)) print (mcompute("equalOrLarger", 5, 15)) print (lcompute("find", 1, [1, 2, 3])) print (lcompute("find", 3, [1, 2, 3])) print (lcompute("add", 7, [1, 2, 3])) print (lcompute("del", 3, [1, 2, 3])) The result is: hello 30 and 20, nice to meet you. 50 10 12345 True True False 0 2 [1, 2, 3, 7] [1, 2]
Explanation / Answer
def mcompute(ele,*var):
if ele == "sayhello":
result="hello "
for i in var:
result += str(i) + " and "
return result[0:len(result)-5]+", nice to meet you."
elif ele == "add":
n = 0
for i in var:
n += int(i)
return n
elif ele == "sub":
n = 0
n = int(var[0]) - int (var[1])
return n
elif ele == "loop":
s=""
for x in range(int(var[0]), int(var[1])+1):
s+=str(x)
return s
elif ele == "equalOrLarger":
if int(var[0]) >= int(var[1]):
return "True"
else:
return "False"
return;
print(mcompute("sayhello", 30, 20))
print(mcompute("add", mcompute("sub", 70, 40), 20))
print(mcompute("sub", 30, 20))
print(mcompute("loop", 1, 5))
print(mcompute("equalOrLarger", 10, 5))
print(mcompute("equalOrLarger",5,5))
print(mcompute("equalOrLarger", 5, 15))
def lcompute(s,e,var):
if s == "find":
return var.index(e)
elif s == "add":
var.append(e)
return var
elif s == "del":
var.remove(e)
return var
print(lcompute("find", 1, [1, 2, 3]))
print(lcompute("find", 3, [1, 2, 3]))
print(lcompute("add", 7, [1, 2, 3]))
print(lcompute("del", 3, [1, 2, 3]))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.