Python 中的指数哥伦布解码器(Codewars)
Exponential-Golomb Decoder in Python (Codewars)
我正在尝试通过这里的 Codewars 挑战之一,指数哥伦布解码器”:https://www.codewars.com/kata/5e4d8a53b499e20016b018a0/swift
想法是采用二进制格式输入:"001000000101111001010001101" -> 00100 000010111 1 00101 0001101
和return解码数字列表:[3, 22, 0, 4, 12]
如果你给出 Sentence 例如 00100 00101,让我们看看你从 2 个前导零开始,然后点击 1,所以第一个结果数是 零的数量 + 1 + 零的数量 (00+1+00),比方说这个的长度number 是 5,所以我想造一个像 sentence = [length:] 这样的新句子并遍历它,但它不会迭代通过新句,但旧句
有什么想法吗?非常感谢
def reccurent():
sentence = input("Enter the number:")
nums = []
number = ""
for i in sentence:
i = int(i)
if i == 0:
number += str(0)
else:
number += str(1)
if len(number) == 1:
nums.append(number)
number = ""
sentence = sentence[i:]
else:
length = len(number) + len(number) - 1
word = str(sentence[:length])
nums.append(word)
number = ""
sentence = sentence[length:]
print(nums)
reccurent()
结果
输入 number:0011000100
['00110', '1', '0100']
进程已完成,退出代码为 0
应该是
['00110','00100']
谢谢
以下是在提供的代码基础上进行的修改
def reccurent():
sentence = input("Enter the number:")
nums = []
number = ""
# Define parameters to save the cursor offset position
offset = 0
for index, i in enumerate(sentence):
# Skip the value before the cursor
if index < offset:
continue
number += i
if i != "0":
if len(number) == 1:
nums.append(number)
offset = index + 1
else:
length = len(number) * 2 - 1
word = sentence[offset: offset + length]
nums.append(word)
offset += length
number = ""
print(nums)
reccurent()
Enter the number:0011000100
['00110', '00100']
我正在尝试通过这里的 Codewars 挑战之一,指数哥伦布解码器”:https://www.codewars.com/kata/5e4d8a53b499e20016b018a0/swift
想法是采用二进制格式输入:"001000000101111001010001101" -> 00100 000010111 1 00101 0001101
和return解码数字列表:[3, 22, 0, 4, 12]
如果你给出 Sentence 例如 00100 00101,让我们看看你从 2 个前导零开始,然后点击 1,所以第一个结果数是 零的数量 + 1 + 零的数量 (00+1+00),比方说这个的长度number 是 5,所以我想造一个像 sentence = [length:] 这样的新句子并遍历它,但它不会迭代通过新句,但旧句
有什么想法吗?非常感谢
def reccurent():
sentence = input("Enter the number:")
nums = []
number = ""
for i in sentence:
i = int(i)
if i == 0:
number += str(0)
else:
number += str(1)
if len(number) == 1:
nums.append(number)
number = ""
sentence = sentence[i:]
else:
length = len(number) + len(number) - 1
word = str(sentence[:length])
nums.append(word)
number = ""
sentence = sentence[length:]
print(nums)
reccurent()
结果 输入 number:0011000100
['00110', '1', '0100']
进程已完成,退出代码为 0
应该是 ['00110','00100']
谢谢
以下是在提供的代码基础上进行的修改
def reccurent():
sentence = input("Enter the number:")
nums = []
number = ""
# Define parameters to save the cursor offset position
offset = 0
for index, i in enumerate(sentence):
# Skip the value before the cursor
if index < offset:
continue
number += i
if i != "0":
if len(number) == 1:
nums.append(number)
offset = index + 1
else:
length = len(number) * 2 - 1
word = sentence[offset: offset + length]
nums.append(word)
offset += length
number = ""
print(nums)
reccurent()
Enter the number:0011000100
['00110', '00100']