我正在构建一个脚本来检查玩家宾果卡对数字随机选择。在我测试之前,我认为我有足够的逻辑。 如果无论订单如何,我如何检查解决方案是否有效?下面是我的大型程序中的逻辑块。
chosennumbers = ['B8', 'O69', 'I27', 'G56']
jordonsboard = ['B8', 'I18', 'N38', 'G47', 'O66',
'B10', 'I27', 'N44', 'G53', 'O71',
'B3', 'I19', 'fre', 'G48', 'O67',
'B4', 'I25', 'N39', 'G56', 'O65',
'B9', 'I30', 'N34', 'G58', 'O69']
hitnodes = [12] # this list houses hits on your board added 12 because its a free space
win = False
iterator = 0
for i in chosennumbers:
if i in jordonsboard:
hitnodes.append(iterator)
iterator += 1
if 0 in hitnodes:
if 6 in hitnodes:
if 12 in hitnodes:
if 18 in hitnodes:
if 24 in hitnodes:
win = True
print('Bingo!')
elif 2 in hitnodes:
if 3 in hitnodes:
if 4 in hitnodes:
win = True
print('Bingo!')
elif 5 in hitnodes:
if 10 in hitnodes:
if 15 in hitnodes:
if 20 in hitnodes:
win = True
print('Bingo!')
if 1 in hitnodes:
if 6 in hitnodes:
if 11 in hitnodes:
if 16 in hitnodes:
if 21 in hitnodes:
win = True
print('Bingo!')
if 2 in hitnodes:
if 7 in hitnodes:
if 12 in hitnodes:
if 17 in hitnodes:
if 22 in hitnodes:
win = True
print('Bingo!')
if 3 in hitnodes:
if 8 in hitnodes:
if 13 in hitnodes:
if 18 in hitnodes:
if 23 in hitnodes:
win = True
print('Bingo!')
if 4 in hitnodes:
if 8 in hitnodes:
if 16 in hitnodes:
if 20 in hitnodes:
win = True
print('Bingo!')
elif 9 in hitnodes:
if 14 in hitnodes:
if 19 in hitnodes:
if 24 in hitnodes:
win = True
print('Bingo!')
答案 0 :(得分:1)
您需要set.intersection()
。您可以将所有数字放在一个集合中,并检查它与列表的交集是否与集合相等。
my_set = {0, 6, 12, 18, 24}
if my_set.intersection(hitnodes) == my_set:
# do stuff
答案 1 :(得分:0)
这就是我最终如何做到这一点。因为我需要知道我的命中列表是否与每个解决方案中的每个项目相匹配,这就是我构建它的方式。
> import urllib.request import urllib.parse import re import collections
>
> url =
> 'http://www.executiveadministrator.com/cgi-local/inoutPROhosted4/inoutPRO.pl?refresh=1&ID=AFTCO'
> #testlist = ['B9,','B8,','B3,','B4,'] counter = 0 #inicialize counter hitlist = [] #inicialize hitlist
>
> board = ['B8,', 'I18,', 'N38,', 'G47,', 'O66,',
> 'B10,','I27,', 'N44,', 'G53,', 'O71,',
> 'B3,', 'I19,', 'fre,', 'G48,', 'O67,',
> 'B4,', 'I25,', 'N39,', 'G56,', 'O65,',
> 'B9,', 'I30,', 'N34,', 'G58,', 'O69,']
>
> resp = urllib.request.urlopen(url) #request html data from website
> respdata = resp.read() #store request data in respdata
>
> string = '<TEXTAREA ROWS="2" NAME="1 ANNOUNCEMENTS-return" COLS="50"
> WRAP=VIRTUAL>(.*?)</TEXTAREA>'#search within text area for bingo
> numbers search = re.findall(string, str(respdata)) #find all between
> <b> and </b> searchstring = str(search) #convert search results to
> string and assign cleanlist = searchstring.split() #split seachstring
> to a cleaned up list cleanlist.append('fre,') #add free space
> solutions =
> [(0,6,12,18,24),(4,8,12,16,20),(0,5,10,15,20),(1,6,11,16,21),
> (2,7,12,17,22),(3,8,13,18,23),(4,9,14,19,24),(0,1,2,3,4),
> (5,6,7,8,9),(10,11,12,13,14),(15,16,17,18,19),(20,21,22,23,24)]
>
> for i in board: #for each item in board
> for j in cleanlist: #and for each item in cleanlist
> if i == j: #check to see if they match append match
> hitlist.append(counter)#add hit number to hitlist
> print('hit: {} at space: {}'.format(i, counter))
> counter += 1
> #check each possible solution against current hit list for each in solutions: #for each item in solutions
> if collections.Counter(hitlist) == collections.Counter(each): #check to see if all items match
> print('BINGO')