[Python training camp] Python daily practice -- day 9: seven segment code

πŸ“’πŸ“’πŸ“’πŸ“£πŸ“£πŸ“£
🌻🌻🌻 Hello, everyone. My name is Dream. I'm an interesting Python blogger. Please take care of me 😜😜😜
πŸ…πŸ…πŸ… CSDN is a high-quality creator in Python field and is a sophomore. Welcome to find me for cooperative learning (VX wants to enter the learning exchange group or learning materials at the end of the article. Welcome to + + +)
πŸ’• Introduction note: this paradise is never short of genius, and hard work is your final admission ticket! πŸš€πŸš€πŸš€
πŸ’“ Finally, may we all shine where we can't see and make progress together 🍺🍺🍺
πŸ‰πŸ‰πŸ‰ "Ten thousand times sad, there will still be Dream, I have been waiting for you in the warmest place", which is me! Ha ha ha~ 🌈🌈🌈
🌟🌟🌟✨✨✨

[Python training camp] is a topic brushing Carnival party for Python language learning! If you don't grasp the basic knowledge firmly, you are welcome to refer to this set of courses: Python open class It's best to use it together. If you like it, hurry up and subscribe! πŸ‹πŸ‹πŸ‹ If you don't have self-control or motivation to learn and communicate together, please send a private letter or add my VX at the end of the text. I will pull you into the learning and communication group. We will communicate and study together and punch in the group

Title Description

Title Description
This question is a blank filling question. You only need to calculate the result and use the output statement in the code to output the filled result.
Xiaolan uses a seven segment code nixie tube to represent a special text.

Picture description

The figure above shows a diagram of a seven segment code nixie tube. There are 77 segments of light-emitting diodes in the nixie tube, marked as a, B, C, D, e, F, GA, B, C, D, e, F and G.

Xiaolan should select some diodes (at least one) to emit light to express characters. When designing the expression of characters, all light-emitting diodes are required to be connected together.

For example: bb light, other diodes do not light, can be used to express a character.

For example, cc emits light and other diodes do not emit light, which can be used to express a character. This scheme and the scheme on the previous line can be used to represent different characters, although they look similar.

For example, a, b, c, d, ea,b,c,d,e emit light, f, gf,g do not emit light, which can be used to express a character.

For example, if B, FB and f emit light, other diodes do not emit light, they cannot be used to express a character, because the light-emitting diodes are not connected together.
Excuse me, how many different characters can Xiaolan express with seven segment code nixie tube?
Operational limits
Maximum running time: 1s
Maximum operating memory: 128M

Master practice: Drawing

In fact, when I see this topic, my first feeling is the arrangement and combination problem in high school. Then I can draw the number of pictures directly. Anyway, there should not be many kinds:

# In the first type, only one segment of nixie tube emits light
l1 = 7
# Two segment nixie tube (ab,af,bc,bg,cg,cd,de,eg,ef,fg)
l2 = 10
# Three segment nixie tube luminescence (abf,abc,abg,afg,afe,bcd,bcg,bgf,bge,cgd,cgf,cge,cde,cdg,deg,def,efg)
l3 = 16
# Four segment nixie tube luminescence (abcd,abcg,abcf,abge,abgf,abfe,afeg,bcde,bcdg,bcgf,bcge,bged,bgef,cdef,cdeg,cdgf,cgfa,cgfe,defg,defa)
l4 = 20
# Five segment nixie tube emits light (check that two segments do not emit light ab,ac,ad,ae,af,ag,bc,bd,be,bg,cd,cf,cg,de,df,dg,ef,eg,fg)
l5 = 19
# The six segment nixie tube emits light (check that the first segment does not emit light)
l6 = 7
# Seven segment nixie tube luminous
l7 = 1
sum = l1 + l2 + l3 + l4 + l5 + l6 + l7
print(sum)

Problem solving ideas

  • Use the dictionary to list all labels connected with this label. Dict = {a ': ['f', 'B'],'b ': ['a', 'C', 'g'],'c ': ['b','d ',' g '],'d': ['e ',' C '],'e': ['d ',' f ',' g '],'f': ['a ',' e ',' g '],'g': ['b ',' C ',' e ',' f ']}
  • Then combine all keys to form all combinations containing 1, 2, 3, 4, 5, 6 and 7 elements respectively, and store them in the list for later use;
  • Then traverse the list. If the convenient element length is 1, it must meet the requirements;
  • If it is greater than 1, this element should be rearranged, because, for example, in new_ In the case of "cfg" in the list ("cfg" is also a case representing different characters), if the string "cfg" is not arranged in order, directly use:
  for c in range(1, len(str1)):
      if str1[c - 1] not in dict[str1[c]]:
           break
  else:
      num += 1
      break

Statement. Since "c" is not in dic ["f"], it will leave "cfg".
If it is arranged in an orderly manner, it will not leave a situation similar to "cfg":

 for situation in itertools.permutations(str1):
      for c in range(1, len(situation)):
          if situation[c - 1] not in dict[situation[c]]:
              break
      else:
          num += 1
          break

After arranging str1 strings in order, when situation = "cgf", else statement will be executed to add one to num and exit the outer for loop. And there will be no loss

Source sharing

import itertools
new_list = []
# count
num = 0
# Use a dictionary to list all the labels connected with this label
dict = {'a': ['f', 'b'], 'b': ['a', 'c', 'g'], 'c': ['b', 'd', 'g'],
        'd': ['e', 'c'], 'e': ['d', 'f', 'g'], 'f': ['a', 'e', 'g'],
        'g': ['b', 'c', 'e', 'f']}

string = list(dict.keys())
# print(string)
for i in range(1, 8):
    # Unordered combination of the first i characters in a string. i means that several numbers are a combination.
    for j in (itertools.combinations(string, i)):
        new_list.append(''.join(j))
# print(new_list)
for str1 in new_list:
    # If the length of the string is 1, ('a ',' B ',' C ','d', 'e', 'f', 'g') can represent a different character, so add one directly
    if len(str1) == 1:
        num += 1
        continue
    # print(str1)
    for situation in itertools.permutations(str1):
      # print(situation)
      for c in range(1, len(situation)):
          # print(situation[c-1])
          # print(dict[situation[c]])
          if situation[c] not in dict[situation[c-1]]:
              break
      else:
          num += 1
          break
print(num)

***

Today, I would like to summarize the permutation and combination problem often mentioned in Mathematics:
1.itertools.combinations() usage:

for j in (itertools.combinations('abcd', 3)):
    print(j)
    print(''.join(j))
# ('a', 'b', 'c')
# ('a', 'b', 'd')
# ('a', 'c', 'd')
# ('b', 'c', 'd')
# abc
# abd
# acd
# bcd

2.itertools.permutations() usage:

import itertools
for i in itertools.permutations('abc'):
    print(i)
# ('a', 'b', 'c')
# ('a', 'c', 'b')
# ('b', 'a', 'c')
# ('b', 'c', 'a')
# ('c', 'a', 'b')
# ('c', 'b', 'a')

πŸ… Today is my ninth day in Python training camp. I hope to see the best of you every day πŸ…

πŸ† Previous articles - good articles recommended πŸ†

πŸ† Previous articles -- good article recommendation πŸ†

πŸ₯‡ [Python open class] zero foundation playing with Python basics -- Section 1: Python's self introduction

πŸ₯ˆ [Python open class] zero foundation playing with Python advanced chapter -- Section 1: file operation in Python

πŸ₯‰ Come to a topic brushing Carnival party---- [Python training camp]
🌲🌲🌲 Well, that's all I want to share with you today
❀️❀️❀️ If you like it, don't be stingy with your one button three connections~

Keywords: Python Back-end

Added by sdizier on Fri, 28 Jan 2022 02:23:03 +0200