Median of the List
Return the median of nums: put the numbers in order, and if there is an odd number of them take the middle one; if there is an even number, take the average of the two middle ones.
The answer can be a whole number or a half, like 2.0 or 2.5. Do not change nums.
Example 1
nums [3,1,2]
returns 2.0
In order: 1, 2, 3. The middle one is 2.
Example 2
nums [4,1,3,2]
returns 2.5
In order: 1, 2, 3, 4. The two middle ones are 2 and 3, whose average is 2.5.
Constraints
1 <= len(nums) <= 100-1000 <= nums[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Sorting and searching problems
- Names in OrderBeginner
- Shortest Words FirstBeginner
- Sort by Last DigitBeginner
- Strictly Increasing CheckBeginner
- Top Three ValuesBeginner
- Where It LandsBeginner
- Widest Gap After SortingBeginner
- Binary SearchEasy