# Is this string a letter doing a cartwheel?

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.

# Rules

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.

• 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


# Winner

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

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

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

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

/nu|wm|qb/!d
/^((.).)\1*\2$/!d  Try it online! Deletes input if falsey, otherwise does nothing to the input. ### Explanation 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  • Here comes the sed wizard... Aug 2, 2017 at 17:34 • @MDXF I'm far from being a wizard, still a beginner :) Aug 2, 2017 at 17:35 • Everything in sed looks like wizardry to me. :P Aug 2, 2017 at 17:38 • 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. – Neil Aug 2, 2017 at 18:41 ## JavaScript (ES6), 8278 77 bytes Saved 1 byte by using two falsy values, as suggested by ThePirateBay and MD XF. ([c,...s],S='bqwmun')=>s.reduceRight((r,a)=>r&a==S[S.search(c)^++k&1],k=s>'')  ### Test cases let f = ([c,...s],S='bqwmun')=>s.reduceRight((r,a)=>r&a==S[S.search(c)^++k&1],k=s>'') 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 • Any reason for leaving && instead of &? – user72349 Aug 2, 2017 at 18:56 • @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.) Aug 2, 2017 at 19:04 • @Arnauld You may remove the second &; I specified (in chat) sometime that inconsistent falsy values are allowed. Aug 3, 2017 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! • sighs as you pass me in rep again Aug 2, 2017 at 17:17 • I don't feel good about this, since you're about to reach my byte count again, but -2 bytes. Aug 2, 2017 at 17:35 • BTW, your solution is invalid, just like mine was in the beginning. Fails for nunununu. Aug 2, 2017 at 17:36 • Fixed at the cost of a lot of bytes. ;-; Aug 2, 2017 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! • 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()) Aug 2, 2017 at 20:00 • You could save 1 byte by doing s[0] instead of c[-1]. Aug 2, 2017 at 20:12 • 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. Aug 2, 2017 at 21:50 • The 59 bytes breaks with single character inputs. Sorry for picking on this one, I like Python :) Aug 2, 2017 at 22:32 • Aren't exceptions falsey? That break is intentional Aug 2, 2017 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! # Explanation • ''.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. • Fails no test cases, good job +1 Aug 2, 2017 at 17:27 • @MDXF Oh thanks! I am so relieved rn... >_< Aug 2, 2017 at 17:27 • uwuwuwuwuwu results in true Aug 2, 2017 at 17:43 • Invalid: nunuuunun Aug 3, 2017 at 1:51 • nuuun -> True. This is not right. Aug 3, 2017 at 21:06 # JavaScript (ES6), 40 bytes s=>/^(nu|un|bq|qb|wm|mw)+$/.test(s+s[1])


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.

Tests:

[
"nun",
"nunun",
"nunununu",
"wmw",
"wmwun",
"bqbqbqbqbqb",
"v^v^v",
"AVAVA",
"OOO",
"ununununu",
"nunwmwnun",
"nun unun",
"nunwmw",
"nnuunnuunnuu",
"nwnwnwnwn",
"m",
"nunuuunun"
].forEach( x=>console.log( x, (s=>/^(nu|un|bq|qb|wm|mw)+$/.test(s+s[1]))(x) ) ) • 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. Aug 4, 2017 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'bqbunuwmw'  Try it online! The spaces in the string are DEL characters. • |u| is interpreted as a cartwheel. Aug 3, 2017 at 11:27 • @Harrichael I put in DEL characters to be clear. – xnor Aug 3, 2017 at 19:23 • 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' Aug 3, 2017 at 19:46 # Retina, 24 bytes Gnu|mw|bq ^((.).)\1*\2$


Outputs 1 for truthy, 0 for falsy.

Try it online!

• The newer version outputs truthy for nunwmwnun (when it should be false), which was why I had the \1* in my sed answer. Aug 2, 2017 at 18:08
• @Cowsquack Ah I see.
– Okx
Aug 2, 2017 at 18:10
• The first line can be Gnu|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..
– Neil
Aug 2, 2017 at 18:40
• @Neil That fails the test case ununununu
– Okx
Aug 2, 2017 at 18:49
• @Okx Neil's suggestion still seems to work for that testcase Try it online! Aug 2, 2017 at 19:00

## Grime, 28 bytes

e..-#!"nu\|bq\|mw"oTv&..v+.


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

## Explanation

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

e..-#!"nu\|bq\|mw"oTv&..v+.
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 (..)+.

# Jelly, 23 bytes

This took more work than one might think!

“nmbuwq”iÐ€o⁵IAs2⁼€3,3Ȧ


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

Try it online! or see a test suite.

### How?

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, 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

&qeQhQ/"nu,mw,bq"S+{%2tQ{%2


Test Suite.

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

# Jelly, 27 bytes

Ḋm2Qµ³m2Q;Ṣe“nu“mw“bq”ȧ³⁼U¤


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:
³     z
⁼      ==
U        z[::-1]
¤  [End chain]

• Jelly... longer than Pyth?! Aug 2, 2017 at 20:19
• @Mr.Xcoder shhhh I'm working on it... xD Aug 2, 2017 at 20:45
• Hah well I'm tied with you now xD Aug 2, 2017 at 20:47

# 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!

• Can you compress the string to save any bytes? Aug 2, 2017 at 18:49
• nwnwn is true when it ahould be false Aug 3, 2017 at 11:15
• @Harrichael Thanks, fixed! Aug 3, 2017 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!

$c={"nuunmwwmbppb"=~/./g}->{$l=chop};$_=/^($l$c)+$/&&$c  Try it online! Prints the "upside down" version of the first character for true, nothing for false. • Beat me by a solid 18 bytes. Nice answer! Aug 2, 2017 at 21:26 • Not quite so many now. The original was returning true for any string of all the same character. Aug 2, 2017 at 21:37 # PHP, 59+1 bytes <?=preg_match('#^(nu|un|mw|wm|bq|qb)\1+$#',$argn.$argn[1]);


Run as pipe with -F.

partly regex solution, 101+1 bytes:

# MATL, 25 bytes

'nuwmbq'&mq2&\d~wdts~Gnqv


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.

### Explanation

'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
re.compile('(n(un)+|u(nu)+|m(wm)+|w(mw)+|b(pb)+|p(bp)+)$').match  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.

Explanation:

# 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. s/^($&)*//;
# 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)+/)


Run:

1> (f^$#/n(un)+|u(nu)+|m(wm)+|w(mw+)|b(qb)+|q(bq)+/) #<intrinsic fun: 1 param> 2> [*1 "nun"] "nun" 3> [*1 "nuns"] nil 4> [*1 "mwm"] "mwm" 5> [*1 "wmw"] "wmw" 6> [*1 "abc"] nil  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 nun 0:$ ./txr cart.txr
abc
1:$./txr cart.txr bab 1:$ ./txr cart.txr
mwm
1:$./txr cart.txr nununununun 0:$


Explanation:

• @{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

#include<map>
#include<string>
std::map<char,char>c{{'n','u'},{'m','w'},{98,'q'},{'w','m'},{'u','n'},{'q',98}};
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;}

• Save 10 bytes by using the ASCII values for all the characters instead of just two. Aug 2, 2017 at 23:12
• @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 ) Aug 2, 2017 at 23:28

# JavaScript (ES6), 63 bytes

s=>/bq|wm|un/.test(s)&s.replace(RegExp(s[0]+s[1],'g'),'')==s[0]


Returns 1 or 0.

Explanation

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

/bq|wm|un/.test(s)


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:

s.replace(RegExp(s[0]+s[1],'g'),'')==s[0]


let f=
s=>/bq|wm|un/.test(s)&s.replace(RegExp(s[0]+s[1],'g'),'')==s[0]

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`