Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To practice testing for functions you write yourself Degree of Difficulty: For e

ID: 3803175 • Letter: T

Question

To practice testing for functions you write yourself Degree of Difficulty: For each of the following, implement the stated function, and test it: The function closest_to_zero3(num1, num2, num3) returns value closest to zero from its 3 inputs For example, given the numbers 2, 7,0, the number closes to 0 is 0. For another example, number closest to O when comparing 3, -1, 5 is -1. The function more_odds_than_even(num_list) returns True if the sum of all integers from num_list in odd numbered indices is greater than the sum of all integers of even numbered indices For example, the function should return False for the list [1, 2, 3] (Index portions O and 2 give the sum of 4 (1+3), and index position 1 gives the sum of 2) and True for the list [1, 2] (There is only one even index position (0). and one odd index position(1), and 2 > 1) Your testing can be done using the techniques given in the textbook (Chapter 15), or as demonstrated in the Lab 8 A document called a8q3.py containing your functions and testing code as described above You can put your test driver in this document, or you can submit a separate test-driver script named a8q3_testing.py. Be sure to include your name. NSID. student number course number and lecture section and laboratory section at the top of all documents Your function closest_to_zero3() takes 3 input values and the integer closest to 0 correctly. The doc-string for closest_to_zero3() is sufficiently descriptive, indicating its purpose and requirements. You designed test-cases and implemented a test driver that would reveal faults in your closest_to_zero3() Your function more_odds_than_even() takes one list as input and returns a boolean value indicating whether or the sum of integers n oddly numbered index positions is greater than the sum of integers in evenly numbered index positions. The doc-string for more_odds_than_even() is sufficiently descriptive, indicating its purpose and requirements You designed test-cases and implemented a test driver that would reveal faults in your more_odds_than_even().

Explanation / Answer

# python script to calculate minimum difference

import sys

def closest_to_zero(a, b, c):
"""Returns the closest to zero number by calculating absolute difference."""
num = a
min = sys.maxsize
diff = abs(a) # 12

if diff<min: #12
min = diff #12
num = a
diff = abs(b) #30
else:
diff = abs(b) #
  
if diff<min:
min = diff
num = b
diff = abs(c)
else:
diff = abs(c)
  
if diff<min:
num = c
min = diff
  
"""print the minimum of value, maintain num to get original value instead of absolute"""
print(num)


"""Function call stub to test our implementation"""
closest_to_zero(-11, 31, -1)
closest_to_zero(11, 12, -2)
closest_to_zero(100, 200, 300)
closest_to_zero(99,999, 9)
closest_to_zero(101, -101, 102)
closest_to_zero(-101, 101, -1)

# python script to check more_odd_than_even in a list
def more_odd_than_even(alist):
"""Returns true if odd indices sum is greater than even indices sum."""
odd = 0
even = 0
for index, value in enumerate(alist):
if index % 2==0:
even = even + value
else:
odd = odd + value
  
if(odd>even):
"""if odd indices sum is greater then return True."""
return True
else:
"""if odd indices sum is greater then return True."""
return False

"""sample lists for testing method"""
list_num1= [1, 2, 3]
list_num2 = [1, 2]
list_num3 = [2, 0, 3, 4]
list_num4 = [2]
list_num5 = [2, 2]
list_num6 = [3, 1]
ret1 = more_odd_than_even(list_num1)
ret2 = more_odd_than_even(list_num2)
ret3 = more_odd_than_even(list_num3)
ret4 = more_odd_than_even(list_num4)
ret5 = more_odd_than_even(list_num5)
ret6 = more_odd_than_even(list_num6)
print(ret1)
print(ret2)
print(ret3)
print(ret4)
print(ret5)
print(ret6)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote