Forums

Python - Bubble Sort - ascii average

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 )

  1. 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.

what do they mean by ascii value? could it be this?

Yea conrad. The average ascii value of string in a list, sorting it that way.

If you have a function that can determine the average ascii value, then you can just call that and use the result in your sort - in the same way that you're calling len in the part where you're sorting by length.

to glenn, I cant, the len fucntion gives me the length, it doesnt sort it. I changed the sort function for it.

about function of average ascii value, I dont think there is... But anyway, I managed to create the full code, but its 95% completed. I turned the list into a list of ascii values, did the sum, average, I sorted the average ascii list, but I dont know how to change it back to list... look at the code example:

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()  # Global list as variable words
print(f"List not sorted: {words}")

new_words = copy.deepcopy(words)  # List changes during time, new variable.


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}")


def square_brackets():
    asciis = []
    for word in new_words:
        super_ascii = []
        for ch in word:
            super_ascii.append(ord(ch))
        asciis.append(super_ascii)
    return asciis


squared = square_brackets()
print(f"List unsorted by ascii value: {squared}")


def average_ascii_value():
    new_ascii = []
    for word in new_words:
        super_ascii = []
        for ch in word:
            super_ascii.append(ord(ch))
        new_ascii.append(sum(super_ascii) // len(word))
    return new_ascii


averaged = average_ascii_value()
print(f"List unsorted by ascii average value: {averaged}")


def sorting_by_average_ascii_value(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


average_sorted = sorting_by_average_ascii_value(averaged)
print(f"List sorted by average ascii value: {average_sorted}")

Output for example:

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 alphabet: ['dear', 'hello', 'my']
List sorted by length: ['my', 'dear', 'hello']
List unsorted by ascii value: [[104, 101, 108, 108, 111], [109, 121], [100, 101, 97, 114]]
List unsorted by ascii average value: [106, 115, 103]
List sorted by average ascii value: [103, 106, 115]

Process finished with exit code 0

the unsorted by ascii value and average one, are useless, I know, I did it only for me to understand what I am writing. I know the code is long, I can make it smaller afterwards, first I want to complete the code, I want to finish the question, afterwards I will make it shorter by alot... Is there anyway to turn back the average ascii value - the sorted one, back to strings? if yes please help me with it, afterwards, I am finished with the question.

I can't think of any way to do that -- if you think of strings as just being lists of numbers (where all of the numbers are less than 256) then you can't go from an average back to the string, any more than you can tell whether the average 4 originally came from the list [4] or the list [2, 6].