why do well when you can just skate?

The Josephus Problem: It’s a way of thinking

danvk tried to solve the Josephus Problem in Perl, Python, and Ruby, comparing each one. He had some pretty ignorant things to say about each.

There’s no reason to use object orientation here. It doesn’t help the problem or organize the data any better. But, it’s cargo cult to make everything a class. Even then, why all the bells and whistles? To you really need string overloading here?

Peter Scott has a natural approach using just the stuff from Learning Perl:

danvk complains that he doesn’t like Perl’s object-oriented framework, but doesn’t realize Larry stole it from Python, the language he prefers. The joke is on him.

Object orientation is a way of thinking about things, not a particular technology. It’s not about translating a program from another language line-by-line using the same techniques.

But, it’s not like the non-OO Perl solutions out there are any better. A Perl 5 Rosetta Code example spends a lot of time shifting array elements around so that the head of the array is the prisoner to remove:

This is a translation of the Perl 6 solution to the same problem where the programmer was overly fond of the rotate method.

All of these solutions, including Peter’s, misses the point of the original problem. Josephus wanted to cheat so that he’d be last. But, he didn’t want to be last alone so he wanted to arrange it so a friend would be left with him. That is, the problem isn’t who is last, where where the last two should stand (and maybe, who should start).

Instead of modifying the array, you can mark soldiers as dead. In this solution, I represent each soldier as a tuple that has their name, their initial position, and the order in the killing. I’ll loop through that until I’ve dispatched enough soldiers. Afterward, I can look at that array to see what happened.