【问题标题】:Read a string of 1's and 0's. Count numbers of successive 1's and number of successive 0's, until the end读取一串 1 和 0。计算连续 1 的个数和连续 0 的个数,直到结束
【发布时间】:2015-10-22 16:16:38
【问题描述】:

读取一串 1 和 0。计算连续 1 的个数 和连续 0 的数量, 直到最后。

例如,

s = "10001110000111"

输出应该是:

1 1's
3 0's
3 1's
4 0's
3 1's

我需要帮助来解决这个问题,只使用字符串函数(没有 find 函数)和 while/for 循环。

我有这个:

myString = input("Please enter a string of 0s and 1s: ")
zeroCount = 0
oneCount = 0
index = 0

while index < (len(myString) -1):
    if myString[index] == "0":
        zeroCount += 1
        if myString[index +1] == "1":
            zeroCount = 0
    elif myString[index] == "1":
        oneCount += 1
        if myString[index +1] == "0":
            oneCount = 0
    index += 1

我做错了什么?

【问题讨论】:

  • 欢迎来到 Stack Overflow!您似乎在要求某人为您编写一些代码。 Stack Overflow 是一个问答网站,而不是代码编写服务。请see here学习如何写出有效的问题。

标签: python python-3.x python-3.4


【解决方案1】:

这与所谓的“运行长度编码”非常相似,后者在Rosettacode.org 上有一个很好的条目

def encode(input_string):
    count = 1
    prev = ''
    lst = []
    for character in input_string:
        if character != prev:
            if prev:
                entry = (prev,count)
                lst.append(entry)
                #print lst
            count = 1
            prev = character
        else:
            count += 1
    else:
        entry = (character,count)
        lst.append(entry)
    return lst


def decode(lst):
    q = ""
    for character, count in lst:
        q += character * count
    return q

#Method call
encode("aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa")
decode([('a', 5), ('h', 6), ('m', 7), ('u', 1), ('i', 7), ('a', 6)])

我认为您将能够接受并对其进行一些修改以做您想做的事情。

【讨论】:

    【解决方案2】:

    如果您想摆脱纯语言技术并使用更惯用的风格,这是使用groupby 的好时机,它接受输入并根据键函数何时返回不同将其分成组价值。在这种情况下,键函数只是值,所以我们可以省略它。

    import itertools
    
    s = "10001110000111"
    groups = itertools.groupby(s)
    

    groups 现在是一个 groupby 迭代器,它懒惰地计算为:

    [('1', ['1']),
     ('0', ['0', '0', '0']),
     ('1', ['1', '1', '1']),
     ('0', ['0', '0', '0', '0']),
     ('1', ['1', '1', '1'])]
    

    你可以遍历这个来查看:

    for groupname, group in groups:
        length = sum(1 for _ in group)
        # group is not a list, it just acts like one, so we can't use len
        print("{} {}'s".format(length, groupname))
    

    总之,看起来像:

    import itertools
    
    s = "10001110000111"
    groups = itertools.groupby(s)
    
    for groupname, group in groups:
        length = sum(1 for _ in group)
        print("{} {}'s".format(length, groupname))
    

    并返回结果

    1 1's
    3 0's
    3 1's
    4 0's
    3 1's
    

    【讨论】:

      【解决方案3】:

      您可以使用字符串的index方法查找下一个序列并在每个步骤中修剪源字符串:

      s = "10001110000111"
      
      while s:
          try:
              count = s.index("0") if s[0] == "1" else s.index("1")
              print("{} {}'s".format(count, s[0]))
              s = s[count:]
          except ValueError:
              print("{} {}'s".format(len(s), s[0]))
              break
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 2020-02-22
        • 1970-01-01
        相关资源
        最近更新 更多