Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › python [testing help]
New Posts  All Forums:Forum Nav:

python [testing help]

post #1 of 6
Thread Starter 
Okay need some help biggrin.gif

so this is the situation. you have a deck fo cards in your hand which is represented as a list. You place it down in a list, then you compare it to the card left of it one spot. If the suit or the number match, you add it on top of that card. Same thing for card 3 to the left, if it matches suit or number, add on top of it. otherwise, you just leave it and draw another card. I worked up some code/pseudo code so far and i was wondering what you guys think in means of it solving my problem?

input would look like
meaning queen of spades and 3 of clubs and so forth
Code:
def play(list):
        
        for k in list:
                a = 0
                #insert into the pile
                pile[a] = list[k]
                
                #check if the new card in the pile is compatible to the card to the left 

                #check in terms if the first char of each card is compatible 
                if( ((pile[a])[0]) = ((pile[a-1])[0]) ):
                        #add card to the top of the card to the left
                        #and increase a and move on

                #check in terms if the last char of each card is compatible 
                else if(((pile[a])[(len((pile[a])))-1]) = ((pile[a-1])[(len((pile[a-1])))-1]) ):
                        #add card to the top of the card to the left
                        #and increase a and move on
                
                #check if the new card in the pile is compatible to the card 3 to the left

                #check in terms if the first char of each card is compatible 
                else if( ((pile[a])[0]) = ((pile[a-3])[0]) ):
                        #add card to the top of the card 3 to left
                        #and increase a and move on

                #check in terms if the last char of each card is compatible 
                else if(((pile[a])[(len((pile[a])))-1]) = ((pile[a-3])[(len((pile[a-3])))-1]) ):
                        #add card to the top of the card 3 to left
                        #and increase a and move on

                else:
                        #increase a and move on

#move on means the next card from your hand will be placed
I guess i just want to know if the setup looks good and im not that sure about all the else if's, but
thanks for the help guys, and i know the code for the if's worked because i tested that much.

cheers!
Edited by protzman - 11/7/12 at 5:57pm
post #2 of 6
Thread Starter 
bump with new op!
post #3 of 6
This type of problem would be done best with recursion, I think.

Once you've spread out the cards (put them in a list), recursively call the function that compares each subsequent card and compares with the card currently on top of the deck you are making to the extreme right and either put it on top of the card, or skip on to the next function.

I couldn't be bothered to read your code redface.gif because the extensive commenting hurts my eyes. wink.gif Seriously, don't go overboard with the comments.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #4 of 6
Thread Starter 
im not haha that was just pseudo code and im alot further now! thanks for the reply tho thumb.gif
post #5 of 6
Thread Starter 
Hey guys Please need some last bit of help:

I think i wrote my method correctly, i dont really have any way to test it, tried to instal pydev in eclipse but it doesnt seem to work, nor do i really know how to test it any ways. If i could get some guidance? Oh and i tested all the little bits of code in a python shell so i know they should work. but idk how to execute the whole program!

here is my method: Warning: Spoiler! (Click to show)
Code:
def play(list):
    
    for k in list:
        a,x,z = 0
        #insert into the pile
        pile = []
        pile = pile + [[list[k]]]

        #check if the new card in the pile is compatible to the card 3 to the left

        #check in terms if the first char of each card is compatible 
        if( (((pile[a])[(len(pile[a]))-1])[0] ) == ((((pile[a-3])[(len(pile[a-3]))-1])[0])) ):
            #add card to the top of the card 3 to left
            pile[a-3] = pile[a-3] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]
            
        #check in terms if the last char of each card is compatible 
        elif((((pile[a])[(len(pile[a]))-1])[(len((((pile[a])[(len(pile[a]))-1]))))-1]) == (((pile[a-3])[(len(pile[a-3]))-1])[(len((((pile[a-3])[(len(pile[a-3]))-1]))))-1])):
            #add card to the top of the card 3 to left
            pile[a-3] = pile[a-3] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]

        #check if the new card in the pile is compatible to the card to the left 

        #check in terms if the first char of each card is compatible 
        elif((((pile[a])[(len(pile[a]))-1])[0] ) == ((((pile[a-1])[(len(pile[a-1]))-1])[0]))):
            pile[a-1] = pile[a-1] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]

        #check in terms if the last char of each card is compatible 
        elif( (((pile[a])[(len(pile[a]))-1])[(len((((pile[a])[(len(pile[a]))-1]))))-1]) == (((pile[a-1])[(len(pile[a-1]))-1])[(len((((pile[a-1])[(len(pile[a-1]))-1]))))-1])):
            pile[a-1] = pile[a-1] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]

        else:
            a = a + 1  

        #now go through and look for additional moves 
        #(if any match the 1 to the left or 3rd)

        while x < (len(pile)):
            if((((pile[x])[(len(pile[x]))-1])[0] ) == ((((pile[x-3])[(len(pile[x-3]))-1])[0]))):
                pile[x-3] = pile[x-3] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[x])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                    del pile[(len(pile))-1]
                else:
                    del (pile[x])[-1]
                x = x + 1

            elif((((pile[x])[(len(pile[x]))-1])[(len((((pile[x])[(len(pile[x]))-1]))))-1]) == (((pile[x-3])[(len(pile[x-3]))-1])[(len((((pile[x-3])[(len(pile[x-3]))-1]))))-1])):
                pile[x-3] = pile[x-3] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[a])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                else:
                    del (pile[a])[-1]        
                x = x + 1

            elif((((pile[x])[(len(pile[x]))-1])[0] ) == ((((pile[x-1])[(len(pile[x-1]))-1])[0]))):
                pile[x-1] = pile[x-1] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[x])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                    del pile[(len(pile))-1]
                else:
                    del (pile[x])[-1]
                x = x + 1

            elif( (((pile[x])[(len(pile[x]))-1])[(len((((pile[x])[(len(pile[x]))-1]))))-1]) == (((pile[x-1])[(len(pile[x-1]))-1])[(len((((pile[x-1])[(len(pile[x-1]))-1]))))-1])):
                pile[x-1] = pile[x-1] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[a])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                else:
                    del (pile[x])[-1]
                x = x + 1
            
            else:
                x = x + 1

        #end while loop



and here is like what i need to test:
Warning: Spoiler! (Click to show)
Code:
import unittest
import solitaire

class Test(unittest.TestCase):

    def test1(self):
        actual   = solitaire.play(['AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC',
                                   'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
                                   'AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
                                   'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS'] )
        expected = [['AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS','KH','QH','JH','TH','9H','8H','7H','6H','5H','4H','3H','2H','AH','AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD','KC','QC','JC','TC','9C','8C','7C','6C','5C','4C','3C','2C','AC']]
        self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
        self.assertListEqual(     expected,      actual,  'Incorrect result' )
        pass

    def test2(self):
        actual   = solitaire.play(['QS','5D','4S','8D','8H','3C','3H','5C','9H','6S','QD','2S','2C',
                                   'KS','QC','7C','JC','4H','3D','5S','6C','KD','JS','9C','TS','2D',
                                   '4D','AS','AC','7H','TC','AH','KH','6D','4C','8C','TD','AD','8S',
                                   '3S','JH','KC','QH','TH','6H','7S','5H','2H','9S','7D','JD','9D'] )
        expected = [['8D','8H','3H','3C','JC','7C','QC','QD','4D','4S','6S','KS','KD','5D','5C','2C','2S','QS'],
                    ['9C','4C','4H','9H'],
                    ['6D','2D','3D'],
                    ['JS','TS','TC','6C','AC','AS','5S'],
                    ['KH','AH','7H'],
                    ['8C'],
                    ['AD','TD'],
                    ['3S','8S'],
                    ['6H','TH','JH'],
                    ['KC'],
                    ['2H','QH'],
                    ['9D','JD','7D','7S'],
                    ['5H'],
                    ['9S']]
        self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
        self.assertListEqual(     expected,      actual,  'Incorrect result' )
        pass

    def test3(self):
        actual   = solitaire.play(['5S','9H','QH','2H','8H','6H','3S','3H','AS','5H','JH','3C','KH',
                                   '4H','6S','8S','8D','2D','JD','AH','KC','TD','TC','3D','6D','2C',
                                   'AD','QS','9C','7C','6C','QD','JS','7H','7S','7D','KD','TS','5D',
                                   'KS','9S','9D','4C','5C','8C','QC','JC','TH','4S','4D','2S','AC'] )
        expected = [['4D','5D','5C','8C','QC','JC','4C','4S','TS','AS','AH','4H','5H','7H','7S','JS','QS','QD','AD','6D','3D','TD','JD','2D','8D','8S','6S','KS','KD','7D','9D','9C','7C','6C','2C','TC','3C','KC','KH','JH','9H','QH','2H','8H','6H','3H','3S','5S'],
                    ['9S'],
                    ['TH'],
                    ['2S'],
                    ['AC']]
        self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
        self.assertListEqual(     expected,      actual,  'Incorrect result' )
        pass

    def test4(self):
        actual   = solitaire.play(['7C','9S','QC','4H','9D','3S','AD','9C','8H','AC','2D','9H','AH',
                                   '5D','6C','QD','3H','TC','QS','2S','8D','7D','QH','6H','4C','3C',
                                   'JS','JD','7H','TS','4S','TD','5H','KD','8C','KS','JC','6D','2H',
                                   '5C','3D','KH','8S','JH','TH','KC','2C','5S','AS','4D','7S','6S'] )
        expected = [['3S','9S','9D','AD','AC','7C'],
                    ['QD','8D','7D','2D','5D','5H','8H','4H','9H','9C','QC'],
                    ['AH'],
                    ['TC','6C'],
                    ['6H','3H'],
                    ['JD','JS','2S','QS'],
                    ['QH'],
                    ['5C','JC','8C','3C','4C'],
                    ['7H'],
                    ['6D','TD','KD','KS','4S','TS'],
                    ['2H'],
                    ['3D'],
                    ['TH','KH'],
                    ['AS','5S','8S'],
                    ['JH'],
                    ['2C','KC'],
                    ['4D'],
                    ['6S','7S']]
        self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
        self.assertListEqual(     expected,      actual,  'Incorrect result' )
        pass

    def test5(self):
        actual   = solitaire.play(['6C','AC','7C','7D','KC','6S','9H','5C','JS','3S','TS','9D','JH',
                                   'QD','2D','8H','QS','8D','8C','2S','TC','9C','9S','5H','4D','3C',
                                   'JD','QC','AS','3H','TD','7H','KS','KH','AD','TH','KD','4C','2C',
                                   'QH','3D','5D','7S','4S','6D','2H','AH','JC','8S','4H','5S','6H'] )
        expected = [['JC','QC','2C','2H','QH','AH','4H','6H','6D','3D','5D','JD','TD','TH','7H','7S','4S','4C','TC','3C','3H','5H','KH','KS','KD','8D','4D','AD','AS','9S','9C','KC','5C','8C','8H','9H','JH','JS','TS','3S','6S','QS','2S','2D','QD','9D','7D','7C','AC','6C'],
                    ['5S','8S']]
        self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
        self.assertListEqual(     expected,      actual,  'Incorrect result' )
        pass

    def test6(self):
        actual   = solitaire.play(['8S','7C','AS','5H','7H','9D','8C','8H','3H','TC','AH','KH','JH',
                                   'JC','AD','9C','AC','QS','6H','KC','KS','5C','TS','3C','9H','7D',
                                   '3S','4D','TD','QD','5D','9S','3D','6S','6D','8D','JS','2S','4C',
                                   '4S','5S','6C','2D','4H','7S','2H','KD','TH','JD','QH','QC','2C'] )
        expected = [['5S','7S','JS','2S','4S','4C','4H','TH','QH','QC','6C','9C','AC','KC','KS','QS','3S','3C','5C','5D','QD','7D','4D','TD','TS','9S','6S','6D','3D','8D','2D','2H','KH','6H','9H','9D','AD','AS','AH','3H','5H','JH','JC','8C','TC','7C','7H','8H','8S'],
                    ['JD','KD'],
                    ['2C']]
        self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
        self.assertListEqual(     expected,      actual,  'Incorrect result' )
        pass

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

    

Is that how i would take the list into the play function?
And is the
Code:
for k in list
a good way to iterate through the incoming list?

those are the only things im a bit unsure about!

Thanks guys!
Edited by protzman - 11/8/12 at 6:49pm
post #6 of 6
I looked though it briefly, but one improvement you could do (performance wise) is to put the cards into linked objects.

Think:

Object<->Object2<->Object3(<-Current Pointer because it is newest item in list)

As of now your placing it just in a list which looks like this:

Object->Object2->Object3

So if you want to access Object3 in a regular list you have to iterate though the entire list to get to Object3.

Another thing to note in a linked objects is that since your current pointer is at Object3 you can access Object2 in constant time because Object2 is directly linked to Object3.
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › python [testing help]