38
\$\begingroup\$

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.

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.

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

Winner

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

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

34 Answers 34

13
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ Here comes the sed wizard... \$\endgroup\$
    – MD XF
    Commented Aug 2, 2017 at 17:34
  • \$\begingroup\$ @MDXF I'm far from being a wizard, still a beginner :) \$\endgroup\$
    – user41805
    Commented Aug 2, 2017 at 17:35
  • \$\begingroup\$ Everything in sed looks like wizardry to me. :P \$\endgroup\$
    – MD XF
    Commented Aug 2, 2017 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
    Commented Aug 2, 2017 at 18:41
8
\$\begingroup\$

JavaScript (ES6), 82 78 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

\$\endgroup\$
3
  • \$\begingroup\$ Any reason for leaving && instead of &? \$\endgroup\$
    – user72349
    Commented Aug 2, 2017 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
    Commented Aug 2, 2017 at 19:04
  • \$\begingroup\$ @Arnauld You may remove the second &; I specified (in chat) sometime that inconsistent falsy values are allowed. \$\endgroup\$
    – MD XF
    Commented Aug 3, 2017 at 22:34
5
\$\begingroup\$

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!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ sighs as you pass me in rep again \$\endgroup\$
    – MD XF
    Commented Aug 2, 2017 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
    Commented Aug 2, 2017 at 17:35
5
\$\begingroup\$

Python 2, 63 bytes

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

Try it online!

\$\endgroup\$
6
  • \$\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
    Commented Aug 2, 2017 at 20:00
  • \$\begingroup\$ You could save 1 byte by doing s[0] instead of c[-1]. \$\endgroup\$
    – DJMcMayhem
    Commented Aug 2, 2017 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\$ Commented Aug 2, 2017 at 21:50
  • \$\begingroup\$ The 59 bytes breaks with single character inputs. Sorry for picking on this one, I like Python :) \$\endgroup\$ Commented Aug 2, 2017 at 22:32
  • \$\begingroup\$ Aren't exceptions falsey? That break is intentional \$\endgroup\$
    – Harrichael
    Commented Aug 2, 2017 at 22:34
5
\$\begingroup\$

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.

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

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

\$\endgroup\$
1
  • \$\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
    Commented Aug 4, 2017 at 1:09
4
\$\begingroup\$

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))))
\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ |u| is interpreted as a cartwheel. \$\endgroup\$
    – Harrichael
    Commented Aug 3, 2017 at 11:27
  • \$\begingroup\$ @Harrichael I put in DEL characters to be clear. \$\endgroup\$
    – xnor
    Commented Aug 3, 2017 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
    Commented Aug 3, 2017 at 19:46
4
\$\begingroup\$

Retina, 24 bytes

G`nu|mw|bq
^((.).)\1*\2$

Outputs 1 for truthy, 0 for falsy.

Port of Cows quack's answer.

Try it online!

\$\endgroup\$
9
  • \$\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
    Commented Aug 2, 2017 at 18:08
  • \$\begingroup\$ @Cowsquack Ah I see. \$\endgroup\$
    – Okx
    Commented Aug 2, 2017 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
    Commented Aug 2, 2017 at 18:40
  • \$\begingroup\$ @Neil That fails the test case ununununu \$\endgroup\$
    – Okx
    Commented Aug 2, 2017 at 18:49
  • \$\begingroup\$ @Okx Neil's suggestion still seems to work for that testcase Try it online! \$\endgroup\$
    – user41805
    Commented Aug 2, 2017 at 19:00
3
\$\begingroup\$

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 (..)+.
\$\endgroup\$
2
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
0
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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:
                       ³  ¤  Nilad followed by links as nilad
                       ³     z
                        ⁼      == 
                         U        z[::-1]
                          ¤  [End chain]
\$\endgroup\$
3
  • \$\begingroup\$ Jelly... longer than Pyth?! \$\endgroup\$
    – Mr. Xcoder
    Commented Aug 2, 2017 at 20:19
  • \$\begingroup\$ @Mr.Xcoder shhhh I'm working on it... xD \$\endgroup\$
    – hyperneutrino
    Commented Aug 2, 2017 at 20:45
  • \$\begingroup\$ Hah well I'm tied with you now xD \$\endgroup\$
    – hyperneutrino
    Commented Aug 2, 2017 at 20:47
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Japt, 47 bytes

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

Try it online!

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

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!

\$\endgroup\$
0
1
\$\begingroup\$

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

$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.

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

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:

for($s=$argn;$c=$s[$i++];$p=$c)$c!=$p||die;echo$i%2<1&&preg_match("#^(nu|mw|bq)#",count_chars($s,3));

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

\$\endgroup\$
1
\$\begingroup\$

Java 8, 57 bytes

s->s.matches("(nu)+n|(un)+u|(mw)+m|(wm)+w|(bq)+b|(qb)+q")

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 ^...$.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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).
}
\$\endgroup\$
0
\$\begingroup\$

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.)

\$\endgroup\$
0
\$\begingroup\$

Python 3, 66 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

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;}
\$\endgroup\$
2
  • \$\begingroup\$ Save 10 bytes by using the ASCII values for all the characters instead of just two. \$\endgroup\$
    – MD XF
    Commented Aug 2, 2017 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\$ Commented Aug 2, 2017 at 23:28
0
\$\begingroup\$

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

\$\endgroup\$

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.