Blue Bridge Cup python group - seven-segment code

Blue Bridge Cup python group - seven-segment code

insert image description here
insert image description here

import os
import sys
dic = {
    
    'a':['f','b'],
       'b':['a','g','c'],
       'c':['g','d','b'],
       'd':['e','c'],
       'e':['f','g','d'],
       'f':['a','g','e'],
       'g':['f','b','e','c']}
import itertools
totle = []
for i in range(2,8):
  a = list(itertools.combinations('abcdefg',i))#i代表组合的长度,最短为2最长为7
  for j in a:
    totle.append(''.join(j))  #化元组为str
#已经变成了['ab', 'ac',......'bcdefg', 'abcdefg']
#然后就要排序''
sum = 0
for str1 in totle:
  for num in itertools.permutations(str1):#全排列
    for j in range(len(num)-1):  #判断例如'bcdefg'变成其他不同序列后是否符合
      if num[j+1] not in dic[num[j]]:
        break
    else:
      sum += 1
      break
print(sum+7)#最后加7是因为,没有算单个管的,一共7个管所以加上7

The answer is 80

For itertools.combinations usage, tap the link to send

click me click me!
Thank you for your support. Your one-click three-connection is the biggest driving force for Ganggang students!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324322196&siteId=291194637