Functions program

Part 1: Simple Functions
Challenge 1
Begin by copying the following program into a new file.
# these are the basic arithmetic functions you will be using for this challenge

# function: add
# input: two integers/floats
# processing: adds the two supplied values
# output: returns the sum (integer/float)
def add(a,b):
return a b

Don't use plagiarized sources. Get Your Custom Essay on
Functions program
For $10/Page 0nly
Order Essay

# function: sub
# input: two integers/floats
# processing: subtracts the two supplied values
# output: returns the difference (integer/float)
def sub(a,b):
return a-b

# function: mult
# input: two integers/floats
# processing: multiplies the two supplied values
# output: returns the product (integer/float)
def mult(a,b):
return a*b

# function: div
# input: two integers/floats
# processing: divides the first value by the second value
# output: returns the result (float)
def div(a,b):
return a/b

# function: sqrt
# input: one integer/float
# processing: computes the square root of the supplied value
# output: returns the square root (float)
def sqrt(a):
return a**0.5

# function: square
# input: one integer/float
# processing: raises the supplied integer/float to the 2nd power
# output: returns the square (integer/float)
def square(a):
return a**2
Your task is to translate some simple expressions into function calls. Here are the rules:
You can only fill in the blank with a single line of code
You cannot use any math operations (i.e. , -, /, *, // or % are strictly prohibited)
You cannot use any print statements
You cannot reduce your terms (i.e. if you are asked to translate 1 2 3 you can’t simply add these together yourself and supply the number 5 to the program)
The only thing you can use are calls to the functions above, in any order
You cannot use any calls to any functions other than the ones above (i.e. input, len, etc. are all prohibited)
Here is an example to get you started:
# translate this expression:
# y = mx b
m = 3
x = 2
b = 5

y = __________
print(y) # you should expect 11 as your outputYou can solve this by create a function call that looks like the following:
y = add(mult(m,x),b)Challenge 1: Expression 1# translate this expression:
# x = (3-4) (1*2)
x = __________
print(x) # you should expect 1 as your outputChallenge 1: Expression 2# translate this expression:
# x = 5 1 7 9 13 12
x = __________
print(x) # you should expect 47 as your outputChallenge 1: Expression 3# translate this expression:
# x = (5 1) / (7 2 3)
x = __________
print(x) # you should expect 0.5 as your outputChallenge 1: Expression 4# compute the distance between these two points
# point 1
x1 = 0
y1 = 0

# point 2
x2 = 100
y2 = 100

distance = ______________________

print (distance) # you should expect 141.4213562373095 as your outputRecall that the distance formula is defined as follows:

Challenge 2
Write two functions called ‘maximum’ and ‘minimum’ – these function should accept two arguments and return the larger/smaller of the two supplied values. For the purpose of this program you can always assume that the arguments being supplied are numeric. Describe your function using IPO notation. Your program should work perfectly with the following code:
a = 5
b = 10
c = 15
d = 20
e = 20

ans1 = maximum(a,b)
ans2 = maximum(a,c)
ans3 = maximum(a,d)
print (ans1,ans2,ans3) # 10 15 20

ans4 = minimum(d,c)
ans5 = minimum(d,b)
ans6 = minimum(d,a)
print (ans4,ans5,ans6) # 15 10 5

ans7 = maximum( maximum(a,b), maximum(c,d) )
print (“The biggest is:”, ans7)

ans8 = maximum(d,e) # d and e are the same, so either is considered the maximum
ans8 = minimum(d,e) # d and e are the same, so either is considered the minimum
print(ans8, ans8) # 20 20Challenge 3
Write a function called ‘valid_date’ which accepts two arguments – a month and a day (both integers, will always be integers). Test to see if the date being described is valid or not. If the date is valid your function should return a True value, and if it is not it should return a False value. For the purpose of this program you can assume February has 28 days. Describe your function using IPO notation. Your program should work perfectly with the following code:
print (valid_date(99,1)) # False
print (valid_date(1,99)) # False
print (valid_date(99,99)) # False

print (valid_date(-99,1)) # False
print (valid_date(1,-99)) # False
print (valid_date(-99,-99)) # False

print (valid_date(9,25)) #True
print (valid_date(10,15)) # True
print (valid_date(11,31)) # False
print (valid_date(2,28)) # True
print (valid_date(2,29)) # FalseChallenge 4
# write a function called ‘simple_sort_version1’ that accepts two values. you can assume
# that your three values will always be the same data type (all ints, all floats or all strings).
# sort these two values in ascending order and return them in that order.
# you may use any function that you’ve written so far in this assignment if you’d like to (maximum, minimum, etc)

# your function should work perfectly with the following lines of code
a,b = simple_sort_version1(10,20)
print (a,b) # 10 20

a,b = simple_sort_version1(20,10)
print (a,b) # 10 20

a,b = simple_sort_version1(30,30)
print (a,b) # 30 30Challenge 5
# next, write a new function called ‘simple_sort_version2’ that accepts three values. you can assume
# that your three values will always be the same data type (all ints, all floats or all strings).
# sort these values in ascending order and return them.
# you may use any function that you’ve written so far in this assignment if you’d like to (simple_sort_version1, maximum, minimum, etc)

# your function should work perfectly with the following lines of code
a,b,c = simple_sort_version2(10,20,30)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(10,30,20)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(30,20,10)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(30,20,20)
print (a,b,c) # 20 20 30

Calculator

Calculate the price of your paper

Total price:$26

Need a better grade?
We've got you covered.

Order your paper