Kattis问题除了Python循环,还有其他更好的解决方案吗?

时间:2019-06-20 17:28:32

标签: python python-3.x python-2.7 list

我正在处理以下问题(https://open.kattis.com/problems/whowantstoliveforever)。我的代码遍历列表,但是在第二个正确的结果处停止。并且它停止循环。例如,输出停在100100010

下面是我的代码。

import sys


def liveForever(input_list):
    if len(set(input_list)) == 1:
        return False
    return True


def get_bit(bits, i):
    if 0 <= i < len(bits):
        return int(bits[i])
    else:
        return 0


def print_result(boolean):
    print("LIVES" if boolean else "DIES")


num_cases = int(sys.stdin.readline().strip())
for i in range(num_cases):
    _list = []
    case = sys.stdin.readline().strip()
    for char in case:
        _list.append(char)
    output_list = []
    for index in range(len(_list)):
        if (get_bit(_list, index-1) == 0 and get_bit(_list, index+1) == 0) or (get_bit(_list, index-1) == 1 and get_bit(_list, index+1) == 1):
            output_list.append(0)
        elif(get_bit(_list, index-1) == 0 and get_bit(_list, index+1) == 1) or (get_bit(_list, index-1) == 1 and get_bit(_list, index+1) == 0):
            output_list.append(1)
    print(output_list)
    print_result(liveForever(_list))

还有其他方法可以解决此问题吗?而不是做for index in range(len(_list)):吗?

0 个答案:

没有答案