简单的递归生成器问题 [英] Simple Recursive Generator Question

查看:71
本文介绍了简单的递归生成器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个生成函数,它可以生成掩码中每个设置位的索引位置



例如

for bitIndexGenerator中的x(0x16):#10110

打印x

- > 1 2 4

这就是我所拥有的,但它不起作用。

更改打印的产量,表明递归正常工作。

def bitIndexGenerator(mask,index = 0):

if mask == 0:return

elif mask& 0x1:收益率指数

bitIndexGenerator(掩码>> 1,指数+ 1)


我缺少什么?

解决方案

MetalOne写道:

我正在尝试编写一个生成函数,生成每个设置位的索引位置
一个面具。
对于bitIndexGenerator中的x(0x16):#10110
print x
- > 1 2 4

这就是我所拥有的,但它不起作用。
改变打印的产量,表明递归正常工作。

def bitIndexGenerator( mask,index = 0):
如果mask == 0:返回
elif mask& 0x1:yield index
bitIndexGenerator(mask>> 1,index + 1)

我错过了什么?




bitIndexGenerator(mask>> 1,index + 1)只返回一个新的生成器,它立即丢弃
。要让生成器做任何事情,你必须调用它的next()方法,其中一种方法是for循环,e。 g(不是掩码< 0

证明):

def big(mask, index = 0):
.... if mask!= 0:

.... if mask& 1:

....收益率指数

....结果大(掩码>> 1,指数+ 1):

....收益结果

.... for x in big(0x16):



.. ..打印x,

....

1 2 4


彼得


MetalOne在消息中写道

< 92 ************************** @ posted。 google.com取代。 ..

我缺少什么?




生成器和生成器函数之间的区别。

def gen_func():yield无
.... gen_func()#返回什么?




此外,这不是一个递归问题。使用循环。

-

Francis Avila


文章< 92 ** ************************@posting.google.com>,
jc*@iteris.com (MetalOne)写道:

def bitIndexGenerator(mask,index = 0):
if mask == 0:return
elif面具& 0x1:yield index
bitIndexGenerator(mask>> 1,index + 1)




问题的实际答案是,当你递归调用a时

生成器,它的返回值(其结果的迭代器)需要

使用时不要忽略。


但原因我发帖是建议另一种方法:

bitIndices = dict([(1L<< i,i)for i in range(32)])

>
def bitIndexGenerator(蒙版):

而面具:

bit = mask&〜(mask - 1)

yield bitIndices [bit]

mask& =〜bit


-

David Eppstein http://www.ics.uci.edu/~eppstein/

大学加州,欧文,信息学院和计算机科学


I am trying to write a generator function that yields the index position
of each set bit in a mask.
e.g.
for x in bitIndexGenerator(0x16): #10110
print x
--> 1 2 4
This is what I have, but it does not work.
Changing yield to print, shows that the recursion works correctly.

def bitIndexGenerator(mask, index=0):
if mask == 0: return
elif mask & 0x1: yield index
bitIndexGenerator(mask >> 1, index+1)

What am I missing?

解决方案

MetalOne wrote:

I am trying to write a generator function that yields the index position
of each set bit in a mask.
e.g.
for x in bitIndexGenerator(0x16): #10110
print x
--> 1 2 4
This is what I have, but it does not work.
Changing yield to print, shows that the recursion works correctly.

def bitIndexGenerator(mask, index=0):
if mask == 0: return
elif mask & 0x1: yield index
bitIndexGenerator(mask >> 1, index+1)

What am I missing?



The bitIndexGenerator(mask>>1, index+1) just returns a new generator which
is immediately discarded. For a generator to do anything, you must invoke
its next() method, and one way to do it is a for loop, e. g (not mask<0
proof):

def big(mask, index=0): .... if mask != 0:
.... if mask & 1:
.... yield index
.... for result in big(mask>>1, index+1):
.... yield result
.... for x in big(0x16):


.... print x,
....
1 2 4

Peter


MetalOne wrote in message
<92**************************@posting.google.com>. ..

What am I missing?



The difference between a generator and a generator function.

def gen_func(): yield None .... gen_func() # what does this return?



Also, this isn''t a recursive problem. Use a loop.
--
Francis Avila


In article <92**************************@posting.google.com >,
jc*@iteris.com (MetalOne) wrote:

def bitIndexGenerator(mask, index=0):
if mask == 0: return
elif mask & 0x1: yield index
bitIndexGenerator(mask >> 1, index+1)



The actual answer to your question is that when you recursively call a
generator, its return value (the iterator of its results) needs to be
used not ignored.

But the reason I''m posting is to suggest an alternative approach:
bitIndices = dict([(1L<<i,i) for i in range(32)])

def bitIndexGenerator(mask):
while mask:
bit = mask &~ (mask - 1)
yield bitIndices[bit]
mask &=~ bit

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science


这篇关于简单的递归生成器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆