AITC Wiki

Practice2 solution

练习 2 参考答案

Practice2 solution

中文版:练习 2 参考答案

Using function

  1. Define a function called ‘Comparison’ that takes three inputs: a and b. The function should output the smaller value between a and b.
def Comparison(a,b):
 if a<b:
 return a
 else:
 return b
# call and your function
print(Comparison(3,5)) # expected output: 3
a='hi'
b=5
print(Comparison(a,b)) # comparison between a string and an integer
 
# you'll get an error cause string type is not comparable with an int type

Change 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

def Comparison_print(a,b):
 if a<b:
 print("a is smaller", a)
 elif a==b:
 print("a==b")
 else:
 print("b is smaller", b)
# call and your function
print(Comparison_print(3,5))
 
# you will get an extra None value for this print, cause nothing is returned from function Comparison_print
  1. 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
def Cumulate(n):
 
 result=0
 for i in range(n+1):
 result+=i
 
 return result
n=5
Cumulate(n) # expected output 15
# use while loop
def Cumulate(n):
 i=1
 result=0
 while i <= n:
 result+=i
 i+=1
 return result
n=5
Cumulate(n) # expected output 15

Change 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

# using for loop
def Cumulate_from(x,y):
 
 result=0
 for i in range(x,y+1):
 result+=i
 
 return result
# call your function
x=3
y=5
Cumulate_from(x,y) # expected output 12
  1. 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

def Verification(a,b,result):
 return a/b == result
# 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)
 
# you'll get an error here cause it's not allowed to do caculation division by 0

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:

  1. Check Lengths:

If a and b have different lengths, return “not comparable”.

If they have the same length, proceed to element-wise comparison.

  1. Element-wise Comparison:

Compare each corresponding pair: a[0] > b[0], a[1] > b[1], …, a[n] > b[n].

  1. 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.

def list_greater(a, b):
 
 if len(a)!=len(b):
 return "not comparable"
 
 a_greater = 0 # to count how many times a is greater than b
 for i in range(len(a)):
 if a[i] > b[i]:
 a_greater+=1
 
 if a_greater == len(a): # if for the whole list, items in a are all greater than b
 return a
 elif a_greater > 0 and a_greater < len(a): # if several but not all the items in a is greater than b
 return False
 else: # if if for the whole list, items in b are all greater than a
 return b
a=[1,2,3,4]
b=[5,6,7,8,9]
 
print(list_greater(a,b))