id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

david.white@denison.edu cs112fall2015 > nim
prev  |  next  |  chance

In the game of Nim, there is a pile of cigars (or they could be squirrels). You and another player take turns removing cigars from the pile. On each turn you can take 1, 2, or 3. The player to take the last cigar wins. Write the function nim(n), which returns the number of the n cigars you should take to guarantee an eventual win regardless of what the other player does. If there is no way to guarantee a win, just take 1 cigar -- leaving as big a pile as possible so maybe the opponent will make a mistake.


nim(3) → 3
nim(7) → 3
nim(8) → 1

...Save, Compile, Run (ctrl-enter)

def nim(n):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 300 Post-solution available

Copyright Nick Parlante 2017 - privacy