Practice2
中文版:练习 2
Using function
- Define a function called ‘Comparison’ that takes three inputs: a and b. The function should output the smaller value between a and b.
# call and your function
print(Comparison(3,5)) # expected output: 3a='hi'
b=5
print(Comparison(a,b)) # comparison between a string and an integerChange the function and rename the function to ‘Comparison_print’ to directly print the smaller value between a and b within the function and return nothing
# call and your function
print(Comparison_print(3,5))- Define a function named ‘Cumulate’ that takes a single input n. The function should return the sum of all integers from 1 to n (i.e., 1 + 2 + 3 + … + n).
# use for loop# use while loopn=5
Cumulate(n) # expected output 15Change the function and rename it to ‘Cumulate_from’ that takes two input x and y, the function should be able to return the sum of all integers from x to y
# use for or while loop# call your function
x=3
y=5
Cumulate_from(x,y) # expected output 12- Define a function ‘Verification’ that takes three input variables: a, b and result
the function will calculate a/b and compare to the input ‘result’
return true if a/b and ‘result’ are the same, return false if not
# call your function
x=6
y=2
z=3
Verification(x,y,z)==False # expected output False# call your function
x=6
y=0
z=3
Verification(x,y,z)Function Description: list_greater(a, b)
Purpose: This function compares two lists, a and b, and determines if one list is element-wise strictly greater than the other.
Input: Two lists, a and b, containing comparable elements (e.g., numbers, strings).
Behavior:
- Check Lengths:
If a and b have different lengths, return “not comparable”.
If they have the same length, proceed to element-wise comparison.
- Element-wise Comparison:
Compare each corresponding pair: a[0] > b[0], a[1] > b[1], …, a[n] > b[n].
- Output:
If all elements in a are greater than those in b, return a.
If all elements in b are greater than those in a, return b.
If neither list is fully greater, return False.