hacker.org Forum Index
RegisterSearchFAQMemberlistUsergroupsLog in
'Valuation' in Python error

 
Reply to topic    hacker.org Forum Index » Challenges View previous topic
View next topic
'Valuation' in Python error
Author Message
spike21333



Joined: 20 Mar 2010
Posts: 1

Post 'Valuation' in Python error Reply with quote
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
Tue Apr 06, 2010 12:44 am View user's profile Send private message
Karian



Joined: 09 Jan 2008
Posts: 75

Post Reply with quote
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.
Tue Apr 06, 2010 8:51 am View user's profile Send private message
Raeith



Joined: 15 May 2009
Posts: 1

Post Problem Reply with quote
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.
Sun Apr 18, 2010 8:58 am View user's profile Send private message
Karian



Joined: 09 Jan 2008
Posts: 75

Post Re: Problem Reply with quote
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
Mon Apr 19, 2010 1:22 pm View user's profile Send private message
Masti6



Joined: 15 May 2010
Posts: 55
Location: Finland, Nurmes

Post Reply with quote
Quote:
I tried to modify spike21333's code with following results:

Code:
#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.
Mon May 17, 2010 4:43 pm View user's profile Send private message MSN Messenger
Karian



Joined: 09 Jan 2008
Posts: 75

Post Reply with quote
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.

Wed May 19, 2010 3:21 pm View user's profile Send private message
Display posts from previous:    
Reply to topic    hacker.org Forum Index » Challenges All times are GMT
Page 1 of 1

 
Jump to: 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Flowers Online.