((PYTHON)) For parts (a) - (c), I will give a purpose statement, and your job is
ID: 3663420 • Letter: #
Question
((PYTHON)) For parts (a) - (c), I will give a purpose statement, and your job is to write down the signature* of the function. For parts (d) and (e), I will give you a few sample inputs and the corresponding returned values. Your job for those two parts is to first give a purpose statement**, and then give the signature. Then, implement all of these functions.
***Note that if the function works with any numerical type, you can just use "number", but if it only works with integers, you should write "int".
a) cube(): takes a number and returns its cube. For example, cube(3) returns 27, and cube(-0.1) returns -0.001.
b) triplicate(): takes a string and returns a string consisting of three copies of that string concatenated together. For example, triplicate("na") returns "nanana", andtriplicate("..?") returns "..?..?..?".
c) avg(): takes four numbers and returns their average (specifically, the arithmetic mean). For example, avg(1, 2, 3, 4) returns 2.5, and avg(1.8, 1.9, 2, 2.3) returns 2.0.
d) count(): I'm not going to tell you exactly what it does, but I will give you a few examples:count("banana","na") returns 2, count("abracadabra","a") returns 5, andcount("foobar","baz") returns 0.
e) multiplicate(): I'm not going to tell you exactly what it does, but I will give you a few examples: multiplicate("x",5) returns "xxxxx", multiplicate("goo",3) returns"googoogoo", and multiplicate("never",0) returns "" (the empty string).
Explanation / Answer
I have written the working codes for the same.
a)
# your code goes here
def cube(x):
return (x*x*x)
a=3
b=cube(a)
print b
b)
# your code goes here
def triplicate(x):
return (x+x+x)
a="na"
b=triplicate(a)
print b
c)
# your code goes here
def avg(x1,x2,x3,x4):
return (x1+x2+x3+x4)/float(4)
b=avg(1,2,3,4)
print b
d)
def count(x1,x2):
return x1.count(x2)
b=count("banana","na")
print b
e)
# your code goes here
def multiplicate(x1,x2):
if(x2==0):
return ""
else:
return (x1*x2)
b=multiplicate("goo",3)
print b
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.