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!

  • 29
    \$\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 '17 at 17:24
  • \$\begingroup\$ Another testcase suggestion: uwuwuwuwuwu \$\endgroup\$
    – user41805
    Aug 2 '17 at 17:44
  • 19
    \$\begingroup\$ Why bqb but not pdp? \$\endgroup\$
    – aschepler
    Aug 3 '17 at 0:33
  • \$\begingroup\$ @aschepler I messed up. \$\endgroup\$
    – MD XF
    Aug 3 '17 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 '17 at 1:05

34 Answers 34


sed 4.2.2, 30 + 1 -r = 43 31 bytes

Saved 12 bytes thanks to @Neil by shortening the first line


Try it online!

Deletes input if falsey, otherwise does nothing to the input.


With the -r flag, we do not need to use \( and \) for capturing groups and this saves bytes.

/nu|wm|qb/!                # On every line that does not match this regex
           d               # Delete
/^((.).)\1*\2$/!           # On every line that does not math
 ^((.).)                   #  the first two characters at the beginning of the line
        \1*                #  repeated
           \2$             #  with the first character at the end of the line
                d          # Delete
  • \$\begingroup\$ Here comes the sed wizard... \$\endgroup\$
    – MD XF
    Aug 2 '17 at 17:34
  • \$\begingroup\$ @MDXF I'm far from being a wizard, still a beginner :) \$\endgroup\$
    – user41805
    Aug 2 '17 at 17:35
  • \$\begingroup\$ Everything in sed looks like wizardry to me. :P \$\endgroup\$
    – MD XF
    Aug 2 '17 at 17:38
  • 1
    \$\begingroup\$ You only have to check for containment of half of the letter pairs e.g. both unu and nun contain nu and the second line ensures that the rest of the letters match those two. \$\endgroup\$
    – Neil
    Aug 2 '17 at 18:41

JavaScript (ES6), 82 78 77 bytes

Saved 1 byte by using two falsy values, as suggested by ThePirateBay and MD XF.


Test cases

let f =


console.log('Truthy ...')
console.log(f("nun"         )) // -> truthy
console.log(f("nunun"       )) // -> truthy
console.log(f("wmw"         )) // -> truthy
console.log(f("bqbqbqbqbqb" )) // -> truthy
console.log(f("ununununu"   )) // -> truthy

console.log('Falsy ...')
console.log(f("nunununu"    )) // -> falsy
console.log(f("wmwun"       )) // -> falsy
console.log(f("v^v^v"       )) // -> falsy
console.log(f("AVAVA"       )) // -> falsy
console.log(f("OOO"         )) // -> falsy
console.log(f("nunwmwnun"   )) // -> falsy
console.log(f("nun unun"    )) // -> falsy
console.log(f("nunwmw"      )) // -> falsy
console.log(f("nnuunnuunnuu")) // -> falsy
console.log(f("m"           )) // -> falsy

  • \$\begingroup\$ Any reason for leaving && instead of &? \$\endgroup\$
    – user72349
    Aug 2 '17 at 18:56
  • \$\begingroup\$ @ThePirateBay Well, the only reason is the consistency of the returned falsy value, although it doesn't really seem to be a requirement for this challenge. (Using & would produce either false or 0.) \$\endgroup\$
    – Arnauld
    Aug 2 '17 at 19:04
  • \$\begingroup\$ @Arnauld You may remove the second &; I specified (in chat) sometime that inconsistent falsy values are allowed. \$\endgroup\$
    – MD XF
    Aug 3 '17 at 22:34

Python 3, 111 bytes

-2 bytes thanks to Mr. Xcoder.

lambda s:s[0]==s[-1]and any(any({*s[::2]}=={i[j]}and{*s[1::2]}=={i[j<1]}for j in[0,1])for i in['nu','mw','bq'])

Try it online!

  • 2
    \$\begingroup\$ sighs as you pass me in rep again \$\endgroup\$
    – MD XF
    Aug 2 '17 at 17:17
  • \$\begingroup\$ I don't feel good about this, since you're about to reach my byte count again, but -2 bytes. \$\endgroup\$
    – Mr. Xcoder
    Aug 2 '17 at 17:35
  • \$\begingroup\$ BTW, your solution is invalid, just like mine was in the beginning. Fails for nunununu. \$\endgroup\$
    – Mr. Xcoder
    Aug 2 '17 at 17:36
  • \$\begingroup\$ Fixed at the cost of a lot of bytes. ;-; \$\endgroup\$ Aug 2 '17 at 17:38

Python 2, 63 bytes

lambda s:s[:3]in'ununqbqbwmwm'and s==s[:2]*max(len(s)/2,1)+s[0]

Try it online!

  • \$\begingroup\$ Nice answer, welcome to the site! Some tips: You could do "nu un nm mn bp pb".split() to save 4 bytes, and you could remove some whitespace. 75 bytes: lambda s:any(s==c[-1]+c*max(len(s)/2,1)for c in"nu un nm mn bp pb".split()) \$\endgroup\$
    – DJMcMayhem
    Aug 2 '17 at 20:00
  • \$\begingroup\$ You could save 1 byte by doing s[0] instead of c[-1]. \$\endgroup\$
    – DJMcMayhem
    Aug 2 '17 at 20:12
  • \$\begingroup\$ The 61 byte answer returns True for unmnu and unmwnu. Really it returns false positives when (s==s[::-1])+len(set(s)) is 4, which is easy to force. Even just 4 different characters makes it return True. \$\endgroup\$ Aug 2 '17 at 21:50
  • \$\begingroup\$ The 59 bytes breaks with single character inputs. Sorry for picking on this one, I like Python :) \$\endgroup\$ Aug 2 '17 at 22:32
  • \$\begingroup\$ Aren't exceptions falsey? That break is intentional \$\endgroup\$
    – Harrichael
    Aug 2 '17 at 22:34

Python 3, 71 bytes

lambda n:''.join(sorted({*n[1::2]}|{*n[::2]}))in"nu,mw,bq"*(n==n[::-1])

Try it online!

-1 thanks to @HyperNeutrino and -13 thanks to @ovs

If the above is found to fail for any test case, there is an alternative:

lambda n:(sorted(list({*n[1::2]}.union({*n[::2]})))in[[*'nu'],[*'mw'],[*'bq']])*n[0]==n[-1]

Try it online!


  • ''.join(sorted(list({*n[1::2]}).union({*n[::2]})))) - Gets the characters at odd indexes and the characters at even indexes, de-duplicates them and sorts the list formed by their union.

  • in'nu,mw,bq' - Checks if they are valid cart-letter combinations.

  • n[0]==n[-1] - Checks if the first character is the same as the last one.

  • \$\begingroup\$ Fails no test cases, good job +1 \$\endgroup\$
    – MD XF
    Aug 2 '17 at 17:27
  • \$\begingroup\$ @MDXF Oh thanks! I am so relieved rn... >_< \$\endgroup\$
    – Mr. Xcoder
    Aug 2 '17 at 17:27
  • 1
    \$\begingroup\$ uwuwuwuwuwu results in true \$\endgroup\$
    – user41805
    Aug 2 '17 at 17:43
  • 1
    \$\begingroup\$ Invalid: nunuuunun \$\endgroup\$
    – Harrichael
    Aug 3 '17 at 1:51
  • 1
    \$\begingroup\$ nuuun -> True. This is not right. \$\endgroup\$
    – recursive
    Aug 3 '17 at 21:06

JavaScript (ES6), 40 bytes


Checks to see if the input string concatenated with the second character of the input string is a repeating string of the same pair of cartwheel characters.


  "nun unun",
].forEach( x=>console.log( x, (s=>/^(nu|un|bq|qb|wm|mw)+$/.test(s+s[1]))(x) ) )

  • \$\begingroup\$ Really love the logic here! I never would have thought of adding a character at the end and testing. Nice, clean code, too, even when golfed. \$\endgroup\$
    – trlkly
    Aug 4 '17 at 1:09

Clojure, 156 bytes

(fn[w](let[s["nu""wm""bq"]c #(= 1(count(set %)))e #(take-nth 2 %)r #(and(c(e %))(c(e(drop 1 %))))](and(=(first w)(last w))(r w)(some #(=(set w)(set %))s))))

This was deceptively difficult! I ended up having to break it down into 3 sub problems:

  • Is the first letter the same as the last?
  • Do the letters repeat?
  • Are all the letters a part of one of the valid sets?

I certainly didn't win, but this was a good morning exercise! Full explanation below:

(defn cartwheel? [word]
  (let [; Valid Character Sets
        cs ["nu" "wm" "bq"]
        ; Is the list composed of only a single character?
        count-1? #(= 1 (count (set %)))
        ; Grabs every other element of the list
        every-other #(take-nth 2 %)
        ; Do the characters repeat? Works by checking if every other element is the same, then drops the first letter
        ; to check all the odd indexed characters
        repeating? #(and (count-1? (every-other %))
                         (count-1? (every-other (drop 1 %))))]

    ; Do all the predicates hold?
    (and (= (first word) (last word))
         (repeating? word)
         ; Is the set of letters in the word part of some set of the valid characters?
         (some #(= (set word) (set %)) cs))))

Haskell, 80 78 bytes

f s|l<-length s=odd l&&l>1&&any((==s).take l.cycle)(words"un nu mw wm bq qb")

Try it online!

How it works:

l<-length s        -- let l be the length of the input string

f s         =      -- return True, if
    odd l          -- l is odd and
    l > 1          -- l is greater than 1 and 
    any            -- any of the following is also True
      (     )(words "  ...  ")
                   -- apply the function to each of the words "un", "nu" ... "qb"
           cycle   --  infinitely repeat the word
      take l       --  take the first l characters
     (==s)         --  and compare to s

Python 2, 45 bytes

lambda s:s[2:]+s[1:3]==s>s[:2]in'bqbunuwmw'

Try it online!

The spaces in the string are DEL characters.

  • \$\begingroup\$ |u| is interpreted as a cartwheel. \$\endgroup\$
    – Harrichael
    Aug 3 '17 at 11:27
  • \$\begingroup\$ @Harrichael I put in DEL characters to be clear. \$\endgroup\$
    – xnor
    Aug 3 '17 at 19:23
  • \$\begingroup\$ DEL characters can still be input characters though. I like your solution but you should borrow a trick from my answer: s[:3]in'bqbqnunuwmwm' \$\endgroup\$
    – Harrichael
    Aug 3 '17 at 19:46

Retina, 24 bytes


Outputs 1 for truthy, 0 for falsy.

Port of Cows quack's answer.

Try it online!

  • \$\begingroup\$ The newer version outputs truthy for nunwmwnun (when it should be false), which was why I had the \1* in my sed answer. \$\endgroup\$
    – user41805
    Aug 2 '17 at 18:08
  • \$\begingroup\$ @Cowsquack Ah I see. \$\endgroup\$
    – Okx
    Aug 2 '17 at 18:10
  • \$\begingroup\$ The first line can be G`nu|mw|bp since all truthy strings contain one of those letter pairs and the second line will ensure that all of the rest of the string contains those letters too.. \$\endgroup\$
    – Neil
    Aug 2 '17 at 18:40
  • \$\begingroup\$ @Neil That fails the test case ununununu \$\endgroup\$
    – Okx
    Aug 2 '17 at 18:49
  • \$\begingroup\$ @Okx Neil's suggestion still seems to work for that testcase Try it online! \$\endgroup\$
    – user41805
    Aug 2 '17 at 19:00

Grime, 28 bytes


Try it online! Prints 1 for truthy inputs and 0 for falsy ones.


Grime syntax resembles regular expressions, and a Grime program specifies a pattern that may or may not match a rectangle of characters.

e`                            Match input against pattern:
      !                       Does not
     #                        contain
  ..                          a 2-character substring
    -                         which is not
       "nu\|bq\|mw"           any of these strings
                   oT         potentially reversed,
                     v&       AND
                       ..     two characters
                         v+   one or more times
                           .  then one more character.

Some features of Grime that helped shorten this:

  • Normally a character literal must be escaped with a backslash, but "" changes this: syntax elements are escaped but literals are not. Without the quotes, the part that enumerates the character pairs would be (\n\u|\b\p|\m\w)oT.
  • Unary operators that follow a binary operator (here -) act on its result: ..-#!"…"oT is equivalent to (..-"…"oT)#!.
  • The vs lower the precedence of the syntax elements that follow them. A lone & has higher precedence than -, but v& has lower. Similarly, ..+ is parsed as .(.+), but ..v+ is equivalent to (..)+.

Python 2, 69 bytes

lambda x:any([x in p*len(x)for p in'nu','mw','bq'])*len(x)%2*len(x)>2

Try it online!


Pyth, 27 bytes


Test Suite.

Outputs 1 for truthy and False or 0 for falsy, as the OP allowed in chat.


Jelly, 27 bytes


Try it online!

How it works

Ḋm2Qµ³m2Q;µṢe“nu“mw“bq”ȧ³⁼U¤  Main link; z is the argument
Ḋ                             z[1:]
 m2                           z[1::2]
   Q                          deduplicate(z[1::2])
    µ                         New Chain
     ³                        z
      m2                      z[::2]
        Q                     deduplicate(z[::2])
         ;                    deduplicate(z[1::2]) + deduplicate(z[::2])
          Ṣ                  Sort the result
           e                 Is it an element of the following?
            “nu“mw“bq”       ["nu", "mw", "bq"]
                      ȧ      It has the correct characters and:
                       ³  ¤  Nilad followed by links as nilad
                       ³     z
                        ⁼      == 
                         U        z[::-1]
                          ¤  [End chain]
  • \$\begingroup\$ Jelly... longer than Pyth?! \$\endgroup\$
    – Mr. Xcoder
    Aug 2 '17 at 20:19
  • \$\begingroup\$ @Mr.Xcoder shhhh I'm working on it... xD \$\endgroup\$
    – hyper-neutrino
    Aug 2 '17 at 20:45
  • \$\begingroup\$ Hah well I'm tied with you now xD \$\endgroup\$
    – hyper-neutrino
    Aug 2 '17 at 20:47

Jelly, 23 bytes

This took more work than one might think!


A monadic link taking a list of characters and returning 1 (truthy) or 0 (falsey).

Try it online! or see a test suite.


Finds the index of each character of the input in the 1-indexed list of characters nmbuwq. This string is arranged such that the indexes of pairs are three apart, as such the incremental difference of the indexes for valid cartwheels will be repetitions of one of [-3,3] or [3,-3].

When an item is not found in a list by the "index of" atom, i, it returns 0, which would pair unfound characters with b, making input like bxbxb truthy. So 0s are replaced by 10 a value more than three away from any other value prior to checking for validity.

“nmbuwq”iЀo⁵IAs2⁼€3,3Ȧ - Link: list of characters, s
         Ѐ             - map across c in s:
        i               -   first index of c in (1-indexed; 0 if not present)
“nmbuwq”                -   literal "nmbuwq"
            ⁵           - literal 10
           o            - logical or (vectorises - replace any 0s with 10s)
             I          - incremental differences (i.e. deltas)
              A         - absolute value (vectorises)
               s2       - split into chunks of 2 (possibly with a single remainder)
                   3,3  - pair three with three = [3,3]
                 ⁼€     - equals? for €ach (1 if so, otherwise 0 - including a single 3)
                      Ȧ - any and all? (0 if any 0s or if empty, 1 otherwise)

Python 2, 103 bytes

lambda x:len(x)>1and{x[0],x[1]}in[{i,j}for i,j in zip('ubw','nqm')]and x==(-~len(x)/2*(x[0]+x[1]))[:-1]

Try it online!


Japt, 47 bytes

g ¦U¬o ª`q¿nwm`ò má c aU¯2)<0?0:UÅë ä¥ ©Uë ä¥ e

Try it online!

  • \$\begingroup\$ Can you compress the string to save any bytes? \$\endgroup\$
    – Shaggy
    Aug 2 '17 at 18:49
  • \$\begingroup\$ nwnwn is true when it ahould be false \$\endgroup\$
    – Harrichael
    Aug 3 '17 at 11:15
  • \$\begingroup\$ @Harrichael Thanks, fixed! \$\endgroup\$
    – Oliver
    Aug 3 '17 at 11:28

Python 3, 88 bytes

lambda x:[len(x)%2,x[:2]in'nu,un,bq,qb,mw,wm',len(set(x[::2])),len(set(x[1::2]))]==[1]*4

len(x)%2: an even-length string can't end on the first character

x[:2] in: check for any of the 6 valid beginning pairs

len(set()): get the length of the sets of characters at 0,2,4... and 1,3,5...

Returns True if the list of evaluations is equal to [1,1,1,1], else False.

Try it online!


Perl 5, 55 + 1 (-p) = 56 bytes


Try it online!

Prints the "upside down" version of the first character for true, nothing for false.

  • \$\begingroup\$ Beat me by a solid 18 bytes. Nice answer! \$\endgroup\$ Aug 2 '17 at 21:26
  • \$\begingroup\$ Not quite so many now. The original was returning true for any string of all the same character. \$\endgroup\$
    – Xcali
    Aug 2 '17 at 21:37

PHP, 59+1 bytes


Run as pipe with -F.

partly regex solution, 101+1 bytes:


Empty output for falsy. Run as pipe with -nR.


Java 8, 57 bytes


Try it here.

Simple regex to match all six cases. Note that Java's String#matches automatically matches the entire String, so there is no need for ^...$.


MATL, 25 bytes


The ouput is a non-empty numeric column vector, which is truthy if all its entries are nonzero, and falsy otherwise. Try it online!

To verify all test cases, an if branch is added in the footer that replaces any truthy value by the string 'truthy', or any falsy value by the string 'falsy', and then displays the string.


'nuwmbq'  % Push this string
&m        % Implicit input. For each char, push index of membership in the above
          %  string, or 0 if not member
q         % Subtract 1
2         % Push 2
&\        % Divmod. Pushes remainders, then quotients
d~        % Consecutive differences negated. Gives an array of ones iff all
          % quotients were equal
w         % Swap. Moves remainders to top
d         % Consecutive differences. Gives nonzeros iff no consecutive
          % remainders were equal
ts~       % Duplicate, sum, negate. Gives true iff sum was 0. For unequal
          % consecutive differences of remainders, this corresponds to an odd
          % number of remainders
Gnq       % Push input, length, subtract 1. True iff input longer than 1
v         % Concatenate into column vector. Truthy iff all entries are nonzero

Python 2, 74 bytes

import re

Try it online! This take on the problem is surprisingly competitive.


Clojure, 115 bytes

(apply some-fn(map(fn[r]#(re-matches r %))(map(fn[[x y]](re-pattern(str x"("y x")+")))["nu""un""mw""wm""bq""qb"])))

Build a regex out of each letter pair and see if the input matches one. Lots of more elegant ways to do all of these parts, but they're all more verbose. Such is life with Clojure golfing.


Perl 5, 68 + 1 = 69 bytes

if(/../&&$&=~/nu|un|bq|qb|mw|wm/){s/^($&)*//;$&=~/./;print if/^$&$/}

Run with -n.


# Match the first two characters, and if they exist, then...
if (/../ &&
 # Make sure they are a pair of compatible cartwheel characters.
 $&=~/nu|un|bq|qb|mw|wm/) {
  # If that's true, then eliminate as many of that pair of characters
  # from the beginning of the string as possible.
  # Then get the first character out of the match (the first character
  # of the original string).
  # Print the text (which is truthy since it's nonempty) if the original
  # first character matches exactly the remaining string.
  print if/^$&$/
  # Otherwise, print nothing (the empty string in Perl is falsy).

TXR Lisp, 50 bytes

(f^$ #/n(un)+|u(nu)+|m(wm)+|w(mw+)|b(qb)+|q(bq)+/)


1> (f^$ #/n(un)+|u(nu)+|m(wm)+|w(mw+)|b(qb)+|q(bq)+/)
#<intrinsic fun: 1 param>
2> [*1 "nun"]
3> [*1 "nuns"]
4> [*1 "mwm"]
5> [*1 "wmw"]
6> [*1 "abc"]

f^$ is a combinator which takes a regex object and returns a function which matches that regex in an anchored way. (By itself, a regex object is function-callable object which takes a string and searches for itself through it.)


Python 3, 66 bytes

lambda x:x==len(x)//2*x[:2]+x[0]and{*x}in[{*'nu'},{*'mw'},{*'bq'}]

Try it online!


TXR: 78 74 bytes

@{x 2}@(rep :gap 0)@x@(end)@y
@(bind(x y)(#"nu un mw wm bq qb"@[x 0..1]))

Run, from system prompt. Number in prompt is termination status: 0 = success, 1 = failure:

0:$ ./txr cart.txr 
0:$ ./txr cart.txr 
1:$ ./txr cart.txr 
1:$ ./txr cart.txr 
1:$ ./txr cart.txr 


  • @{x 2}: match two characters, bind to x variable.
  • @(rep :gap 0)@x@(end): repeated match with no skipped gaps: zero or more occurrences of x, the previously matched digraph.
  • @y: remainder of line matched, captured into y.
  • @(bind(x y)(foo bar)): bind x to foo, y to bar. Since x and y are already bound, they have to match foo and bar, or else there is a failure.
  • foo is #"nu un mw wm bq qb", a word list literal, syntactic sugar for the Lisp list ("nu" "un" ... "qb"). A bind match between a variable and a list means that the variable must match an element.
  • bar is @[x 0..1]: the one-character substring of x from its beginning. The bind match between y and this forces the last letter of the line to match the first.

C++, 268 bytes

int w(std::string s){if(s[0]!=s[s.size()-1]||c.find(s[0])==c.end()||s.size()<3)return 0;for(int i=0;i<s.size()-1;i+=2)if(s[i+1]!=c[s[i]])return 0;return 1;}
  • \$\begingroup\$ Save 10 bytes by using the ASCII values for all the characters instead of just two. \$\endgroup\$
    – MD XF
    Aug 2 '17 at 23:12
  • \$\begingroup\$ @MDXF n = 110, u = 117, m = 109, w = 119, q = 113. So using ASCII values or not does not matter for any chars higher than c ( 99 ) \$\endgroup\$ Aug 2 '17 at 23:28

JavaScript (ES6), 63 bytes


Returns 1 or 0.


All cartwheel strings will have one or more of bq, wm, or un. We test for that with:


If you replace all instances of the first two letters of a cartwheel string with nothing, you're left with the first letter of the string. We test for that with:


let f=

console.log(f('nun'))          // truthy
console.log(f('nunun'))        // truthy
console.log(f('nunununu'))     // falsy
console.log(f('wmw'))          // truthy
console.log(f('wmwun'))        // falsy
console.log(f('bqbqbqbqbqb'))  // truthy
console.log(f('v^v^v'))        // falsy
console.log(f('AVAVA'))        // falsy
console.log(f('OOO'))          // falsy
console.log(f('ununununu'))    // truthy
console.log(f('nunwmwnun'))    // falsy
console.log(f('nun unun'))     // falsy
console.log(f('nunwmw'))       // falsy
console.log(f('nnuunnuunnuu')) // falsy
console.log(f('nwnwnwnwn'))    // falsy
console.log(f('m'))            // falsy


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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