#My brain being dumb, scroll on

1 messages ยท Page 1 of 1 (latest)

misty trench
#
def InsertionSort(l):
    for a in range(1, len(l)):
        b = a
        while (b > 0) and (l[b-1] > l[b]):
            l[a], l[b] = l[b], [a]
            b -= 1
    return l

print(InsertionSort([9, 8, 3, 5, 4, 6, 0, 1, 2]))

Exception has occurred: TypeError
'>' not supported between instances of 'list' and 'int'
  File "pp.py", line 13, in InsertionSort
    while (b > 0) and (l[b-1] > l[b]):
  File "pp.py", line 18, in <module>
    print(InsertionSort([9, 8, 3, 5, 4, 6, 0, 1, 2]))
TypeError: '>' not supported between instances of 'list' and 'int'

@amber topaz
@echo zodiac

echo zodiac
#

what's l?

misty trench
#

list

echo zodiac
#

i mean
The list you're giving it

misty trench
#

the function parameter

echo zodiac
#

Ah wait I'm blind

misty trench
#

i see

#

@misty trench

#

im blind wtf is wrong with the code

amber topaz
#

i think i found a fix

#

give a sec

#

@misty trench done

misty trench
#

enligthen me

amber topaz
#

i converted it to an int

#

and also theres a slight issue in the next line

#
def InsertionSort(l):
    for a in range(1, len(l)):
        b = a
        while (b > 0) and (int(l[b-1]) > l[b]):
            l[b], l[b-1] = l[b-1], l[b]
            b -= 1
    return l

print(InsertionSort([9, 8, 3, 5, 4, 6, 0, 1, 2]))
#

this should be it

misty trench
#

wait wot

amber topaz
#

kekw

misty trench
#

why does l[b-1] return a list

amber topaz
#

i dont know

misty trench
#

hein

amber topaz
#

but i dont think thats all the problem contains

#

there is a fundamental error on line 5

#

you wanna swap l[b] and l[b-1]

#

but you are swapping it with l[a]

#

check your code

#
l[a], l[b] = l[b], [a]
#

this is what you did

#
l[b], l[b-1] = l[b-1], l[b]

this is what you should do

misty trench
#

oh yeah right

amber topaz
#

works fine kekw

misty trench
#

definitely that ๐Ÿ™‚ you're assigning [a] to l[b] ... you don't want that.

#

that's putting a list with one item inside a list of ints.

amber topaz
#

ye

misty trench
#

ellie called it right ๐Ÿ™‚

#

ok so how does this

#

coz this

#

@amber topaz nicely done.

#

@misty trench split that statement

amber topaz
#

i thought you had been doing this for a while phantom Kappa

misty trench
#

you're assigning two variables in one go, make it into two assignments and tell me what you see

misty trench
#

but that is fine

#

your code doesn't do that.

#

ik ik

#

this is why I rarely do dual assignment in one line ๐Ÿ™‚

amber topaz
misty trench
#

human performance is an issue

#

the weird thing is when this weird shit happens both a and b are the same

#

shouldnt there be.. no change?

#

pretty sure the swap code is behaving exactly like you told it to. you just haven't run through the code step by step to see why.

#

i did that

misty trench
#

i dont understand how swapping two elements in a list create an entirely new list inside the former list

#

break it down into two assignments, then step over the code line by line with a == b

#

you're not swapping two elements.

#

that's what you wanted to do

#

but the code you wrote is very much not doing that.

#

but... a and b are still integers... even if the integers are wrong it should still swap right... just the wrong elements

#

ps: I'm not trying to make you feel bad, just trying to get you to see the issue for yourself. ๐Ÿ™‚

misty trench
#

l[a], l[b] = l[b], [a]

take a moment, breathe in, then write to me in english what that code does please.

#

walk me step by step ๐Ÿ™‚

#

uhhh.. take the element at pos a in list l, and set its value to the value of element at pos b in list l and vice versa?

#

tell you want since you didn't do it .. i'll do it for you... this is what that code looks like in two statements:
l[a] = l[b]
l[b] = [a]

#

lets make it easier to read

#

list[a] = list[b]
list[b] = [a]

#

you descrbied the first line

#

tell me what the second one does

#

yeah thatll just set both list[a] and list[b] to the value of list[b]

#

i see whats wrong here

#

that's not quite right.

#

how should that code look like ?

#

uhm

misty trench
#

the second line

#

oh what thatll just set the value of b'th element of the list to the value of a

#

not the value list[a] holds

#

but the value of a itself

#

yep.

#

now you get it. ๐Ÿ™‚

#

mhm

misty trench
#

the first part of that assignment is correct. the second one is not swapping anything and is adding a new literal list with the value of a as it's single item and putting that in your list.

#

wait, SON OF A BITCH I THOUGHT IVE WRITTEN l[a]

#

if a and b were 1 and 2 your code would read:
list[1] = list[2]
list[2] = [1] <--- that's you inserting a list into a list.

#

told u im blind

#

this is why:

  • be careful abbreviating variable names. it makes it easier for your brain to see what it wants to see.
  • when code doesn't work, use prints to log the decisions and the data so you can see what is happening - log the decision being made, the data used to make that decision, and also log the data you were expecting if there was any.
#

i.. i- should name things better
sorry for wasting ur time
thx for the help