首页 > 解决方案 > somethingGoesWrong (PYTHON) - 大脑挑战

问题描述

所以,我有这个脚本。

lista = [0,0,0,0,0,0,0,'X',0,0]

jump = 5
lista2 = list()
for i in range(10):
    lista2.append(0)

for lettera in lista:
    # check if list[i] + jump is more then list len, if FALSE put that 'X' in place,
    # if TRUE go throught list and go back to lista[0] and put that 'X' in rightplace.

    # example: len_list = 10 | start_position = list[7] | jump = 5 | final_position = list[2]
    # rember that count start on list[0]

    control = lista.index(lettera) + jump

    if control < len(lista):
        lista2[lista.index(lettera)] = lettera
    else:
        differenza = control - len(lista)
        lista2[differenza] = lettera


print(lista2)

就是这样了。一切正常吗?现在,试试这个:

msg = 'GAr cnff78eq vfGr8L4qetPEsslog32gdsoRK8XSP6x2RHh'

msgSplit = []

msgShuffle = list() #where the final product goes
for i in range(len(msg)): #start with a the same numbers of element(msg)
    msgShuffle.append('*')

def split_msg(msg):
    #split msg letter into list
    for i in msg:
        msgSplit.append(i)

    return msgSplit

def shuffle_msg(lista,lista2):
    for i in lista:
        control = lista.index(i) + 13 #13 here is the 'jump'

        if control < len(lista):
            lista2[lista.index(i)] = i
        else:
            differenza = control - len(lista)
            lista2[differenza] = i

################# T  E  S  T  #################
split_msg(msg)
print(msgSplit)
print('----------------')
shuffle_msg(msgSplit, msgShuffle)
print(msgShuffle)

如果您运行该程序,这是输出:

['G', 'u', 'r', ' ', 'c', 'n', 'f', 'f', 'j', 'b', 'e', 'q', ' ', 'v', 'f', ' ', '5', 'G', 'r', '8', 'L', '4', 'q', 'e', 't', 'P', 'E', 's', 'P', 'k', '8', 'h', 't', 'q', 'j', 'h', 'R', 'K', '8', 'X', 'S', 'P', '6', 'x', '2', 'R', 'H', 'h']
----------------
['G', 'R', 'K', ' ', 'X', 'S', 'f', '6', 'x', '2', 'e', 'H', '', 'v', '', '', '5', '', '', '8', 'L', '4', '', '', 't', 'P', 'E', 's', '', 'k', '', 'h', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

第一个是拆分为列表(msgSplit)的字符串,第二个是在我将它传递给您可以在脚本中看到的函数(shuffle_msg)之后的msgSplit。

现在 80% 的元素是 ''。所以有什么问题???该函数只需移动列表中的元素。

谢谢!

就是这样:

@ = list[4]

so now we want to move @ forward for 3 position:
[o,o,o,o,@,o] start @=list[4]
[o,o,o,o,o,@] +1 @=list[5]
[@,o,o,o,o,o] +2 the list is finish so we continue from @=list[0]
[o,@,o,o,o,o] +3 finish on @=list[1]

标签: pythonlist

解决方案


所以看起来你想右旋转数组。您可以稍微修改您的代码:

def shuffle_msg(lista, lista2):
    # Using indexes, not values
    for i in range(len(lista)):
        # control is the target index in the other list
        control = i + 13 #13 here is the 'jump'

        # If not out of bounds, insert it
        if control < len(lista):
            lista2[control] = lista[i]
        # Else, wrap around
        else:
            differenza = control - len(lista)
            lista2[differenza] = lista[i]

您可以使用模数简化这一点:

def shuffle_msg2(lista, lista2):
    # Using enumerate instead of range
    for i, val in enumerate(lista):
        lista2[(i + 13) % len(lista)] = val

您可以使用列表切片进一步简化:

msgShuffle = msg[-13:] + msg[:-13]

还有其他方法。


推荐阅读