Python Forum
Sorting by average ascii value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting by average ascii value
#1
Hey, I have a question in which I need to sort some stuff without using sort/sorted.
I need to do:
1.Write a code which receives words in a list till it get "stop".
2. Afterwards, print the list ( not included stop )
3. Afterwards, sort it lexicography and print again.
4. Afterwards, sort it by length and print again.
5. Lastly, sort it by average ascii value and print again.

I did 1, 2, 3, 4
only 5 is a little problematic.
My code ( Till the average ascii value, it works good, on that it crashes, ill write the error ):
def word_amount_to_list():
    empty_list = []
    while True:
        phrase = input("Please enter your word: ")
        empty_list.append(phrase)
        if phrase == "stop":
            empty_list.remove("stop")
            break
    return empty_list


word = word_amount_to_list()
print(f"List not sorted: {word}")


def lexicography_list():
    x = len(word)
    for i in range(x-1, -1, -1):
        for j in range(i):
            if word[j] > word[j + 1]:
                word[j], word[j + 1] = word[j + 1], word[j]
    return word


lex_list = lexicography_list()
print(f"List sorted by alphabet: {lex_list}")


def sorting_by_length(lst):
    x = len(lst)
    for i in range(x - 1, -1, -1):
        for j in range(i):
            if len(word[j]) > len(word[j + 1]):
                word[j], word[j + 1] = word[j + 1], word[j]
    return word


length_list = sorting_by_length(word)
print(f"List sorted by length: {length_list}")
Till this it works.
Now it wont work:
def sorting_by_ascii_average(lst):
    x = len(word)
    for i in range(x - 1, -1, -1):
        for j in range(i):
            if (ord(word[j])) > (ord(word[j + 1])):
                word[j], word[j + 1] = word[j + 1], word[j]
    return word


average_ascii = sorting_by_ascii_average(word)
print(f"List sorted by average ascii: {average_ascii}")
output: ( If I do single letter, which works: )
Please enter your word: a
Please enter your word: A
Please enter your word: K
Please enter your word: stop
List not sorted: ['a', 'A', 'K']
List sorted by alphabet: ['A', 'K', 'a']
List sorted by length: ['A', 'K', 'a']
List sorted by average ascii: ['A', 'K', 'a']

Process finished with exit code 0
output: (if I dont do single letters - not working the ascii )
Please enter your word: hello
Please enter your word: my
Please enter your word: dear
Please enter your word: blabla
Please enter your word: stop
List not sorted: ['hello', 'my', 'dear', 'blabla']
List sorted by alphabet: ['blabla', 'dear', 'hello', 'my']
List sorted by length: ['my', 'dear', 'hello', 'blabla']
Traceback (most recent call last):
  File "C:\Users\bensh\NewSapirCourse\Sapir_Lesson4\Sorting\Sort-5.py", line 25, in <module>
    average_ascii = sorting_by_ascii_average(word)
  File "C:\Users\bensh\NewSapirCourse\Sapir_Lesson4\Sorting\Sort-5.py", line 20, in sorting_by_ascii_average
    if (ord(word[j])) > (ord(word[j + 1])):
TypeError: ord() expected a character, but string of length 5 found

Process finished with exit code 1
It works only if I put in a list single letters.
I thought about adding sum to the ords, wont work also.
Tried maybe with ord(word[i][j]) and to the other one also, but didnt work either.
I Thought in order to solve this, I need 2 definitions:
1. gets the list and count the ascii value of each letter and then sums it.
2. sort it afterwards

problem is, I dont know how to do that lol.... the ascii are the one i am most weak at... I never use it, but I have to use them in the question...

I will be happy to get some tip in order to answer it, not an answer, just a tip, which function to use, 2 definitions? if yes, what to do on each and such...
Reply


Messages In This Thread
Sorting by average ascii value - by ben1122 - Sep-07-2021, 06:33 PM
RE: Sorting by average ascii value - by deanhystad - Sep-07-2021, 07:26 PM
RE: Sorting by average ascii value - by ben1122 - Sep-07-2021, 07:42 PM
RE: Sorting by average ascii value - by ben1122 - Sep-07-2021, 08:05 PM
RE: Sorting by average ascii value - by deanhystad - Sep-07-2021, 08:19 PM
RE: Sorting by average ascii value - by ben1122 - Sep-08-2021, 11:46 AM
RE: Sorting by average ascii value - by deanhystad - Sep-07-2021, 08:42 PM
RE: Sorting by average ascii value - by deanhystad - Sep-08-2021, 04:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,222 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020