Coding Quiz - Sorting a List
In this coding quiz, you are to write a function that sorts a given list using Python's sort()
function.
The program receives a list of numbers from the user and sorts it in either ascending
or descending
order, then prints the sorted list.
def solution(numbers, is_ascending): return # Write your code here
The function takes the following two parameters:
-
numbers
: The list of numbers to be sorted -
is_ascending
: A boolean value indicating whether to sort in ascending order -True/False
or0/1
Constraints
-
The list provided as the first argument must contain integers.
-
The sorting order provided as the second argument should allow for user input in the form of
True
,False
,0
, or1
.
Example Input/Output
Example 1
-
Input: solution([3, 1, 4, 1, 5, 9, 2], True)
-
Output:
[1, 1, 2, 3, 4, 5, 9]
Example 2
-
Input: solution([3, 1, 4, 1, 5, 9, 2], False)
-
Output:
[9, 5, 4, 3, 2, 1, 1]
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help