> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Chalkboard XOR Game Python Solution with Tests

> Tested Python solution for LeetCode 810 with 32 pytest cases. Generate a practice environment with lcpy.

LeetCode 810, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Bit Manipulation](/catalog/topics/bit-manipulation), Brainteaser, [Game Theory](/catalog/topics/game-theory). [View on LeetCode](https://leetcode.com/problems/chalkboard-xor-game/description/).

Generate this problem as a practice environment: tested reference solution, 32 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 810   # by problem number
lcpy gen -s chalkboard_xor_game   # by problem name
```

## Problem

You are given an array of integers `nums` represents the numbers written on a chalkboard.

Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is `0`.

Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to `0`, then that player wins.

Return `true` if and only if Alice wins the game, assuming both players play optimally.

### Examples

```
Input: nums = [1,1,2]
Output: false
Explanation:
Alice has two choices: erase 1 or erase 2.
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
If Alice erases 2 first, now nums become [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.
```

```
Input: nums = [0,1]
Output: true
```

```
Input: nums = [1,2,3]
Output: true
```

### Constraints

* 1 \<= nums.length \<= 1000
* 0 \<= nums\[i] \< 2^16

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/chalkboard_xor_game/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/chalkboard_xor_game/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from functools import reduce
from operator import xor


class Solution:
    # Time: O(n)
    # Space: O(1)
    def xor_game(self, nums: list[int]) -> bool:
        # Alice loses only when the count is odd and the total XOR is nonzero:
        # with an even count she can always mirror Bob's erasures (two copies of
        # every value would XOR to 0), and a zero XOR wins on the spot.
        return len(nums) % 2 == 0 or reduce(xor, nums, 0) == 0
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.