Introduction to Nim Game

Sort by

recency

|

76 Discussions

|

  • + 0 comments

    C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star :) )

    std::string nimGame(std::vector<int> const & _piles){
        auto binSum = 0;
        for(auto const & size : _piles){
            binSum ^= size;
        }
        return binSum ? "First" : "Second";
    }
    
  • + 0 comments

    the question is very good, but the explainnation for sample second input seems not correct

  • + 0 comments

    https://en.wikipedia.org/wiki/Nim#Proof_of_the_winning_formula

  • + 0 comments
    def nimGame(pile):
        res = 0
        for p in pile:
            res ^= p
        
        return 'Second' if res == 0 else 'First'
    
  • + 0 comments

    Javascript one-liner:

    const nimGame = p => p.reduce((a, v) => a ^= v) ? 'First' : 'Second'