import copy
def words_to_list():
----growing_list = [] # Creating an empty list.
----while True: # Continue till string: "stop".
--------enter_word = input("Please enter your word: ")
--------growing_list.append(enter_word) # Adding the words to the list in each iteration.
---------if enter_word == "stop":
-------------growing_list.remove("stop")
-------------break # Finish function.
----return growing_list
words = words_to_list()
print(f"List not sorted: {words}")
new_words = copy.deepcopy(words) # List changes during time, new variable.
def bubble_sort(lst, key):
----for i in range(len(lst) - 1, - 1, - 1):
---------for j in range(i):
-------------if key(lst[j]) > key(lst[j + 1]):
------------------lst[j], lst[j + 1] = lst[j + 1], lst[j]
----return lst
print(f"List sorted by length: {bubble_sort(words, key=len)}")
print(f"List sorted by lexicography: {bubble_sort(words, key=str.lower)}")
The code I did, to match the lst and key as you said. ( its good yea? ) its the one I was told before also with lst, key, I guess as you say its allowed to use it.
The output:
Please enter your word: hello
Please enter your word: my
Please enter your word: dear
Please enter your word: stop
List not sorted: ['hello', 'my', 'dear']
List sorted by length: ['my', 'dear', 'hello']
List sorted by lexicography: ['dear', 'hello', 'my']