使用Python的弦压缩
其他所有内容似乎都很好,但是最后一个角色总是偏离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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您要执行
Itertools
模块 - 尝试groupby
:实现它的另一种方法是使用
more_itertools.run_length
。If you are up to the
itertools
module - trygroupby
:Another way to achieve it, is to use
more_itertools.run_length
.您可以在使用字符时使用字典来更轻松,并在使用字符时删除字符,这计算要压缩的字符数量
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
Python函数以执行字符串压缩。例如,“ AABCCCCCAAA”将成为“ A2B1C5A3”。
Python function to perform string compression. For example, "aabcccccaaa" would become "a2b1c5a3".
您可以使用带有BackReference
([AZ])\ 1
匹配重复字符的模式,并使用匹配的长度组装最终字符串。然后,您可以比较原始字符串和组装的字符串的长度。
示例代码
输出
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
Output