计数算法

huangapple go评论114阅读模式
英文:

Count algorithm

问题

I am looking for an algorithm that counts a given number of states in Python. There is a string like this: 10?01?011 with "0, 1, ?" it was made. The question mark can be 0 or 1. The program should enumerate all the states that question marks can have in a string. For example, in the program '1?01?1' should be printed:

  1. 110111
  2. 110101
  3. 100111
  4. 100101

Note: The order does not matter.

I'm thinking about this algorithm but I can't find the optimal algorithm for this problem in Python. Does anyone know how I can create an algorithm for this in Python?
I am looking for this algorithm in Python without using libraries.

英文:

I am looking for an algorithm that counts a given number of states in Python.
There is a string like this: 10?01?011
with "0, 1, ?" it was made. The question mark can be 0 or 1. The program should enumerate all the states that question marks can have in a string.
For example, in the program '1?01?1' should be printed:

  1. 110111
  2. 110101
  3. 100111
  4. 100101

Note: The order does not matter.

I'm thinking about this algorithm but I can't find the optimal algorithm for this problem in Python.Does anyone know how I can create an algorithm for this in Python?
I am looking for this algorithm in Python without using libraries.

答案1

得分: 3

你可以使用以下生成器函数:

  1. from itertools import product # 或者使用下面的`product`函数
  2. def states(s):
  3. for tpl in product("01", repeat=s.count("?")):
  4. gaps = iter(tpl) # 产品元组的迭代器
  5. yield "".join(next(gaps) if c == "?" else c for c in s)
  6. list(states("1?01?1"))
  7. # ['100101', '100111', '110101', '110111']
  8. list(states("???"))
  9. # ['000', '001', '010', '011', '100', '101', '110', '111']
  10. list(states("11?"))
  11. # ['110', '111']
  12. list(states("11"))
  13. # ['11']

你形成了"01" x "01" x ... x "01"的笛卡尔积,次数取决于有多少个问号。然后,你反复用产品的元组填充这些问号。

如果不允许使用库,你可以编写自己的product函数,作为一个可替换的函数:

  1. def product(pool, repeat=1):
  2. if not repeat:
  3. yield []
  4. else:
  5. for elmnt in pool:
  6. for tpl in product(pool, repeat-1):
  7. yield [elmnt] + tpl

itertools.product文档中还有另一个product的实现。

英文:

You can use the following generator function:

  1. from itertools import product # or use `product` function from below
  2. def states(s):
  3. for tpl in product("01", repeat=s.count("?")):
  4. gaps = iter(tpl) # iterator over the product tuple
  5. yield "".join(next(gaps) if c == "?" else c for c in s)
  6. list(states("1?01?1"))
  7. # ['100101', '100111', '110101', '110111']
  8. list(states("???"))
  9. # ['000', '001', '010', '011', '100', '101', '110', '111']
  10. list(states("11?"))
  11. # ['110', '111']
  12. list(states("11"))
  13. # ['11']

You form the cartesian product of "01" x "01" x ... x "01" for as how many gaps there are. Then you repeatedly fill the gaps with the tuples of the product.

You can write your own product function as a drop-in replacement if libraries aren't allowed:

  1. def product(pool, repeat=1):
  2. if not repeat:
  3. yield []
  4. else:
  5. for elmnt in pool:
  6. for tpl in product(pool, repeat-1):
  7. yield [elmnt] + tpl

There is another implementation of product in the itertools.product docs.

答案2

得分: 0

逐个字符扩展:

  1. s = '1?01?1'
  2. ss = ['']
  3. for c in s:
  4. if c == '?':
  5. c = '01'
  6. ss =
    展开收缩
  7. for s in ss:
  8. print(s)

输出(在线尝试):

  1. 100101
  2. 100111
  3. 110101
  4. 110111
英文:

Expanding by one chacracter at a time:

<!-- language-all: lang-python -->

<pre><code>s = '1?01?1'

ss = ['']
for c in s:
if c == '?':
c = '01'
ss =

展开收缩

for s in ss:
print(s)
</code></pre>

Output (Try it online!):

TIO-ljbjo7ry: https://tio.run/##TYwxDoAgDEX3nuJv1bhIXIyJ4SDGCWNkQUJdPD1SWPzTf21f4/tcd5jmmHIWrGBjR2MNE4nixrzTeSc4@ABZCCX@LLiWW8ttoHEqj0VUaK5gwAG1pdpS@6Hd7US/RXsTkw9PJz1Rzh8 "Python 3.8 (pre-release) – Try It Online"

  1. 100101
  2. 100111
  3. 110101
  4. 110111

huangapple
  • 本文由 发表于 2023年6月25日 19:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定