Soluție HackerRank pentru Nimble Game. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Nimble Game

Cerinta completa

Two people are playing Nimble! The rules of the game are:

  • The game is played on a line of [Expresie matematică indisponibilă în copia arhivată] squares, indexed from [Expresie matematică indisponibilă în copia arhivată] to [Expresie matematică indisponibilă în copia arhivată]. Each square [Expresie matematică indisponibilă în copia arhivată] (where [Expresie matematică indisponibilă în copia arhivată]) contains [Expresie matematică indisponibilă în copia arhivată] coins. For example:
    nimble.png
  • The players move in alternating turns. During each move, the current player must remove exactly [Expresie matematică indisponibilă în copia arhivată] coin from square [Expresie matematică indisponibilă în copia arhivată] and move it to square [Expresie matematică indisponibilă în copia arhivată] if and only if [Expresie matematică indisponibilă în copia arhivată].
  • The game ends when all coins are in square [Expresie matematică indisponibilă în copia arhivată] and nobody can make a move. The first player to have no available move loses the game.

Given the value of [Expresie matematică indisponibilă în copia arhivată] and the number of coins in each square, determine whether the person who wins the game is the first or second person to move. Assume both players move optimally.

Input Format

The first line contains an integer, [Expresie matematică indisponibilă în copia arhivată], denoting the number of test cases.
Each of the [Expresie matematică indisponibilă în copia arhivată] subsequent lines defines a test case. Each test case is described over the following two lines:

  1. An integer, [Expresie matematică indisponibilă în copia arhivată], denoting the number of squares.
  2. [Expresie matematică indisponibilă în copia arhivată] space-separated integers, [Expresie matematică indisponibilă în copia arhivată], where each [Expresie matematică indisponibilă în copia arhivată] describes the number of coins at square [Expresie matematică indisponibilă în copia arhivată].

Constraints

  • [Expresie matematică indisponibilă în copia arhivată]
  • [Expresie matematică indisponibilă în copia arhivată]
  • [Expresie matematică indisponibilă în copia arhivată]

Output Format

For each test case, print the name of the winner on a new line (i.e., either [Expresie matematică indisponibilă în copia arhivată] or [Expresie matematică indisponibilă în copia arhivată]).

Sample Input

2
5
0 2 3 0 6
4
0 0 0 0

Sample Output

First
Second

Explanation

Explanation for [Expresie matematică indisponibilă în copia arhivată] testcase:
The first player will shift one coin from [Expresie matematică indisponibilă în copia arhivată] to [Expresie matematică indisponibilă în copia arhivată]. Hence, the second player is left with the squares [Expresie matematică indisponibilă în copia arhivată]. Now whatever be his/her move is, the first player can always nullify the change by shifting a coin to the same square where he/she shifted it. Hence the last move is always played by the first player, so he wins.

Exlanation for [Expresie matematică indisponibilă în copia arhivată] testcase:
There are no coins in any of the squares so the first player cannot make any move, hence second player wins.


Limbajul de programare folosit: python3

Cod:

#!/bin/python3

import math
import os
import random
import re
import sys
from functools import reduce

# Complete the nimbleGame function below.
def nimbleGame(s):
    if len(s) > 1:
        res = 0
        for ind, el in enumerate(s):
            if el%2 == 1:
                res ^= ind
    else:
        return 'Second'

    if res == 0:
        return 'Second'
    else:
        return 'First'


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input())

    for t_itr in range(t):
        n = int(input())

        s = list(map(int, input().rstrip().split()))

        result = nimbleGame(s)

        fptr.write(result + '\n')

    fptr.close()

Scor obtinut: 1.0

Submission ID: 464605371

Link challenge: https://www.hackerrank.com/challenges/nimble-game-1/problem

Nimble Game