Challenge inspiration was this that I saw somewhere:

The word "nun" is just the letter n doing a cartwheel

Your challenge is to take a string and determine if it is the first letter doing a cartwheel.


A string is a letter doing a cartwheel if:

  • The first letter is the same as the last letter. (The letter can't land on its head.)
  • The string alternates between cartwheeling letters every one character.

The cartwheeling letters are n and u, m and w, b and q. Note that n and w together are not cartwheeling letters, and neither are w and b.

  • You will take a string using any of our standard input methods.
  • You will output a truthy value if the string is a cartwheeling letter, and a falsy value if it is not. Output can be done using any standard output methods.

Additional rules:

  • Only lowercase cartwheel letters n/u/m/w/b/q need to be handled.
  • You may assume that input is never empty.
  • A one-character string is not a valid cartwheel.

Test cases

Input        -> Output
nun          -> truthy
nunun        -> truthy
nunununu     -> falsy
wmw          -> truthy
wmwun        -> falsy
bqbqbqbqbqb  -> truthy
v^v^v        -> falsy
AVAVA        -> falsy
OOO          -> falsy
ununununu    -> truthy
nunwmwnun    -> falsy
nun unun     -> falsy
nunwmw       -> falsy
nnuunnuunnuu -> falsy
nwnwnwnwn    -> falsy
m            -> falsy
nunuuunun    -> falsy


As with , the shortest code (in each language) wins!

  • 30
    \$\begingroup\$ I think b cartwheels into q, doesn't it? d and p are also cartwheel buddies. They key is that they rotate, not flip. \$\endgroup\$ Aug 2, 2017 at 17:24
  • \$\begingroup\$ Another testcase suggestion: uwuwuwuwuwu \$\endgroup\$
    – user41805
    Aug 2, 2017 at 17:44
  • 20
    \$\begingroup\$ Why bqb but not pdp? \$\endgroup\$
    – aschepler
    Aug 3, 2017 at 0:33
  • \$\begingroup\$ @aschepler I messed up. \$\endgroup\$
    – MD XF
    Aug 3, 2017 at 22:33
  • 2
    \$\begingroup\$ Since dpd, pdp and such don't work, I think you should have them in the test cases with a falsy answer. \$\endgroup\$
    – trlkly
    Aug 4, 2017 at 1:05

34 Answers 34


Javascript, 58 bytes


Tests string for all possible cartwheel strings. Port of this Java answer


Japt, 28 bytes

`wmq¿n`pw ò øU¯2)«(U+g1)rU¯2

Test it online!


`wmq¿n` pw ò øU¯  2)«  (U+ g1)rU¯  2
"wmqbun"pw ò øUs0,2)&&!(U+Ug1)rUs0,2
                                       Implicit: U = input string
"wmqbun"                               Take this string.      "wmqbun"
        pw                             Append its reverse.    "wmqbunnubqmw"
           ò                           Form into groups of 2. ["wm","qb","un","nu","nq","mw"]
             ø     )                   Check whether this contains
              Us0,2                      the first two chars of U.
                        U+             Take U and append
                       (  Ug1)           the second char of U.
                              r        Remove all copies of
                               Us0,2     the first two chars of U.
                                       This returns "" iff U has the pattern "xyxyxyx".
                      !                Take the logical NOT: true iff the result is "".
                    &&                 Return whether both of these conditions are true.
                                       Implicit: output result of last expression
  • \$\begingroup\$ Okay not so related but is the TIO version of Japt outdated? If so, should it be updated? \$\endgroup\$
    – ASCII-only
    Aug 4, 2017 at 6:38
  • \$\begingroup\$ @ASCII-only It is outdated, but I've made a bunch of breaking changes since it was updated (most of them unintentional and still broken :P) so I'm going to hold off on requesting an update for the moment... \$\endgroup\$ Aug 4, 2017 at 16:30
  • \$\begingroup\$ oh no Dennis already pulled (so sorry) also D: why do you still have breaking changes pls fix immediately \$\endgroup\$
    – ASCII-only
    Aug 4, 2017 at 23:59

C (gcc), 91 90 bytes

Fixed to support strings longer than 255 bytes, which also saves a byte.


Try it online!


Japt, 20 bytes

Êu *`q¿nwm`pÔò d@¥îX

Try it

Êu *`...`pÔò d@¥îX     :Implicit input of string U
Ê                      :Length
 u                     :Mod 2
   *                   :Times
    `...`              :  Compressed string "wbunwm"
         p             :  Append
          Ô            :    Reverse
           ò           :  Partitions of length 2
             d         :  Any true
              @        :  When passed through the following function as X
               ¥       :    Test U for equality with
                îX     :    X repeated and sliced to length of U

Your Answer

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

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