【问题标题】:From binary to unary (Codingame.com)从二进制到一元 (Codingame.com)
【发布时间】:2018-09-10 20:41:26
【问题描述】:

这是codingame chucknorris游戏的一部分,当我们想从一个字符串开始,然后将它转换为“一元”,一个只有0和空格的代码。 这是我的代码,我的问题是没有任何东西可以打印出来。 前任 : CC => 10000111000011:

0 0 (one 1)
00 0000 (four 0)
0 000 (three 1)
00 0000 (four 0)
0 00 (two 1)

CC 给出:0 0 00 0000 0 000 00 0000 0 00

import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.*

message = input()
# message to binary (01001)
bimsg = str([ bin(ord(ch))[2:].zfill(8) for ch in message ])[2:-2]

# function
to_chuck = ''
for n in bimsg:
    index = bimsg.index(n)
    if index >= 1 and bimsg[index] == bimsg[index-1]:
        to_chuck+='0'
    else: 
        if n == 1:
            to_chuck+='0 0'
        elif n == 0:
            to_chuck+='00 0'

print(to_chuck)

【问题讨论】:

  • “一元”和“0 和空格”不能一起使用。一元意味着只有一种类型的字符,而不是两种不同的字符。但更重要的是,您可能应该尝试更详细地解释对话算法。目前并不容易弄清楚。另外,你的代码有什么问题?输出不正确吗?它会抛出错误吗?

标签: python python-3.x algorithm binary ascii


【解决方案1】:

您的代码中有 4 个错误。

  1. 您将输入字符串转换为二进制的方式不正确。您将每个字符填充为 8 位而不是 7 位,并且由于您在列表上调用 str,因此具有多个字符的字符串将在字符之间使用逗号。正确的变换是

    bimsg = ''.join(format(ord(x), 'b').zfill(7) for x in message)
    
  2. index = bimsg.index(n) 将始终为您提供输入中n第一次 出现的索引。如果您的输入是1010bimsg.index('1') 将始终返回 0,bimsg.index('0') 将始终返回 1。这不是您想要的。

    要获得正确的索引,请使用enumerate 函数:

    to_chuck = ''
    for index, n in enumerate(bimsg):
        if index >= 1 and bimsg[index] == bimsg[index-1]:
            to_chuck+='0'
        else: 
            if n == 1:
                to_chuck+='0 0'
            elif n == 0:
                to_chuck+='00 0'
    
  3. 由于bimsg 是一个字符串,n 也将是一个字符串。当您执行if n == 1if n == 0 时,您是在将字符串与数字进行比较——其结果总是False。您必须将 n 与字符串进行比较:

    to_chuck = ''
    for index, n in enumerate(bimsg):  # use enumerate here
        if index >= 1 and bimsg[index] == bimsg[index-1]:
            to_chuck+='0'
        else: 
            if n == '1':
                to_chuck+='0 0'
            elif n == '0':
                to_chuck+='00 0'
    
  4. 您的代码没有在此处的零块和一块之间放置任何空格:

    if n == '1':  # changed 1 to '1' here
        to_chuck+='0 0'
    elif n == '0':  # changed 0 to '0' here
        to_chuck+='00 0'
    

    您在此处添加的零与to_chuck 末尾的零融合在一起,从而导致输出不正确。要解决这个问题,只需添加一个空格:

    to_chuck = ''
    for index, n in enumerate(bimsg):
        if index >= 1 and bimsg[index] == bimsg[index-1]:
            to_chuck+='0'
        else: 
            if n == '1':
                to_chuck+=' 0 0'  # added leading space here
            elif n == '0':
                to_chuck+=' 00 0'  # added leading space here
    to_chuck = to_chuck[1:]  # get rid of the leading space
    

现在我们得到正确的输出:

print(to_chuck)
# 0 0 00 0000 0 000 00 0000 0 00

【讨论】:

  • 我删除了我的答案,尽管它对于帖子中的问题是正确的,解决了打印中没有问题的问题。但是,测试你的代码,它也没有通过练习的要求:codingame chuck norris
  • b.t.w 您还应该使用更直接的 dtype 转换:bimsg = ''.join(format(ord(x), 'b') for x in message)
  • @mr_mo 我不认为输入转换是问题的一部分。我修好了,现在所有的测试用例都通过了。
【解决方案2】:
def firstBlock(x):
    if x == "1":
        return "0"
    return "00"

def getBlock(x, counter):
    return firstBlock(x) + " " + ("0" * counter)

message = input()
result = ""

binaryString = ""
for x in message:
    binaryChar = format(ord(x), 'b')
    paddedChar = binaryChar.zfill(7)
    binaryString += paddedChar

counter = 1
currentChar = binaryString[0]

for x in binaryString[1:]:
    if x == currentChar:
        counter += 1
        continue

    result += getBlock(currentChar, counter) + " "
    counter = 1
    currentChar = x

result += getBlock(currentChar, counter)
print(result)

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 2012-01-29
    相关资源
    最近更新 更多