Проблема Каттиса Есть ли лучшее решение, кроме петель Python - PullRequest
0 голосов
/ 20 июня 2019

Я работаю со следующей проблемой (https://open.kattis.com/problems/whowantstoliveforever). Мой код проходит по списку, однако он останавливается на втором правильном результате. И он останавливается. Например, выходы останавливаются на 10 и 0100010.

Ниже мой код.

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...