You and some buddies are going bowling. There are a total of N bowlers. However, there are only N-1 chairs to sit in. The solution is simple: whoever's turn it currently is doesn't get a chair. Then when their turn is over, they sit in the chair of the person that goes next.

Lets take an example. Say You are named A, and your four friends are named B, C, D, and E. Every player moves in alphabetical order, so you get to go first. Since there are 5 players, there are only 4 seats. Your friends sit in the four seats in this order:


You go, and yay you get a strike! It's B's turn next, so you sit in his chair. Now it looks like this:


B goes. Gutterball! Then he sits in C's spot, and C goes next turn.


then C sits in D's chair.


and D sits in E's chair


and lastly, E sits in your chair.


You'll notice that now everybody's seat is (pseudo) shuffled. You must find out, after X turns, who will be sitting where?


Your program must take two inputs from the user, a string and a number. No prompts are needed. The string will be 1-51 alphabetic characters (B-Z and a-z) with no repeats. This represents the order your friends chose to sit. There will be no uppercase A because that is you, and you always go first. The number will be the total number of rounds (not games) that you and your friends play. This number will be positive and reasonably sized (less than 1000).


Your program must print out the order your friends are sitting in after X turns, and whose turn it is. So for example, if after X turns the order was BEDGCAHF and it was Z's turn, your program must print exactly this:

It is Z's turn.

Here are a few sample input and outputs.

input: E, 4 
It is A's turn.

input: E, 5 
It is E's turn.

input: Bb, 2
It is b's turn.

input: dgOPZXKDQYioHflFhpqzUsSaeILwckVNEtGTCJBvnruRyWMmjxb, 999
It is L's turn.


  • Everybody goes in alphabetical order, with capital letters taking precedence over lower case.

  • This is code-golf, so standard loopholes apply, and submissions are scored in bytes.

  • 1
    \$\begingroup\$ I have spent hours working on this for brainflak. I just noticed that I was doing it wrong. \$\endgroup\$
    – user63187
    Commented Jul 28, 2017 at 17:42
  • 1
    \$\begingroup\$ {({}[()])({}<({}<(([])<{{}({}<>)<>([])}{}<>>)<>>)<>{({}[()]<({}<>)<>>)}{}<>>)} \$\endgroup\$
    – user63187
    Commented Jul 28, 2017 at 17:43
  • \$\begingroup\$ It shuffles it and the first item on stack is the answer. \$\endgroup\$
    – user63187
    Commented Jul 28, 2017 at 17:44
  • \$\begingroup\$ That was my surprise for you :P \$\endgroup\$
    – user63187
    Commented Jul 28, 2017 at 17:51

12 Answers 12


Pyth, 39 38 bytes

L@+\ASzbuXGyhHyHQzpyQ"It is ""'s turn.

This is based around repeated applications of the find and replace operation, X. The first bit defines a lookup function, y, which finds the bth player in the player order. Then, we repeatedly perform substitutions to find the final seating order, and finally print out whose turn it is.

Amusingly, the code to find the final seating order is shorter (18 bytes) than the code to print whose turn it is (21 bytes).

The code takes the seating string on the first line of STDIN, and the number of turns on the second.



L@+\ASzbuXGyhHyHQzpyQ"It is ""'s turn.
                                          z = input()
                                          Q = eval(input())

L                                         def y(b): return
  +\ASz                                    "A" + sorted(z)
 @     b                                  (               )[b]
        u       Qz                        reduce for H in range(len(Q)),
                                          G starts as z.
         XGyhHyH                          replace in G y(H+1) with y(H).
                  pyQ"It is ""'s turn.    Print out whose turn it is.
  • \$\begingroup\$ @Sp3000 Thanks for the catch. \$\endgroup\$
    – isaacg
    Commented May 4, 2015 at 5:16
  • \$\begingroup\$ Never use %, if you only insert one thing. Even ++ would save one byte, but the best way (2 bytes) is using p: pyQ"It is ""'s turn \$\endgroup\$
    – Jakube
    Commented May 4, 2015 at 8:08
  • \$\begingroup\$ Oops. Missed the point at the end. So ++ has the same byte-count as %, and p only saves 1 byte. \$\endgroup\$
    – Jakube
    Commented May 4, 2015 at 8:28

CJam, 49 45 43 bytes

It is "\"'s turn."

I think this works. It just runs the algorithm as is.

Try it online.


l                       Read line (initial seating order)
_'A+$                   Copy, add "A" and sort to give bowling order

ri{          }*         Do <number of turns> times...
   :L                     Save bowling order as L
     2<(                  Get next and current bowlers
        er                Replace next with current in seating
          L(+             Push bowling order again and update (rotate)

1<                      Get current bowler from start of bowling order
It is "\"'s turn."      Output message

Python 3, 110

print(s+"\nIt is %s's turn."%y)

An optimized version of Sp3000's solution using replace. The list S cycles though the letters present in order. We perform repeated replacements in the given string of each character of S by the previous one.

  • \$\begingroup\$ Well played, every time :) \$\endgroup\$
    – Sp3000
    Commented May 4, 2015 at 5:54
  • \$\begingroup\$ @Sp3000 I used your solution though. \$\endgroup\$
    – xnor
    Commented May 4, 2015 at 5:55

Pyth, 37 bytes

uXGK<.<+\ASzH2)QzpeK"It is ""'s turn.

Online Demonstration: Pyth Compiler/Executor

The algorithm is kinda based on @isaacg's solution. Like him I start with the initial seat-order and repeatedly use the replace-functionality of X to replace the next player with the current player.

But unlike his implementation, which replaces the char of the next player by the current player in the seating order, I use it in a more broad way. I replace each char of the current player by the next player and each char of the next player by the current player. This is accomplished by passing both players as second arg and omit the third arg (XG"ab") instead of XG"a""b"). Since the current player is not part of the string (he is playing), the first replacement has no effect at all. But it allows me to generate both players at the same time, while @isaacg has to generate them individually.

Another crazy new feature I use is the assignment operator. Until recently =N1 got translated into N = 1, which was executed with Python. But nowadays it compiles to assign('N',1). This function assigns N with 1 and returns the value (but doesn't print it). This allows saving intermediate results, which occur for instance in a reduce operation. Using this I was able to store the pair of players, which changed positions last, and print second player.

Detailed Explanation

                      implicit: z = input string, Q = input number
u              Qz     reduce for H in range(Q), start with G = z
                        update G with:
       +\ASz              "A" + sorted(z)
     .<     H             cyclic shifted by H
    <        2            get the first 2 elements (current + next player)
   K                      store the result in K
 XG           )           replace next player by current player in G
                      implicit print 

peK"It is ""'s turn.  print "It is" + K[-1] (current player) + "'s turn."

Clip 10, 59 56 bytes

[t{)k[qa)qglqtg(lqt}wx)nyN"It is "gnyt"'s turn.."`]s,x'A


[t{)k[qa)qglqtg(lqt}wx)nyN"It is "gnyt"'s turn.."`]s,x'A
It is L's turn.


The first input is the list of players, assigned to the variable x.

The second input is the number of turns, which the program obtains with ny.

[t                                        ]s,x'A .-t is the sorted list of players including A-.
  {                                      `       .-Print the following:            -.
    k[q            }wx)ny                        .-In each round (q is all the previous rounds)-.
       a)q                                       .-Replace                         -.
          glqt                                   .-the next player                 -.
              g(lqt                              .-with the previous player        -.

                     N                           .-Also print a newline            -.
                      "It is "    "'s turn.."
                              gnyt               .-The ny'th player in t           -.

Thanks to Sp3000 for the idea of using "replace".


Python 3, 128 bytes

print(L+"\nIt is %s's turn."%S[i-1])

Takes two lines of input via STDIN — initial seating order then number of turns.

This is basically the same search-and-replace idea as my CJam solution. The only tricky part is that we stick A at the back of the bowling order and make our index i the index of the next bowler, thus taking advantage of indexing by -1 and avoiding IndexErrors.

This is a few bytes shorter in Python 2, but I'm posting Python 3 for comparison with OP's solution.

  • \$\begingroup\$ What improvements does python 2 have? I see that input on line 1 becomes raw_input (+4) and int(input()) on line 4 becomes input (-4) so that cancels out. Then remove parentheses from print and add a space for a total of 127. Am I missing something? \$\endgroup\$
    – DJMcMayhem
    Commented May 5, 2015 at 0:07
  • \$\begingroup\$ @DJMcMayhem You forgot the parentheses for exec \$\endgroup\$
    – Sp3000
    Commented May 5, 2015 at 3:20

JavaScript (ES6) 116

116 bytes as program with I/O via popup window. 114 as a testable function.

Run code snippet in Firefox to test.

// as a program with I/O
It is ${p}' turn`)

// as a function with 2 parameters, returning output as a 2 lines string
It is ${p}' turn`)}

// Test suite
['CEBD',5], ['E', 4],['E',5],['Bb', 2],
['dgOPZXKDQYioHflFhpqzUsSaeILwckVNEtGTCJBvnruRyWMmjxb', 999]];


test.forEach(([p,x])=>Out(p+' '+x+':\n'+f(p,x)+'\n'))
Test cases from OP:
<pre id=OUT></pre>


PowerShell, 168 bytes

function x($s,$t){$p="A"+$s-split''|?{$_}|%{[int][char]$_}|sort|%{[char]$_};$l=$p.count;1..$t|%{$s=$s.replace($p[$_%$l],$p[($_-1)%$l])};$s;"It is $($p[$t%$l])'s turn."}

I've decided all my answers on this site will be in PowerShell. One day I'll have an answer that can compete...

call the function like this: x Bb 2


This answer isn't going to win, but I'll throw it out there anyway.

Python 3, 167 bytes

for i in range(n):
print(''.join(f)+'\nIt is '+c+"'s turn.")

Pip, 54 bytes

Not very competitive, but at least I get to show off Pip's mutable strings and the Swap command. Takes the seating order and number of rounds as command-line arguments (which get assigned to a and b, respectively).

u:'Ao:SN A^u.aLbSu(aa@?C(o++i))Pa"It is ".u."'s turn."


u:'A                   u = player who's currently up

o:SN A^u.a
      ^u.a             Append "A" to the seating order, split into list of characters
o:SN A                 ASCII value of each char, sort the resulting list, assign to o

Lb                     Repeat b times:
  Su                   Swap u with:
    (a          )      The character of a at index:
      a@?              Find in a:
         C(o++i)       chr(ASCII value of next player from o)
                       (Subscripts wrap around, as in CJam, so no need for mod)

Pa                     Print the final lineup
"It is ".u."'s turn."  Current player (auto-printed)

It would have been 49 if I'd bothered to implement SS (sort as strings) at the same time I did SN (sort numeric)... Ah well, the perils of having a language in development.


Python 2, 105 bytes

exec"b=a<max(s)and min(e for e in s if e>a)or'A';s=s.replace(b,a);a=b;"*input()
print s,b

Try it online!

Golf of:

for i in range(n):
  b=min(e for e in s if e>a)
print s
print b

Perl 5, 102 + 1 (-n) = 103 bytes

It is $p[$t%@p]'s turn."

Try it online!


Seating order, followed by number of turns without spaces:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.