Hi, I have a question that I need to sort lists with: lexicography, length and lastly the hardest - ascii average. And when it reaches stop, the lists ( unsorted ) ends. Did all of it, execpt the ascii average, there I got stuck.
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}")
def sorting_by_lexicography(lst):
for i in range(len(lst) - 1, -1, -1):
for j in range(i):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
return lst
lex_list = sorting_by_lexicography(words)
print(f"List sorted by alphabet: {lex_list}")
def sorting_by_length(lst):
for i in range(len(lst) - 1, -1, -1):
for j in range(i):
if len(lst[j]) > len(lst[j + 1]):
lst[j], lst[j + 1] = lst[j + 1], lst[j]
return lst
length_list = sorting_by_length(words)
print(f"List sorted by length: {length_list}")
Output:
List not sorted: ['hello', 'my', 'dear', 'sus', 'stp']
List sorted by alphabet: ['dear', 'hello', 'my', 'stp', 'sus']
List sorted by length: ['my', 'stp', 'sus', 'dear', 'hello']
I got a few ideas about it, but could not code it... its really advanced and I am a beginner sadly... I tried a few stuff, all of em I did with a new window at pycharm and a list prepared from before without receiveing: 1.
words = ["hello", "my", "dear", "properly"]
def list_of_letter_ascii_values(lst):
word = ", ".join(words)
listed = []
for i in word:
listed.append(ord(i))
return listed
x = list_of_letter_ascii_values(words)
print(x)
def average_ascii_values(lst):
total = 0
for i in x:
total += i
average = total / len(x)
return total, average
y, z = average_ascii_values(x)
print(y, z)
Output:
[104, 101, 108, 108, 111, 44, 32, 109, 121, 44, 32, 100, 101, 97, 114, 44, 32, 112, 114, 111, 112, 101, 114, 108, 121]
2295 91.8
Here I thought I can try to turn the list into intergers and then separate it somehow, got stuck and given up. ( if anyone got any ideas how to continue this, I will be happy to know )
- I deleted all the rest, just noticed...
Anyway, can anyone give me any tip on how to answer the ascii? not an answer, just a tip, what to do, which function, from there I can sort it out I think.