On 9/20/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> "bhaaluu" <[EMAIL PROTECTED]> wrote
>
> > of it. But the search worked even when stackB
> > and stackC were empty. Interesting.
>
> You never checked stackB or stackC when they were
> empty because it always found something in a prior
> stack first.
>
> > I could see, all three conditionals in popNum(num)
> > were the same, just checking different lists.
>
> They were, and all had the same problem,
> you only hit it on stackA.
>
> If you had started your sequence with 1c instead
> of 1b and then tried 1b you would have hit the problem
> on the second move.

That's right! =) After Michael suggested a fix, I went back
and started the game with the list at stackC filled, and
worked from C to A and the same thing happened.

So Michael's fix worked fine for *that* problem.
Now I'm working on checking if a move is "legal" or not.
Rules: Only one number may be moved at a time.
A larger number may not be put on top of a smaller number.

Tonight, I'm giving a presentation ("Introduction to Python")
to my local Linux Users Group (LUG). I will be telling everyone
about the great Tutors on this fantastic mailing list. This list
is surely one of the best 'features' about Python.  =)

>
> Alan G.
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Anyway, even though I was somewhat braindead by the end of the day...
Here is the current iteration of the funky funcStacks.py proggie:
It still needs a lot of work, but it is a FUN way to learn stuff.

# funcStacks.py
# passing parameters, returning values, pushing and popping
# 2007-09-19
# b h a a l u u at g m a i l dot c o m
######################################
"""
Pseudocode
 1. Get the number to move and the column to move it to: 1b, 2c, etc.
 2. Find the column the number is in: A, B, or C
 3. Is it the last number (ie. can it be popped?) num == stackA[-1]
 4.   If the number can be popped, pop it!        stackA.pop()
 5.   Else, number is buried: return an error and try again.
 6. Determine if the number can be legally moved to the column requested.
 7.   If the column is empty, move the number:    stackB.append(num)
 8.   If the column has a number, is it larger?   stackB.append(num)
 9.   Else if number in column is smaller,  return an error and try again.
10. Loop.

Test game 1:
1b 2c 1c 3b 1a 2b 1b 4c 1c 2a 1a 3c 1b 2c 1c 5b
1b 2a 1a 3b 1c 2b 1b 4a 1a 2c 1c 3a 1b 2a 1a 5c
1b 2c 1c 3b 1a 2b 1b 4c 1c 2a 1a 3c 1b 2c 1c 0

Test game 2:
1c 2b 1b 3c 1a 2c 1c 4b 1b 2a 1a 3b 1c 2b 1b 5c
1a 2c 1c 3a 1b 2a 1a 4c 1c 2b 1b 3c 1a 2c 1c 0
"""
num = 0
abc = ""
EMPTY = []
stackA = []
stackB = []
stackC = []

def isEmpty(stack):
    if stack == EMPTY:
        return True
    else:
        return False

def isLegal(num,abc):
    if abc == 'C':
        if stackC == EMPTY or num < stackC[-1]:
            return True
        else:
            return False
    elif abc == 'B':
        if stackB == EMPTY or num < stackB[-1]:
            return True
        else:
            return False
    elif abc == 'A':
        if stackA == EMPTY or num < stackA[-1]:
            return True
        else:
            return False

def popNum(num):
    if len(stackA) > 0 and num == stackA[-1]:
        stackA.pop()
    elif len(stackB) > 0 and num == stackB[-1]:
        stackB.pop()
    elif len (stackC) > 0 and num == stackC[-1]:
        stackC.pop()
    else:
        return "Error: number not available."

def pushNum(num,abc):
    if abc == 'C' and isLegal(num,abc):
        stackC.append(num)
    elif abc == 'B' and isLegal(num,abc):
        stackB.append(num)
    elif abc == 'A' and isLegal(num,abc):
        stackA.append(num)
    else:
        return 'Error'

def moveNum(num,abc):
    popNum(num)
    pushNum(num,abc)
    return stack_status()

def stack_status():
    print "A",stackA, isEmpty(stackA)
    print "B",stackB, isEmpty(stackB)
    print "C",stackC, isEmpty(stackC)
    return

# main
print '='*25
stackA=[5,4,3,2,1]
stack_status()
print '='*25

myMove = raw_input('Enter number and stack to move it to: ')
while myMove != '0':
    num = int(myMove[0])
    abc = str.upper(myMove[1])
    print num, abc, isLegal(num,abc)
    moveNum(num,abc)
    print '='*25
    myMove = raw_input('Enter number and stack to move it to: ')
# end funcStacks.py

Happy Programming!
-- 
bhaaluu at gmail dot com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to