计算二进制字符串中的连续数字

时间:2016-03-07 18:32:34

标签: python recursion binary compression

对于家庭作业问题,我们被要求定义一个函数,该函数将计算二进制字符串中连续数字的数量,并返回该数字。

例如,函数应该为二进制输入- (void)viewDidLoad { [super viewDidLoad]; UIView *previousContentView = nil; for (NSInteger i = 0; i < 3; i++) { UIView *contentView = [self addScrollViewWithImageView]; [self.view.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor].active = true; [self.view.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor].active = true; if (previousContentView) { [HorizontalSeparatorView addSeparatorBetweenView:previousContentView secondView:contentView]; NSLayoutConstraint *height = [contentView.heightAnchor constraintEqualToAnchor:previousContentView.heightAnchor]; height.priority = 250; height.active = true; } else { [self.view.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = true; } previousContentView = contentView; } [self.view.bottomAnchor constraintEqualToAnchor:previousContentView.bottomAnchor].active = true; } 返回n = [4,8,4,3,15]

到目前为止我有这个,但我知道这不正确,我不知道从哪里开始。任何帮助将不胜感激!

S = ‘1111000000001111000111111111111111’

注意: 我们不能使用任何循环。我们需要通过递归来完成此操作。

谢谢!

3 个答案:

答案 0 :(得分:2)

这是一种有希望的pythonic方式(忽略了递归地解决这类问题不是pythonic的事实):

def consecutive_length(s):
    def sub(idx, lst, last_char, count):
        try:
            c = s[idx]     # c will be the 'next' char
        except IndexError: # no more chars left to process
            if count:
                lst.append(count)
            return lst
        if c != last_char:
            lst.append(count)
            count = 0
        return sub(idx+1, lst, c, count+1)                            
    return sub(0, [], s[0] if s else None, 0)

,其中

  • 外部函数只是将字符串作为参数并隐藏内部函数附加参数
  • idx是字符串的索引,我们不会在每次递归调用时分配一个新字符串(并且s [idx]是O(1)iirc)
  • 而不是计算字符串的长度,我们等待发生异常(EAFP - 要求宽恕比允许更容易)

测试:

>>> print consecutive_length('1111000000001111000111111111111111')
[4, 8, 4, 3, 15]    
>>> print consecutive_length('1111000000001111000111111111111110')
[4, 8, 4, 3, 14, 1]
>>> print consecutive_length('1')
[1]
>>> print consecutive_length('0')
[1]
>>> print consecutive_length('')
[]

答案 1 :(得分:0)

编辑:uselpa有更好的方法。

由于不允许循环:

def consecutive_length(s, output, prev_char, count):
    # if end of string, append last count and return output
    if s == '':
        output.append(count)
        return output
    # if curr_char is same as prev_char, add 1 to count and parse next char
    if s[0] == prev_char:
        return consecutive_length(s[1:], output, s[0], count + 1)
    # if curr_char is diff from prev_char, append count and reset count to 1
    else:
        prev_char = s[0]
        output.append(count)
        return consecutive_length(s[1:], output, s[0], 1)

使用consecutive_length(s, [], s[0], 0)调用它。

答案 2 :(得分:0)

我假设'11'是1的连续序列。所以'111'有2个连续的1。这个解决方案是,如果循环不是问题。使用索引查找“11”并继续执行,直到找不到为止。以下程序显示连续1的数量。

cnt = 0
pos = -1
while True:
    try:
        pos = '111001100101111'.index('11', pos+1)
        cnt += 1
    except ValueError:
        print cnt
        break

<强>结果:

6
相关问题