Fail while daring greatly def josephus(n, k): The ancient world “� when men were made of iron and their ships were made of wood � ” could occasionally also be an entertainingly violent place, at least if we believe historical docudramas of swords and sandals such as “300”, “Spartacus” and “Game of Thrones” marred by dust and sweat and blood. (Records from those eras are spotty at best, so shut up, he explained.) During one particularly memorable incident, a group of extremist zealots (yes, Lana, literally) found itself surrounded by overwhelming Roman forces. To avoid capture and death by crucifixion that would have been as humiliating as it was slow, these men chose to commit mass suicide in a way that prevented any one of them from changing his mind. The zealots arranged themselves in a circle and used lots to choose a step size k. From the first man, repeatedly count k men ahead, kill that man and remove his corpse from this grim circle. Being normal people instead of computer scientists, they always start counting from one, not zero. (Their reaction to the idea of zero would have been akin to me asking you in our time what letter comes before the letter A in the English alphabet.) This deadly game of eeny-meeny-miney-moe continues until the last man standing falls on his own sword to complete the game. Josephus would very much prefer to be this last man since he has other ideas of surviving. Help him survive with a function that, given n and k, returns the list of the execution order so that these men know which places let them be the survivors who get to walk away from this grim circle. A cute mathematical solution can instantly determine the final survivor for k = 2. Unfortunately for us k can get arbitrarily large, even larger than the original number of men… if only to briefly excite us cold and timid souls, hollow men without chests, the rictus of our black lips gaped in grimace that sneers at the strong men who once stumbled.
Fail while daring greatly
def josephus(n, k):
The ancient world “� when men were made of iron and their ships were made of wood � ” could occasionally also be an entertainingly violent place, at least if we believe historical docudramas of swords and sandals such as “300”, “Spartacus” and “Game of Thrones” marred by dust and sweat and blood. (Records from those eras are spotty at best, so shut up, he explained.) During one particularly memorable incident, a group of extremist zealots (yes, Lana, literally) found itself surrounded by overwhelming Roman forces. To avoid capture and death by crucifixion that would have been as humiliating as it was slow, these men chose to commit mass suicide in a way that prevented any one of them from changing his mind. The zealots arranged themselves in a circle and used lots to choose a step size k. From the first man, repeatedly count k men ahead, kill that man
and remove his corpse from this grim circle. Being normal people instead of computer scientists, they always start counting from one, not zero. (Their reaction to the idea of zero would have been akin to me asking you in our time what letter comes before the letter A in the English alphabet.)
This deadly game of eeny-meeny-miney-moe continues until the last man standing falls on his own sword to complete the game. Josephus would very much prefer to be this last man since he has other ideas of surviving. Help him survive with a function that, given n and k, returns the list of the
execution order so that these men know which places let them be the survivors who get to walk away from this grim circle. A cute mathematical solution can instantly determine the final survivor for k = 2. Unfortunately for us k can get arbitrarily large, even larger than the original number of
men… if only to briefly excite us cold and timid souls, hollow men without chests, the rictus of our black lips gaped in grimace that sneers at the strong men who once stumbled.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images