机器人故障前最后输出语句解码异常问题求助
求助:解析Cypher指令串获取机器人最后输出的有效语句
我拿到了一段西班牙语的Cypher技术测试题,现在卡在了解决步骤上。需求是找出机器人瘫痪前显示的最后一条语句——机器人数日前故障,导致它瘫痪的指令序列已存储在RAM中,具体指令串为:
UNDDNNUUNNNUUUUNNNNUUUUUNNUUUUUNNUUUUUNNNNSDDNNUUNNNUNDNNSUNUUUNNUUUUUNNNNUUUUNNNDDDNNUUUUUUNNDDDNNUUUUUUNNUNNNDNNUUUUUUNNS
通信系统规则
内存为线性结构,仅可向右遍历,每个存储槽存储0-9的数字(默认值为0)。指针初始指向第一个存储槽,用于修改存储值。
指令定义:
U:将当前存储槽的值加1(循环规则:9→0)D:将当前存储槽的值减1(循环规则:0→9)N:将指针向右移动一个存储槽S:输出当前内存内容,随后将指针重置至起始位置,并将所有存储槽的值重置为0
编码规则
字母映射为数字(A=1,…,Z=26),字母之间用0分隔。
示例验证
测试指令串:DDNNUNUUUUNNUUNUUUNNUS,最终内存状态为:8 0 1 5 0 1 2 0 1,屏幕输出为:HOLA。
我的代码实现
我编写了如下Python代码,在上述测试案例中可正常运行,输出正确结果HOLA,但使用长指令串运行时,解码结果为无意义的词汇。
instr = "DDNNUNUUUUUNNUNUUNNUS" #instr = "UNDDNNUUNNNUUUUNNNNUUUUUNNUUUUUNNUUUUUNNNNSDDNNUUNNNUNDNNSUNUUUNNUUUUUNNNNUUUUNNNDDDNNUUUUUUNNDDDNNUUUUUUNNUNNNDNNUUUUUUNNS" alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def group_numbers(memory): groups = [] current_num = "" for val in memory: if val == 0: if current_num: groups.append(int(current_num)) current_num = "" else: current_num += str(val) if current_num: groups.append(int(current_num)) return groups def decode(groups): return "".join(alphabet[n - 1] for n in groups) numbers = [] slot = 0 for ch in instr: if ch == 'U': slot += 1 if slot == 10: slot = 0 if ch == 'D': slot -= 1 if slot == -1: slot = 9 if ch == 'N' or ch == 'S': numbers.append(slot) slot = 0 print(numbers) print(group_numbers(numbers)) print(decode(group_numbers(numbers)))
长指令串运行异常情况
长指令串运行后生成的原始数字序列为:
[1, 8, 0, 2, 0, 0, 1, 5, 0, 1, 4, 0, 2, 5, 0, 2, 0, 0] [8, 0, 2, 0, 0, 1, 9, 0] [1, 3, 0, 1, 4, 0, 1, 0, 0, 2, 3, 0, 7, 0, 6, 0, 7, 0, 2, 6, 0, 1, 0, 0, 1, 9, 0, 6, 0]
分组解析后的数字为:
[18, 20, 15, 14, 20] [8, 20, 19] [13, 14, 10, 23, 7, 6, 7, 26, 10, 19, 6 ]
最终解码得到的字母为:RTONYT HTS MNJWGFGZJSF,均为无意义词汇。
我怀疑可能不存在有效结果,或者我的代码存在未发现的问题,希望得到技术指导。
内容的提问来源于stack exchange,提问作者fsalazar_sch
相关产品推荐
相关产品推荐

