Page 1 of 1

'Valuation' in Python error

Posted: Tue Apr 06, 2010 12:44 am
by spike21333
I'm trying to solve the 'Valuation' challenge using Python, but i keep getting the answer to be 26.
I'm sure there is an obvious error in my logic, but i can't seem to figure it out. Here is my code....
------------------------------------------------------------------------------------------------------------------
mainvalue = [9,3,7,5,2,"x","x","x",7,4,6,etc...](I'm not putting the whole value here, that would be rediculous)
countervalue = 0
lenvalue = len(mainvalue)
total = 0
while countervalue < lenvalue:
if mainvalue[countervalue] == "x":
mainvalue.insert(countervalue, 0)
countervaule = countervalue - 2
else:
total = total + mainvalue[countervalue]
countervalue = countervalue + 1
if countervalue == lenvalue:
print total
------------------------------------------------------------------------------------------------------------------
I greatly appriciate any help, btw my indentation is correct it didnt copy right

Posted: Tue Apr 06, 2010 8:51 am
by Karian
It seems a bit of overkill to format the string yourself as you have done now. At the same time, you can probably solve this by hand, but anyway, as remark on your code. I would suggest you start the program with the input as far as you have displayed it over here, and print out your list (or use another way for inspecting the list, I don't know if there are debuggers for Python) every time you have performed the loop and check if this really looks like what you are expecting. My feeling is your code isn't doing what you expect it to do.

Problem

Posted: Sun Apr 18, 2010 8:58 am
by Raeith
Its the way your treating "x" values, remember the challenge REMOVES them and then subtracts how far back you move the counter... Your method won't remove them and as such, it does affect what happens when you move 2 steps back. Hope that helps, should make whats wrong pretty clear.

Edit: Also insert doesn't replace the value in python, it inserts at that location and shifts everything else across, you're turning [...,"x",...] into a [...,0,"x",...] I think... but shouldn't you be getting 35 with that buggy code? Oh, right you'll want to check for a typo in that code too.

Re: Problem

Posted: Mon Apr 19, 2010 1:22 pm
by Karian
Raeith wrote:Its the way your treating "x" values, remember the challenge REMOVES them and then subtracts how far back you move the counter... Your method won't remove them and as such, it does affect what happens when you move 2 steps back. Hope that helps, should make whats wrong pretty clear.

Edit: Also insert doesn't replace the value in python, it inserts at that location and shifts everything else across, you're turning [...,"x",...] into a [...,0,"x",...] I think... but shouldn't you be getting 35 with that buggy code? Oh, right you'll want to check for a typo in that code too.
those things I was trying to let him find that on it's own. A lot of the problems people ask is to learn them how to debug there programs. It is no use giving things away, since next time, they will probably make the same mistake again in another challenge. If they debug it, they can at least learn from it

Posted: Mon May 17, 2010 4:43 pm
by Masti6
I tried to modify spike21333's code with following results:

Code: Select all

#The list:
main = [9,3,7,5,2,'x','x','x',7,4,6,'x',2,7,'x',1,7,5,4,'x','x',9,0,'x',9,3,'x','x','x','x','x',\
             2,3,8,'x',4,4,'x',7,5,'x','x',0,8,7,5,0,9,1,2,7,3,8,'x',8,4,6,1,'x',8,7,5,9,\
             3,8,3,'x','x',3,2,8,'x',4,'x',4,9,3,5,9,0,3,'x',6,'x',5,5,5,0,3,6,0,5,3,5,0,0,\
             4,'x',0,'x','x',9,4,5,9,5,8,9,6,1,2,9,6,'x',2,6,7,'x',8,8,4,2,'x','x','x',5,'x',6,'x','x',\
             6,1,'x',4,'x',4,8,4,8,2,'x',8,0,'x','x','x',8,3,3,1,6,8,4,3,'x',7,'x',4,'x',8,3,'x',9,5,2,\
             1,7,3,1,'x','x','x',2,5,'x',5,1,'x','x',4,5,7,'x',6,'x',5,'x',9,6,9,8,2,2,2,'x',7,7,1,2,3,7,\
             7,4,5,0,3,4,'x',5,1,3,3,5,9,2,'x',2,7,'x','x',8,'x',8,7,'x','x',3,5,2,2,1,'x',3,6,'x',0,'x',5,\
             0,'x',2,3,'x',7,'x',6,3,'x',9,9,8,4,1,8,'x','x']
#Some variables
counter = 0
lenvalue = len(main)
total = 0
#While loop with if-else
while counter < lenvalue:
    #At the following row it gives an error:
    #ValueError: list.remove(x): x not in list
    #for unknown reason ^ "Yes, the 'x' is there dammit!
    if main[counter] == 'x':
        #Deleting the 'x' from list
        main.remove(counter)
        #I'm not sure if this is right. Shouldn't I get the cursor move
        #2 letters back, not substract 2 from the total?
        counter = counter - 2
    #This is what we do when the conditions aren't met
    else:
        total = total + main[counter]
        counter = counter + 1
#And when counter finally reaches lenvalue, we print the total out
if counter == lenvalue:
    print total
That is the code, it doesn't recognize 'x' and I'm not sure if it's working in any else way either.
Any feedback is appreciated.

Posted: Wed May 19, 2010 3:21 pm
by Karian
Masti6 wrote:Any feedback is appreciated.
Karian wrote:I would suggest you start the program with the input as far as you have displayed it over here, and print out your list (or use another way for inspecting the list, I don't know if there are debuggers for Python) every time you have performed the loop and check if this really looks like what you are expecting. My feeling is your code isn't doing what you expect it to do.