In: Computer Science
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python.
One optional way to test your function is to create a list of random words by using the RandomList function in PythonLabs and passing 'words' as an argument. For example:
>>> from PythonLabs.RecursionLab import *
>>> a = RandomList(20, 'words')
>>> scrabble_sort(a)
>>> print(a)
['mum', 'gawk', 'tree', 'forgo', 'caring', ... 'unquestioned']
Note: you do not need to include the above test case in your solution, but be sure to include at least 2 test cases to verify your scrabble_sort function works.
In PyCharm
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== def scrabble_sort(words): for i in range(len(words)): for j in range(0, len(words) - i - 1): if len(words[j]) == len(words[j + 1]): if words[j] > words[j + 1]: words[j], words[j + 1] = words[j + 1], words[j] elif len(words[j]) > len(words[j + 1]): words[j], words[j + 1] = words[j + 1], words[j] words = ['mum', 'gawk', 'tree', 'forgo', 'caring', 'tall', 'bat', 'unquestioned', 'ant','the','fox'] print('Before sorting') print(words) scrabble_sort(words) print('After sorting') print(words)
================================================================