检查满堂红 [英] Checking for a full house

查看:62
本文介绍了检查满堂红的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我不确定这是否是发布此类问题的地方。所以请随时告诉我是否应该把这个

带到其他地方。


所以,为了让我开始使用python,我决定把它放在一起

脚本测试滚动某些组合的概率

骰子。下面是我检查满堂的代码(当用5个骰子滚动

时)。卷是一个列表,例如[1,3,5,1,4](这个例子是

不是满屋子)


def removeAll( element,num2Rem,list):

l = list [:]

for num in range(0,num2Rem):

l.remove(元素)

返回l


def isfullHouse(roll):

for die in range(1,7):

如果roll.count(die)== 3:

l = removeAll(die,3,roll)

如果l [0] == l [1]:

返回1

返回0


我的问题是:有更好的方法吗?对于python来说,这是一种更自然的方式,或者只是更有效率?


ps。一卷[1,2,1,1,2]是满堂红(三种一种,另外两种是另外两种)

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)

推荐答案

mwdsmith写道:
mwdsmith wrote:
我是python的新手,我不确定这是否是发布的地方
这样的问题;所以,我可以随意告诉我是否应该把它带到其他地方。

所以,为了让我开始使用python,我决定整理一个小的
脚本来测试概率滚动某些组合的骰子。下面是我检查满堂的代码(当用5个骰子滚动时)。卷是一个列表,例如[1,3,5,1,4](这个例子不是满堂红)

def removeAll(element,num2Rem,list):
l = list [:]
for num in range(0,num2Rem):
l.remove(element)
返回l

def isfullHouse( roll):
for die in range(1,7):
如果roll.count(die)== 3:
l = removeAll(die,3,roll)
如果l [0] == l [1]:
返回1
返回0

我的问题是:有更好的方法吗?对于python来说更自然的方式,或者更高效的方式呢?

ps。一卷[1,2,1,1,2]是满屋(三种一种,另外两种)
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):

计数= [roll.count(die)for die in range(1,7)]

counts.sort()

返回计数== [0,0,0,0,2,3]


-

Robert Kern
rk *** @ ucsd.edu


在草地长高的地狱里

梦想的坟墓是否已经死亡。

- Richard Harter



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
rk***@ucsd.edu

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


" mwdsmith" <再********** @ yahoo.com>写道:
"mwdsmith" <re**********@yahoo.com> wrote:
我是python的新手,我不确定这是否是发布此类问题的地方;所以请随时告诉我是否应该把它带到其他地方。


嗯,这取决于。我们在为你做作业吗? :-)

所以,为了让我开始使用python,我决定整理一个小的
脚本来测试滚动某些骰子组合的可能性。下面是我检查满堂的代码(当用5个骰子滚动时)。卷是一个列表,例如[1,3,5,1,4](这个例子不是满堂红)

def removeAll(element,num2Rem,list):
l = list [:]
for num in range(0,num2Rem):
l.remove(element)
返回l

def isfullHouse( roll):
for die in range(1,7):
如果roll.count(die)== 3:
l = removeAll(die,3,roll)
如果l [0] == l [1]:
返回1
返回0

我的问题是:有更好的方法吗?对于python来说更自然的方式,或者更高效的方式呢?
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.
Well, that depends. Are we doing your homework assignment for you? :-)
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?




好​​吧,我想我可以通过字典进行两次旅行。像

这样的东西如下。两个dict()构造函数的参数是列表

comprehensions。我通常不会经常使用它们,而且我通常不喜欢这样复杂的表达式,但我尝试了几种方法,这只是

似乎是到目前为止计算中最紧凑的方式。所以,即使

虽然我不相信这是最清楚的写作方式,你确实要求

获得最多的pythonic方式,我想这可能是那个:-)


#!/ usr / bin / env python


def isFullHouse(roll):

#计算你拥有的每个值的数量,即多少1',

#多少2'等等

valueDict = dict([( x,0)for x in range(1,7)])

for die in roll:

valueDict [die] + = 1

>
#计算你拥有的每个套装尺寸的数量,即多少个b $ b#空洞,有多少单身,多少对等等。

setDict = dict([(x,0)for x in range(0,6)])

for setSize in valueDict.values():

setDict [setSize] + = 1


#满屋是一对,一个是三种

返回setDict [2] == 1和setDict [3] == 1


print isFullHouse([1,1,1,2,2])== True

print isFullHouse([1,1,1] ,1,1]] ==错误

print isFullHouse([1,2,3,4,5])== False

print isFullHouse([3,4,3,3,4])==正确

打印isFullHouse([1,2,1,1,1])== False



Well, I think I might do two trips through a dictionary. Something like
the following. The arguments to the two dict() constructors are list
comprehensions. I don''t normally use them a lot, and I don''t normally like
complicated expressions like this, but I tried a few ways, and this just
seems to be the most compact way by far to do the counting. So, even
though I''m not convinced this is the clearest way to write it, you did ask
for the most pythonic way, and I think this might be that :-)

#!/usr/bin/env python

def isFullHouse (roll):
# count how many of each value you have, i.e. how many 1''s,
# how many 2''s, etc
valueDict = dict ([(x, 0) for x in range(1,7)])
for die in roll:
valueDict[die] += 1

# count how many of each set size you have, i.e. how many
# voids, how many singletons, how many pairs, etc.
setDict = dict ([(x, 0) for x in range(0,6)])
for setSize in valueDict.values():
setDict[setSize] += 1

# A full house is one pair and one three-of-a-kind
return setDict[2] == 1 and setDict[3] == 1

print isFullHouse ([1, 1, 1, 2, 2]) == True
print isFullHouse ([1, 1, 1, 1, 1]) == False
print isFullHouse ([1, 2, 3, 4, 5]) == False
print isFullHouse ([3, 4, 3, 3, 4]) == True
print isFullHouse ([1, 2, 1, 1, 1]) == False


[mwdsmith]
[mwdsmith]
下面是我检查满堂红的代码(当用5个骰子滚动时)。
Below is my code for checking for a full house (when rolling
with 5 dice).




有很多方法可以做到这一点。这是一个:


..def isfullHouse(手):

..计数= [hand.count(card)for card in set(手)]

..返回(3个计数)和(2个计数)和len(手)== 5

这里是一个单行:


..def isfullHouse(手):

..返回已排序(hand.count(卡)为套装(手))== [ 2,3]

Raymond Hettinger



There are many ways to do this. Here''s one:

..def isfullHouse(hand):
.. counts = [hand.count(card) for card in set(hand)]
.. return (3 in counts) and (2 in counts) and len(hand)==5
And here''s a one-liner:

..def isfullHouse(hand):
.. return sorted(hand.count(card) for card in set(hand)) == [2,3]
Raymond Hettinger


这篇关于检查满堂红的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆