使用Python的弦压缩

发布于 2025-02-03 22:29:23 字数 1256 浏览 4 评论 0原文

其他所有内容似乎都很好,但是最后一个角色总是偏离1。 例如,如果我输入ABCCCDDD,我会得到A1B1C3D2,但我应该得到A1B1C3D3。 任何提示都将不胜感激!

迅速的: 字符串压缩:实现一种使用重复字符计数执行基本字符串压缩的方法。例如,字符串AABCCCCCAAA将成为A2BLC5A3。如果“压缩”字符串不会比原始字符串小,则您的方法应返回原始字符串。您可以假设字符串只有大写和小写字母(A -z)。首先做简单的事情。压缩字符串,然后比较长度。请注意,您不会反复将字符串串在一起,这可能非常低效。

def compression(string): 
    hash = {}
    list = []
    count = 0
    for i in range(len(string) - 1): 
        if string[i - 1] != string[i] or i == 0: 
            if string[i] != string[i + 1] or i == len(string) - 2: 
                count = count + 1
                list.append(str(string[i]))
                list.append(str(count))
                count = 0
            elif string[i] == string[i + 1]: 
                count = count + 1
        elif string[i - 1] == string[i]:
            if string[i] != string[i + 1] or i == len(string) - 2: 
                count = count + 1
                list.append(str(string[i]))
                list.append(str(count))
                count = 0
            if string[i] == string[i + 1]: 
                count = count + 1
        print(list)
    result =  "".join(list)
    if len(result) == len(string): 
        return string
    else: 
        return result
string = "abcccfffgggg"
compression(string)

Everything else seems to work just fine, but last character is always off by 1.
For example, if I input abcccddd, I get a1b1c3d2 but I should get a1b1c3d3.
Any hint would be much appreciated!

Prompt:
String Compression: Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z). Do the easy thing first. Compress the string, then compare the lengths. Be careful that you aren't repeatedly concatenating strings together, this can be very inefficient.

def compression(string): 
    hash = {}
    list = []
    count = 0
    for i in range(len(string) - 1): 
        if string[i - 1] != string[i] or i == 0: 
            if string[i] != string[i + 1] or i == len(string) - 2: 
                count = count + 1
                list.append(str(string[i]))
                list.append(str(count))
                count = 0
            elif string[i] == string[i + 1]: 
                count = count + 1
        elif string[i - 1] == string[i]:
            if string[i] != string[i + 1] or i == len(string) - 2: 
                count = count + 1
                list.append(str(string[i]))
                list.append(str(count))
                count = 0
            if string[i] == string[i + 1]: 
                count = count + 1
        print(list)
    result =  "".join(list)
    if len(result) == len(string): 
        return string
    else: 
        return result
string = "abcccfffgggg"
compression(string)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

郁金香雨 2025-02-10 22:29:23

如果您要执行Itertools模块 - 尝试groupby

s = 'bbbbaacddd' # dddeeef gg'
groups = [(label, len(list(group))) 
                  for label, group in groupby(s) if label] #

compressed = "".join("{}{}".format(label, count) for label, count in groups)

print(compressed)  #    b4a2c1d3         

实现它的另一种方法是使用more_itertools.run_length


>>> compressed = list(run_length.encode(s))
>>> compressed
[('b', 4), ('a', 2), ('c', 1), ('d', 3)]
>>> ''.join("{}{}".format(label, count) for label, count in compressed)
'b4a2c1d3'

If you are up to the itertools module - try groupby:

s = 'bbbbaacddd' # dddeeef gg'
groups = [(label, len(list(group))) 
                  for label, group in groupby(s) if label] #

compressed = "".join("{}{}".format(label, count) for label, count in groups)

print(compressed)  #    b4a2c1d3         

Another way to achieve it, is to use more_itertools.run_length.


>>> compressed = list(run_length.encode(s))
>>> compressed
[('b', 4), ('a', 2), ('c', 1), ('d', 3)]
>>> ''.join("{}{}".format(label, count) for label, count in compressed)
'b4a2c1d3'
再可℃爱ぅ一点好了 2025-02-10 22:29:23

您可以在使用字符时使用字典来更轻松,并在使用字符时删除字符,这计算要压缩的字符数量

string = "aabccccaaaa"

output = ""
lastchar = string[0]
counts = {lastchar:1}

for i in range(1, len(string)):
    s = string[i]
    if s == lastchar:
        counts[s] += 1
    else:
        output += f"{lastchar}{counts[lastchar]}" if counts[lastchar] > 1 else lastchar
        del counts[lastchar]
        counts[s] = 1
    lastchar = s

print(output+f"{lastchar}{counts[lastchar]}" if counts[lastchar] > 1 else lastchar)

You can make this easier by using a dictionary and deleting the characters whenever you use them, which counts the number of characters you want to compress

string = "aabccccaaaa"

output = ""
lastchar = string[0]
counts = {lastchar:1}

for i in range(1, len(string)):
    s = string[i]
    if s == lastchar:
        counts[s] += 1
    else:
        output += f"{lastchar}{counts[lastchar]}" if counts[lastchar] > 1 else lastchar
        del counts[lastchar]
        counts[s] = 1
    lastchar = s

print(output+f"{lastchar}{counts[lastchar]}" if counts[lastchar] > 1 else lastchar)
黄昏下泛黄的笔记 2025-02-10 22:29:23

Python函数以执行字符串压缩。例如,“ AABCCCCCAAA”将成为“ A2B1C5A3”。

def string_compression(s):
    result = ""
    if not s:
        return result
    char_count = 1  # Initialize character count to 1
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            char_count += 1
        else:
            result += s[i - 1] + str(char_count)
            char_count = 1
    result += s[-1] + str(char_count)
    return result

print(string_compression('aabcccccaaa'))

Python function to perform string compression. For example, "aabcccccaaa" would become "a2b1c5a3".

def string_compression(s):
    result = ""
    if not s:
        return result
    char_count = 1  # Initialize character count to 1
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            char_count += 1
        else:
            result += s[i - 1] + str(char_count)
            char_count = 1
    result += s[-1] + str(char_count)
    return result

print(string_compression('aabcccccaaa'))
温柔少女心 2025-02-10 22:29:23

您可以使用带有BackReference ([AZ])\ 1匹配重复字符的模式,并使用匹配的长度组装最终字符串。

然后,您可以比较原始字符串和组装的字符串的长度。

示例代码

import re

strings = [
    "abcccddd",
    "aabcccccaaa",
    "abcd",
    "aabbccddeeffffffffffffff",
    "a"
]

def compression(s):
    res = ''.join([x.group(1) + str(len(x.group())) for x in re.finditer(r"([a-z])\1*", s, re.I)])
    return res if len(s) >= len(res) else s

for s in strings:
    print(compression(s))

输出

a1b1c3d3
a2b1c5a3
abcd
a2b2c2d2e2f14
a

You could use a pattern with a backreference ([a-z])\1 matching the repeating characters, and assemble the final string with counts using the length of the matches.

Then you can compare the length of the original string and the assembled string.

Example code

import re

strings = [
    "abcccddd",
    "aabcccccaaa",
    "abcd",
    "aabbccddeeffffffffffffff",
    "a"
]

def compression(s):
    res = ''.join([x.group(1) + str(len(x.group())) for x in re.finditer(r"([a-z])\1*", s, re.I)])
    return res if len(s) >= len(res) else s

for s in strings:
    print(compression(s))

Output

a1b1c3d3
a2b1c5a3
abcd
a2b2c2d2e2f14
a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文