知恵袋で見つけた判断推理のリーグ戦の問題をPythonで解いてみました。(^_^;
A~Fの6チームが総当たり戦で野球の試合を行った。各チームとも毎日1試合を行い、5日間で全試合を終了した。
今、次のア~エのことが分かってる時、確実に言えるのはどれか。
ア AとCの対戦は1日目に行われた。
イ BとFの対戦は2日目に行われた。
ウ DとEの対戦は3日目に行われた。
エ AとDの対戦およびBとCの対戦は4日目に行われた。1.AとEの対戦は5日目に行われた。
2.BとEの対戦は1日目に行われた。
3.CとEの対戦は2日目に行われた。
4.DとFの対戦は5日目に行われた。
5.EとFの対戦は1日目に行われた。
下の表のように、ペアを直接記入して表を作成する方法で作りました。
pairs = ['AB', 'AE', 'AF', 'BD', 'BE', 'CD', 'CE', 'CF', 'DF', 'EF'] を順列 p として回しました。
ただし、p[0]< p[1], p[2]< p[3], p[4]< p[5], p[7]< p[8]< p[9]と順序を付けました。(^_^;
+---+----+----+----+ | 1 | AC |p[0]|p[1]| +---+----+----+----+ | 2 | BF |p[2]|p[3]| +---+----+----+----+ | 3 | DE |p[4]|p[5]| +---+----+----+----+ | 4 | AD | BC |p[6]| +---+----+----+----+ | 5 |p[7]|p[8]|p[9]| +---+----+----+----+
ちなみに、「各チームとも毎日1試合を行う」条件の
「if not len(set('AC')|set(p[0])|set(p[1]))==6: continue」のところは、
「if not(set('AC')|set(p[0])|set(p[1])==set(P)): continue」でもいいです。(^_^;
● LeagueBaseball1.py
# coding: UTF-8 # LeagueBaseball1.py import itertools from time import time # 確実にいえる選択肢を得る def getAns(ch,lb='12345'): return ','.join([lb[i] for i,c in enumerate(ch) if c]) def toStr(li,sp='',fm='%s'): return fm%sp.join(li) # sp.join(li).format(fm) def main(): tm = time() # Timer Start choices = [True]*5 P = 'ABCDEF' pairs = [] for c in itertools.combinations(P,2): pairs.append(toStr(c)) pairs = sorted(list(set(pairs)-{'AC','BF','DE','AD','BC'})) ## print(pairs); exit() # ['AB', 'AE', 'AF', 'BD', 'BE', 'CD', 'CE', 'CF', 'DF', 'EF'] for p in itertools.permutations(pairs): if not(p[0]< p[1]): continue if not(p[2]< p[3]): continue if not(p[4]< p[5]): continue if not(p[7]< p[8]< p[9]): continue if not len(set('AC')|set(p[0])|set(p[1]))==6: continue lm1 = ['AC',p[0],p[1]] # 1日目の試合 if not len(set('BF')|set(p[2])|set(p[3]))==6: continue lm2 = ['BF',p[2],p[3]] # 2日目の試合 if not len(set('DE')|set(p[4])|set(p[5]))==6: continue lm3 = ['DE',p[4],p[5]] # 3日目の試合 if not len(set('AD')|set('BC')|set(p[6]))==6: continue lm4 = ['AD','BC',p[6]] # 4日目の試合 if not len(set(p[7])|set(p[8])|set(p[9]))==6: continue lm5 = [p[7],p[8],p[9]] # 5日目の試合 pass # チェックを潜り抜けたものを表示 print(f' 1:{lm1}\n 2:{lm2}\n 3:{lm3}\n 4:{lm4}\n 5:{lm5}\n') pass # 選択肢のチェック choices[0] &= ('AE' in lm5) choices[1] &= ('BE' in lm1) choices[2] &= ('CE' in lm2) choices[3] &= ('DF' in lm5) choices[4] &= ('EF' in lm1) print(f'∴{getAns(choices)}') print(f'Runtime : {time()-tm:.3f} [sec]') # Timer Stop & Disp if __name__ == '__main__': main()
●実行結果
1:['AC', 'BE', 'DF'] 2:['BF', 'AE', 'CD'] 3:['DE', 'AB', 'CF'] 4:['AD', 'BC', 'EF'] 5:['AF', 'BD', 'CE'] ∴2 Runtime : 2.276 [sec]
※参考URL
●Python, set型で集合演算(和集合、積集合や部分集合の判定など) | note.nkmk.me
●知恵袋で見つけた判断推理のリーグ戦の問題をPythonで解いてみた。 - rscのブログ
●知恵袋で見つけた判断推理のリーグ戦の問題をPythonで解いてみた。(2) - rscのブログ
●知恵袋で見つけた判断推理のリーグ戦の問題をPythonで解いてみた。(3) - rscのブログ
●知恵袋で見つけた判断推理のリーグ戦の問題をPythonで解いてみた。(5) - rscのブログ
