Question: Create a file named sorting.py that provides two functions: swap and sort _ short. swap The swap function must take three arguments: a list, and

Create a file named sorting.py that provides two functions: swap and sort_short.
swap
The swap function must take three arguments: a list, and two integers representing indices into the list. It must then swap the the list elements stored at the provided indices. It should not have any return value.
Here is an example of the expected behavior:
>>> items =["a","b","c"]
>>> swap(items,0,2)
>>> print(items)
['c','b','a']
sort_short
The sort_short function must take a single list argument containing numeric values. The list will have no more than three items. This function must rearrange the items in the list so that they appear in increasing order. Your implementation should call the swap method as needed to move items within the list. For example, if we knew that the list would have exactly two items, the following implementation would be correct:
def sort_short(items):
if items[0]> items[1]:
swap(items,0,1)
There is no return value. You may assume that the list will not contain duplicates.
Here is an example illustrating how we can use the assert statement to check for the correct behavior:
if __name__=="__main__":
items =[1]
sort_short(items)
assert items ==[1]
items =[2,1]
sort_short(items)
assert items ==[1,2]
items =[3,2,1]
sort_short(items)
assert items ==[1,2,3]
The assert statement provides a built-in mechanism for confirming that our code is working as expected. If the expression after assert evaluates to True, then nothing happens, if it evaluates to False, then there will be an AssertionError indicating that there is something wrong with our implementation (or with our test).
Your solution must not use any loops, and it must not use any built-in sorting functions.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!