Question
Write a function that determines a maximum value from a variable number of arguments. The function is described below. Requirements: 1) Use the maxOfTwo function
Write a function that determines a maximum value from a variable number of arguments. The function is described below.
Requirements: 1) Use the maxOfTwo function you wrote for Lab #C. That maxOfTwo function takes two parameters and returns the greater of the two.
2) The new function maxOf should take any number of parameters and return the largest of them. It should call maxOfTwo as many times as required.
Hints: ? In Python you can define a variable with an * preceding the parameter name, to gain access to a functions remaining parameters. For example def maxOf( *args ) : will allow you to access all the arguments passed to the function via the args Tuple ? You can use for oneArg in args: to step though each of the arguments ? Within the loop you can use maxOfTwo of get the larger of: a) the biggest value you found so far, and b) the arg value in the current loop iteration. This can be accomplished with something like: priorBig = maxOfTwo( priorBig, oneArg ) ? Trick: if you use the method above, you need to set priorBig to a known value before the loop. I suggest: priorBig = args[ 0 ]
3) Setup your code to use print to write out the results of these calls: a) maxOf( 24, 10 ) b) maxOf( 102, 12, 4, 101 ) c) maxOf( 6, 7, 8, 9, 10 ) d) maxOf( ?6, 7, 345, 9, ?400 )
LAB C
Write two specific functions that determine a maximum value. Each function is described below. Requirements:
1) The first function maxOfTwo should take two parameters and return the greater of the two. Within the function there should be an if statement that determines which value to return.
2) The second function maxOfThree should take three parameters and return the largest of the three. It should call maxOfTwo twice to get the answer.
3) Setup your code to call the functions with these values: a) maxOfTwo( 5, 7 ) b) maxOfTwo( 105, 7 ) c) maxOfTwo( "yes", "no" ) d) maxOfThree( 8, 16, 1 ) e) maxOfThree( 28, 16, 1 ) f) maxOfThree( 28, 16, 106 ) g) maxOfTwo( False, 0 ) h) maxOfTwo( 0, False ) i) maxOfTwo( 0, "" )
4) The Preferred output is one result per line. You can either store the result of each call, then print it out; or call the functions directly from a print statement.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started