Checking for a full house

Robert Kern rkern at ucsd.edu
Wed May 25 21:58:32 EDT 2005


mwdsmith wrote:
> Hi, I'm new to python, and I'm not sure if this is the place to post
> this kind of question; so feel free to tell me if I should take this
> elsewhere.
> 
> So, to start me off on python, I decided to put together a little
> script to test the probabilities of rolling certain combinations of
> dice.  Below is my code for checking for a full house (when rolling
> with 5 dice).  A roll is a list, eg [1, 3, 5, 1, 4] (this example is
> not a full house)
> 
> def removeAll(element, num2Rem, list):
>     l = list[:]
>     for num in range(0, num2Rem):
>         l.remove(element)
>     return l
> 
> def isfullHouse(roll):
>     for die in range(1,7):
>         if roll.count(die)==3:
>             l = removeAll(die, 3, roll)
>             if l[0]==l[1]:
>                 return 1
>     return 0
> 
> My questions is this: is there a better way to do this?  A way that's
> more natural to python, or just more efficient perhaps?
> 
> ps. A roll of [1, 2, 1, 1, 2] is a full house (three of one kind and
> two of another)

def isFullHouse(roll):
     counts = [roll.count(die) for die in range(1,7)]
     counts.sort()
     return counts == [0, 0, 0, 0, 2, 3]

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list