Assignment 6 Question 1 Part 1: Simple Functions For this part you will be completing a series of short programming challenges that use various function concepts. Each challenge should be saved as its...

1 answer below »
The files are attached below. Please do the coding in the exact manner and order my professor needs it to be.


Assignment 6 Question 1 Part 1: Simple Functions For this part you will be completing a series of short programming challenges that use various function concepts. Each challenge should be saved as its own file (i.e. Kapp_Craig_Assign6_part1_challenge1.py). 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 # 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 output You 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 output Challenge 1: Expression 2 # translate this expression: # x = 5 + 1 + 7 + 9 + 13 + 12 x = __________ print(x) # you should expect 47 as your output Challenge 1: Expression 3 # translate this expression: # x = (5 + 1) / (7 + 2 + 3) x = __________ print(x) # you should expect 0.5 as your output Challenge 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 output Recall 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 20 Challenge 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)) # False Challenge 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 30 Challenge 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 assignment 6 question 2 Part 2 For part 2 of this assignment you will be writing a series of programs that build on one another, so it's important to progress through this part in order. You should submit each individual part of the assignment as a separate file to the Assignment #6 category inside of Brightspace. Part 2a: Number Analysis Functions Write a series of five functions that determine if a given positive integer is EVEN, ODD, PRIME, PERFECT and ABUNDANT. Each of these functions should accept a single integer as an argument and return whether the integer meets the criteria for that classification. Here's some IPO notation to get you started: # function: is_even # input: a positive integer # processing: determines if the supplied number is even # output: boolean # function: is_odd # input: a positive integer # processing: determines if the supplied number is odd # output: boolean # function: is_prime # input: a positive integer # processing: determines if the supplied number is prime. a prime number is # a number that only has two factors - the number 1 and itself. # for example, 17 is prime because it only has two factors (1 and 17). # in order to determine this you might need to count the # of factors # between 1 and the number you are testing. a note on efficiency: if you are # testing a number (say, 15) and you find a factor (say, 3) - do you need to even continue # to find additional factors? The number 1 should not be considered to be prime. # output: boolean # function: is_perfect # input: a positive integer # processing: determines if the supplied number is perfect. a perfect number # is a number that is equal to the sum of its factors (i.e. the # number 6 is perfect because 6 = 1 + 2 + 3) # output: boolean # function: is_abundant # input: a positive integer # processing: determines if the supplied number is abundant. an abundant number # is a number
Answered Same DayNov 02, 2021

Answer To: Assignment 6 Question 1 Part 1: Simple Functions For this part you will be completing a series of...

Shubham Kumar answered on Nov 02 2021
135 Votes
Source Files/part1/Dhwani_Assign6_part1_challenge1.py
# 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
# function: sub
# input: two integers/floats
# processing: subtracts th
e 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
# Challenges
# translate this expression:
# x = (3-4) + (1*2)
x = add(sub(3, 4), mult(1, 2))
print(x)
# translate this expression:
# x = 5 + 1 + 7 + 9 + 13 + 12
x = add(5, add(1, add(7, add(9, add(13, 12)))))
print(x)
# translate this expression:
# x = (5 + 1) / (7 + 2 + 3)
x = div(add(5, 1), add(7, add(2, 3)))
print(x)
# compute the distance between these two points
# point 1
x1 = 0
y1 = 0
# point 2
x2 = 100
y2 = 100
distance = sqrt(add(square(sub(x2, x1)), square(sub(y2, y1))))
print(distance)
Source Files/part1/Dhwani_Assign6_part1_challenge2.py
# function: maximum
# input: two integers/floats
# processing: checks the maximum of the two argument
# output: returns the maximum (integer/float)
def maximum(a,b):
return (a if a>b else b);
# function: minimum
# input: two integers/floats
# processing: checks the minimum of the two argument
# output: returns the minimum (integer/float)
def minimum(a,b):
return (a if aif(__name__ == "__main__") :
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 20
Source Files/part1/Dhwani_Assign6_part1_challenge3.py
# function: valid_date
# input: two integers
# processing: checks if the date, month pair represents a valid date
# output: returns boolean
def valid_date(month, date) :
if date<=0 or month<=0 or date>31 or month>12:
return False
if month==2:
return (True if date<=28 else False)
elif (month==4 or month==6 or month==9 or month==11) :
return (True if date<=30 else False)
else :
return (True if date<=31 else False)
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)) # False
Source Files/part1/Dhwani_Assign6_part1_challenge4.py
# function:...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here