【发布时间】: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