python 的“look-and-say”序列得到改进
我想首先介绍一下“看-说”序列。它就像a = {1, 11, 21, 1211, 111221 ...
系统检查前一个数字并计算数字。
1 = 一 1(所以 = 11)
11 = 两个 1(所以 = 21)
21 = one 2 one 1 (so = 1211)
作为序列的规则,任何数字都不能超过 3,因此创建一个翻译表可以适应。但这不是语义,我不喜欢它。
我想要的是一个评估给定值并返回一个看起来相似的字符串的脚本。
但是,为了超越限制,我希望它甚至可以计算字符,这样它就可以返回 1A2b41
。
我已经尝试让它工作几个小时了,但逻辑很糟糕,我现在脑子一片空白。
这是实际上不起作用的脚本(返回错误结果),但它至少可以给您这个想法。
def seq(a):
k,last,result,a = 1,'','',str(a)
for i in range(len(a)):
if last==a[i]:k+=1
else:
result = result+str(k)+a[i]
k=1
last = a[i]
return result
I would like to introduce look-and-say sequence at first. It goes like a = {1, 11, 21, 1211, 111221 ...
The system is it checks the previous digit and counts the numbers.
1 = one 1 (so = 11)
11 = two 1 (so = 21)
21 = one 2 one 1 (so = 1211)
As a rule of the sequence, no number can go beyond 3, so creating a translation table can fit in. But it is not semantic, I don't like it.
What I want is, a script which evaluates the given value and return a look-and-say-alike string.
However, to go beyond out limits, I want it to even evaluate chars, so it can return 1A2b41
.
I have been trying to make it work for hours, the logic went bad and I am having a brainfreeze at the moment.
Here is the script that actually doesn't work(returns false results), but it can give you the idea, at least.
def seq(a):
k,last,result,a = 1,'','',str(a)
for i in range(len(a)):
if last==a[i]:k+=1
else:
result = result+str(k)+a[i]
k=1
last = a[i]
return result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
groupby
,它正是您想要的:groupby
从可迭代对象返回连续的键和组。键是为每个元素计算的函数,如果未指定,则为恒等函数(如上所述)。该组是一个迭代器——当关键函数的值发生变化时会生成一个新的组。例如,根据文档:You can use
groupby
, it's just what you want:groupby
returns consecutive keys and groups from an iterable object. The key is a function computed for each element, or an identity function if not specified (as above). The group is an iterator - a new group is generated when the value of the key function changes. So, for instance, according to the documentation:我可以看到您的代码存在两个问题:
结果
由k
和a[i]
扩展,尽管计数器k
不计算字符a[i]
,而是计算字符last
。此处将a[i]
替换为last
(您可能不想在第一轮中添加任何内容)。循环结束后,您必须再次将计数器的最后一个值与最后一个字符相加(这尚未完成),即添加另一个
result = result+str(k)+last
在循环之后。总的来说,它看起来像
I can see two issues with your code:
The
result
is expanded byk
anda[i]
although the counterk
does not count charsa[i]
but charslast
. Replacea[i]
bylast
here (you may not want to add anything in the first round).After the loop you have to add the last value of the counter together with the last character again (this was not yet done), i.e. add another
result = result+str(k)+last
after the loop.In total it looks like
我认为你被难住的部分原因是你使用了无意义的变量名。您很好地描述了问题并按名称调用了它,但甚至没有在您的函数中使用该名称。
如果您将开头的字符串视为“look”,将结尾的字符串视为“say”,那么这就是一个开始。
结果
可能很好,但a
和k
让你感到困惑。我认为last
具有误导性,因为它可以表示先前的或最终的。另外,Python 的
for
实际上是foreach
,这是有原因的——您一次获取“look”中的每个字符,因此在循环中明确执行。间距不太重要,但 Python 确实有一个标准编码风格,并且它确实有助于使用它的可读性。您花在解析代码上的精神时间越少,您对问题的关注就越多。
I think part of why you got stumped is your use of meaningless variable names. You described the problem quite well and called it by name, but didn't even use that name for your function.
If you think of the string you start with as "look", and the one you end up with as "say", that is a start.
result
is probably fine buta
andk
have confused you.last
is, I think, misleading, because it can mean either previous or final.Also, Python's
for
is reallyforeach
for a reason -- you're taking each character in the "look" one at a time, so do it explicitly in the loop.The spacing is less important, but Python does have a standard coding style, and it does help readability to use it. The less mental time you have to spend parsing your code, the more focus you have for the problem.