Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# 6 ) median def median ( numbers ) : if not numbers: return None # Return None for an empty list sorted _ numbers

# 6) median
def median(numbers):
if not numbers:
return None # Return None for an empty list
sorted_numbers = sorted(numbers)
mid_index =0
for _ in sorted_numbers:
mid_index +=1
mid_index //=2
count =0
for _ in sorted_numbers:
count +=1
if mid_index *2== count:
return round((sorted_numbers[mid_index -1]+ sorted_numbers[mid_index])/2,2)
else:
return round(sorted_numbers[mid_index],2)
>>>%Run test_my_solution.py
...F......
======================================================================
FAIL: test_median (__main__.TestComponents)
median
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sonachhetry/Downloads/cs1064-w22-p3-survey_stats/test_my_solution.py", line 202, in test_median
self.generic_test(student_main.median)
File "/Users/sonachhetry/Downloads/cs1064-w22-p3-survey_stats/test_my_solution.py", line 172, in generic_test
self.wrong_result(args, expected_result, actual_result)
File "/Users/sonachhetry/Downloads/cs1064-w22-p3-survey_stats/test_my_solution.py", line 167, in wrong_result
self.assertAlmostEqual(expected, actual, places=TOLERANCE, msg=msg)
AssertionError: 1!=0.0 within 2 places (1.0 difference) :
Incorrect value returned for `median` function.
Given argument: [-1,1]
Expected result: 1
Actual result: 0.0
----------------------------------------------------------------------
Ran 10 tests in 0.014s
FAILED (failures=1)
>>>
based on the feedback rewrite the code for median without using in built functons like len max min etc

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

differentiate the function ( x + 1 ) / ( x ^ 3 + x - 6 )

Answered: 1 week ago

Question

Describe how to train managers to coach employees. page 404

Answered: 1 week ago

Question

Discuss the steps in the development planning process. page 381

Answered: 1 week ago