#๐Ÿ”’ Can someone help me figure out why my algorithm isn't working?

91 messages ยท Page 1 of 1 (latest)

wise dagger
#

The objective of the algorithm is to sort a list scores data in accending, showing its work as it sorts one by one and then prints it out in decending order.

scores = [88,92, 79, 85, 90]
accending_order = []
index = 0

for x in scores:
    if len(accending_order) == 0:
        accending_order.append(x)
        print(accending_order)
        
    else:
        for y in range(len(accending_order)):
            y = accending_order[y]
            if x == y:
                continue
            
            elif x>y:
                accending_order.append(x)
                print(accending_order)
                break
            
            elif x<y:
                index = accending_order.index(y)
                accending_order.insert(index, x)
                print(accending_order)
                break


its outputing this

[88]
[88, 92]
[79, 88, 92]
[79, 88, 92, 85]
[79, 88, 92, 85, 90]
[90, 85, 92, 88, 79]
copper tangleBOT
#

@wise dagger

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wise dagger
#

I am aware you can use the sort function but i took it upon my self to do it without one... ๐Ÿคท I love making stuff more difficult for me

ornate hornet
#
[88]
[88, 92]
[79, 88, 92]
[79, 88, 92, 85]
[79, 88, 92, 85, 90]

works fine for me

#

wait nvm

wise dagger
#

yea i thought so too at first

#

then i saw the 85 and my day was ruined

opal flame
#

I can't really look at your specific code atm however I will say that you seem to be making a insertion / selection sort (pretty much the same thing but ascending / descending). It might be worth looking at how they work.

ornate hornet
#

it seems that ur only checking if x is greater then the first number inaccending_order

#

this way of sorting is way more complicated than it needs to be, the simplest sort you can try and take a look at is bubble sort

wise dagger
ornate hornet
#

this part,

for y in range(len(accending_order)):
            y = accending_order[y]
            print(accending_order, x, y)
            if x == y:
                continue
            
            elif x>y:
                accending_order.append(x)
                print(accending_order)
                break
#

ur checking if x is bigger than the first number in accending_order, and if it is, append xto the last position in accending_order

wise dagger
#

But doesn't the for loop just go through one number and then the next and next and so on?

#

i thought that's what for loops did

#

goes to one data is a list then the next and the next and so on

#

or rather from 0 to the next no and the next no and so on

ornate hornet
#

yes, but when a new x iteration starts, it compares the value of x to the first number in accending_order, if x is bigger then it will append it to the last position of accending_order

wise dagger
#

Ohh i hadn't thought of that

#

damn that means a whole 2 hours down the drain

ornate hornet
#

it wasnt for nothing since you learned something

#

nothing is ever down the drain

wise dagger
#

True..

ornate hornet
#

do you know any sorting algorithms?

wise dagger
#

Not really

#

No i dont think so

#

I've watched a few shorts on youtube about bubble sort and some other weird sorting algoriths like stalin sort and stuff

#

but nothing really serious

ornate hornet
#

i can give you a breakdown of one of the simpler ones if youd like, and you could try to recreate it

wise dagger
#

For sure

halcyon umbra
#

imo the simplest sorting algorithm to understand is selection sort

ornate hornet
#

it all depends from person to person, but bubble sort is an easy gateway into algorithms i feel like

ornate hornet
# wise dagger For sure

you basically check if the first number is bigger than the second number, and if it is then you swap them

#

then check if the second number is bigger than the third number and so on

opal flame
#

Selection sort and insertion sort are basically the same thing but different orders. You've made a slightly worse one of those. Bubble sort is good educational value.

wise dagger
opal flame
#

There's some GeeksForGeeks website that explain algorithms and data structures quite well.

halcyon umbra
#

I feel like I have more luck explaining selection sort tbh

  1. find the minimum value of the entire array
  2. swap it with the first element
  3. find the minimum value from the rest of the array
  4. swap it with the second element
    ...
    repeat until the entire array is sorted
wise dagger
halcyon umbra
wise dagger
wise dagger
#

Which one takes less lines though?

#

I've got an exam tommorow(written) and line count is extremly important

#

theres obviously sort() but i dont think we've been taught that in class

halcyon umbra
ornate hornet
#

you usually measure algorithms in big O notation

halcyon umbra
wise dagger
ornate hornet
#

time usage != amount of lines

wise dagger
#

Give me a large as possible buffer for SQL

wise dagger
opal flame
#

If they don't ask you to use a specific sorting algorithm then I don't see why using .sort() is a problem unless they specify.

wise dagger
#

.sort() hasn't been taught it class

#

i got deducted a few marks last exam coz i used f strings

ornate hornet
#

the test isnt specifically about sorting algorithms tho is it?

opal flame
#

Whatever education system you're apart of sucks then

wise dagger
#

No it general pythong and SQL

wise dagger
ornate hornet
#

have you been told not to use any built-in sorting algorithms?

wise dagger
#

And in a 17.5 marks exam even 1 mark matters a ton

opal flame
#

Ignoring the incompetent education system behind. Bubble sort is your homie for fewest lines.

wise dagger
halcyon umbra
#

ig bubble might take the shortest if you're only allowed basic syntax

a = [ ... ] # some unsorted list
for _ in range(len(a)):
  for i in range(len(a)-1):
    if a[i] > a[i+1]:
      a[i], a[i+1] = a[i+1], a[i]
wise dagger
#

wait a-1?

halcyon umbra
wise dagger
#

ahh no worries

#

yea this makes sense in my head

#

think i can recall this in the exam

ornate hornet
#

make sure to understand the concept of bubble sort, once you understand it you can write it out pretty quick

wise dagger
#

hopefully

wise dagger
#

it makes imagining the concept a bit easier

ornate hornet
#

do you know what pseudo code is?

wise dagger
#

Yea.. but i aint really good at understanding it

#

python is almost identical and easier to understand

ornate hornet
#

you pretty much write out what you want your program to do in a real world language before writing any code, this helps alot wit writing algorithms

wise dagger
ornate hornet
#

e.g

loop i in list
  loop j in list
    if i bigger than j
      swap i and j
wise dagger
#

ahh sounds like a me issue then..

halcyon umbra
wise dagger
#

thanks to everyone for the help.. really apreciate it..

#

!close

copper tangleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.