search

fishing kata

deltin55 8 hour(s) ago views 53

  Fishing Kata Solution


  Problem Statement

In this kata, two players take turns fishing from a pond containing n fish. Each player can catch 1, 2, or 3 fish per turn. The player who catches the last fish wins. Determine if the first player can always win given the initial number of fish.


  Rules




Players alternate turns, starting with Player 1.
A player can catch 1, 2, or 3 fish per turn.
The player who catches the last fish wins.


  Approach

The key insight is to recognize this as a variant of the classic Nim game. The optimal strategy involves forcing the opponent into a losing position. If the number of fish n is a multiple of 4, Player 1 will lose if Player 2 plays optimally. Otherwise, Player 1 can always win by reducing the fish count to a multiple of 4 on each turn.


  Solution Code


  def fishing_kata(n):
    return n % 4 != 0


  Explanation


Base Case Analysis:
If n = 1, Player 1 catches the fish and wins.
If n = 4, no matter how many fish Player 1 catches (1-3), Player 2 can catch the remaining and win.


General Strategy:
If n % 4 != 0, Player 1 can always catch (n % 4) fish, leaving n - (n % 4) (a multiple of 4) for Player 2. This forces Player 2 into a losing position.
If n % 4 == 0, Player 1 has no winning move, and Player 2 will win with optimal play.




  Test Cases

| Input (n) | Output | Explanation |
|-----------|--------|-------------|
| 1         | True   | Player 1 wins immediately. |
| 4         | False  | Player 2 wins. |
| 5         | True   | Player 1 catches 1, leaving 4 for Player 2. |
| 6         | True   | Player 1 catches 2, leaving 4 for Player 2. |
| 7         | True   | Player 1 catches 3, leaving 4 for Player 2. |
| 8         | False  | Player 2 wins. |


  Note

This solution assumes the problem follows the classic Nim-like rules. If the game has additional constraints (e.g., different allowed catches or winning conditions), adjust the logic accordingly.
like (0)
deltin55administrator

Post a reply

loginto write comments
deltin55

He hasn't introduced himself yet.

310K

Threads

12

Posts

1110K

Credits

administrator

Credits
115350

Get jili slot free 100 online Gambling and more profitable chanced casino at www.deltin51.com