Cypher技术测试

编程语言 2026-07-11

我发现了这个技术测试,但我解不出。原题是西班牙语的。

问题是要找出机器人在停止工作前显示的最后一句话。

机器人几天前坏了,原因不明。一个指令序列让它失效,其最终指令被存储在RAM中:

UNDDNNUUNNNUUUUNNNNUUUUUNNUUUUUNNUUUUUNNNNSDDNNUUNNNUNDNNSUNUUUNNUUUUUNNNNUUUUNNNDDDNNUUUUUUNNDDDNNUUUUUUNNUNNNDNNUUUUUUNNS

** 通信系统**

  • 内存是线性的,只能向右遍历。
  • 每个槽位保存一个从0 到9 的数字(默认值 = 0)。
  • 一个指针从第一个槽位开始,用于修改数值。

指令:

  • U: 增加当前槽位数值(循环:9 → 0)
  • D: 减少当前槽位数值(循环:0 → 9)
  • N: 将指针向右移一个槽位
  • S: 输出内存,然后将指针重置到起点,所有槽位都设为0

编码:

  • 字母映射为数字(A = 1, ..., Z = 26)
  • 字母之间用0 分隔

示例基本信息 Set: DDNNUNUUUUNNUUNUUUNNUS 结果在内存中:8 0 1 5 0 1 2 0 1 屏幕输出:HOLA

所以我做了这个:

instr = "DDNNUNUUUUUNNUNUUNNUS"
#instr = "UNDDNNUUNNNUNUUUUUNNUNUUUUNNUUNUUUUUNNUUNNNSDDNNUUNNNUNDNNSUNUUUNNUNUUUUNNUNNNUUNUUUNNDDDNNUUUUUUNNDDDNNUUNUUUUUUNNUNNNUNDNNUUUUUUNNS"

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)))

带示例的代码正在工作 输入 : DDNNUNUUUUNNUUNUUUNNUS --> 输出: HOLA

但对于较长的字符串,输出就不同了

#original 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]

#numbers prepared to be parsed
[18, 20, 15, 14, 20] 
[8, 20, 19]
[13, 14, 10, 23, 7, 6, 7, 26, 10, 19, 6 ]

#output letters
RTONYT 
HTS 
MNJWGFGZJSF 

#non coherence words


#If you run the code with string = "DDNNUNUUUUNNUUNUUUNNUS"
#it results the array : [8, 0, 1, 5, 0, 1, 2, 0, 1]
#So the letters are 8,15,12,1 --> H,O,L,A

在这一切之前....

问题是,使用长字符串时会得到没有连贯性的单词 我怀疑没有有效的结果,或者还有我没发现的地方?

解决方案

你差不多快完成了。下面是一些陷阱要点:

  • 你现在以相同的方式处理 "S" 和 "N"。你应该对 "S" 指令在且仅在 当前(最后一个)槽位被修改但尚未追加时执行 numbers.append(slot)
  • 如果原始数字看起来像 [2, 0, 0],那么它们应该被合并为 [20, 0],尽管文档中并未清楚地指出。否则代码10 (J) 和20 (T) 将无法正确处理。

那么请你尝试以下内容:

#!/usr/bin/python3

#instr = "DDNNUNUUUUUNNUNUUNNUS"
instr = "UNDDNNUUNNNUNUUUUUNNUNUUUUNNUUNUUUUUNNUUNNNSDDNNUUNNNUNDNNSUNUUUNNUNUUUUNNUNNNUUNUUUNNDDDNNUUUUUUNNDDDNNUUNUUUUUUNNUNNNUNDNNUUUUUUNNS"

def group_numbers(memory):
    groups = []
    current_num = 0
    for i in range(len(memory)):
        if memory[i] != 0 or i < len(memory) - 1 and memory[i + 1] == 0:
            current_num = current_num * 10 + memory[i]
        else:
            groups.append(current_num)
            current_num = 0
    if current_num:
        groups.append(int(current_num))
    return(groups)

def decode(groups):
    return("".join([chr(i + 0x40) for i in groups]))

numbers = []
slot = 0

for ch in instr:
    if ch == 'U':
        slot = (slot + 1) % 10
    elif ch == 'D':
        slot = (slot + 9) % 10
    elif ch == 'N':
        numbers.append(slot)
        slot = 0
    elif ch == 'S':
        if slot > 0:
            numbers.append(slot)
        print(numbers)
        print(group_numbers(numbers))
        print(decode(group_numbers(numbers)))
        numbers = []
        slot = 0

[说明]

  • 条件 if memory[i] != 0 or i < len(memory) - 1 and memory[i + 1] == 0 满足时若下列任一条件为真:

  • 当前数字不为0。

  • 当前数字为0 但下一个也是0。

那么当前数字将被追加到前一个数字上,而不发送。 * chr(i + 0x40) 将数值 i 映射到相应的字母。 你不需要准备 alphabet 变量。 * slot = (slot + 1) % 10 自增后返回最右边的数字。slot = (slot + 9) % 10 自减后将以同样方式工作。

希望这对你有帮助。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章