Is this string a letter doing a cartwheel?

35

2

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!

MD XF

Posted 2017-08-02T17:08:13.923

Reputation: 11 605

29I think b cartwheels into q, doesn't it? d and p are also cartwheel buddies. They key is that they rotate, not flip. – Engineer Toast – 2017-08-02T17:24:04.817

Another testcase suggestion: uwuwuwuwuwu – user41805 – 2017-08-02T17:44:17.553

19Why bqb but not pdp? – aschepler – 2017-08-03T00:33:13.050

@aschepler I messed up. – MD XF – 2017-08-03T22:33:39.190

2Since dpd, pdp and such don't work, I think you should have them in the test cases with a falsy answer. – trlkly – 2017-08-04T01:05:30.473

Answers

2

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)

Jonathan Allan

Posted 2017-08-02T17:08:13.923

Reputation: 67 804

13

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

user41805

Posted 2017-08-02T17:08:13.923

Reputation: 16 320

Here comes the sed wizard... – MD XF – 2017-08-02T17:34:08.487

@MDXF I'm far from being a wizard, still a beginner :) – user41805 – 2017-08-02T17:35:55.707

Everything in sed looks like wizardry to me. :P – MD XF – 2017-08-02T17:38:46.067

1You 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 – 2017-08-02T18:41:52.667

8

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

Arnauld

Posted 2017-08-02T17:08:13.923

Reputation: 111 334

Any reason for leaving && instead of &? – None – 2017-08-02T18:56:32.197

@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.) – Arnauld – 2017-08-02T19:04:24.130

@Arnauld You may remove the second &; I specified (in chat) sometime that inconsistent falsy values are allowed. – MD XF – 2017-08-03T22:34:21.660

5

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!

totallyhuman

Posted 2017-08-02T17:08:13.923

Reputation: 15 378

2sighs as you pass me in rep again – MD XF – 2017-08-02T17:17:29.073

I don't feel good about this, since you're about to reach my byte count again, but -2 bytes.

– Mr. Xcoder – 2017-08-02T17:35:32.557

BTW, your solution is invalid, just like mine was in the beginning. Fails for nunununu. – Mr. Xcoder – 2017-08-02T17:36:45.520

Fixed at the cost of a lot of bytes. ;-; – totallyhuman – 2017-08-02T17:38:19.367

5

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.

Mr. Xcoder

Posted 2017-08-02T17:08:13.923

Reputation: 39 774

Fails no test cases, good job +1 – MD XF – 2017-08-02T17:27:34.467

@MDXF Oh thanks! I am so relieved rn... >_< – Mr. Xcoder – 2017-08-02T17:27:55.543

@MDXF It does fail, in fact. Fixing soon EDIT: Fixed – Mr. Xcoder – 2017-08-02T17:29:52.437

You use s only once I think. You could probably get rid of the argument then. – totallyhuman – 2017-08-02T17:30:48.603

Huh, it looked like it did in the TIO link... – MD XF – 2017-08-02T17:30:56.563

@totallyhuman Yes, was just changing that. That was left from a previous version of my code. – Mr. Xcoder – 2017-08-02T17:31:48.960

1uwuwuwuwuwu results in true – user41805 – 2017-08-02T17:43:28.440

@Cowsquack So it should, I think :) – Mr. Xcoder – 2017-08-02T17:45:13.703

Great job fixing this! – MD XF – 2017-08-02T18:50:47.430

@MDXF Thanks. It ended up being really short. – Mr. Xcoder – 2017-08-02T18:51:09.353

I ask the downvoter to remove the downvote (if the reason was this answer being invalid in the past). It's now fixed. – Mr. Xcoder – 2017-08-02T18:52:24.663

71 bytes – ovs – 2017-08-02T21:15:35.113

1Invalid: nunuuunun – Harrichael – 2017-08-03T01:51:26.290

1nuuun -> True. This is not right. – recursive – 2017-08-03T21:06:43.137

5

Python 2, 63 bytes

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

Try it online!

Harrichael

Posted 2017-08-02T17:08:13.923

Reputation: 151

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()) – James – 2017-08-02T20:00:08.613

You could save 1 byte by doing s[0] instead of c[-1]. – James – 2017-08-02T20:12:48.343

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. – Arnold Palmer – 2017-08-02T21:50:20.090

The 59 bytes breaks with single character inputs. Sorry for picking on this one, I like Python :) – Arnold Palmer – 2017-08-02T22:32:17.610

Aren't exceptions falsey? That break is intentional – Harrichael – 2017-08-02T22:34:29.997

Exceptions ruled not falsey - updated from 59 to 63 bytes – Harrichael – 2017-08-02T22:44:40.530

5

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

MT0

Posted 2017-08-02T17:08:13.923

Reputation: 3 373

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. – trlkly – 2017-08-04T01:09:03.927

4

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!

Okx

Posted 2017-08-02T17:08:13.923

Reputation: 15 025

The newer version outputs truthy for nunwmwnun (when it should be false), which was why I had the \1* in my sed answer. – user41805 – 2017-08-02T18:08:30.950

@Cowsquack Ah I see. – Okx – 2017-08-02T18:10:15.073

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.. – Neil – 2017-08-02T18:40:07.417

@Neil That fails the test case ununununu – Okx – 2017-08-02T18:49:03.337

@Okx Neil's suggestion still seems to work for that testcase Try it online!

– user41805 – 2017-08-02T19:00:37.380

Try it online! All test cases at once. – Neil – 2017-08-02T19:01:06.937

@Neil Sorry, my mistake. I kept the ^. Anyways, thanks for the suggestion. – Okx – 2017-08-02T19:03:04.490

If 1 is both truthy and falsy, I guess you always win? (You have a typo in your description) – trlkly – 2017-08-04T00:57:37.937

@trlkly Haha. Fixed. – Okx – 2017-08-05T11:08:54.810

4

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

nimi

Posted 2017-08-02T17:08:13.923

Reputation: 34 639

4

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

Carcigenicate

Posted 2017-08-02T17:08:13.923

Reputation: 3 295

4

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.

xnor

Posted 2017-08-02T17:08:13.923

Reputation: 115 687

|u| is interpreted as a cartwheel. – Harrichael – 2017-08-03T11:27:21.283

@Harrichael I put in DEL characters to be clear. – xnor – 2017-08-03T19:23:00.970

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' – Harrichael – 2017-08-03T19:46:07.423

3

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

Zgarb

Posted 2017-08-02T17:08:13.923

Reputation: 39 083

2

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!

officialaimm

Posted 2017-08-02T17:08:13.923

Reputation: 2 739

2

Japt, 47 bytes

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

Try it online!

Oliver

Posted 2017-08-02T17:08:13.923

Reputation: 7 160

Can you compress the string to save any bytes? – Shaggy – 2017-08-02T18:49:28.037

nwnwn is true when it ahould be false – Harrichael – 2017-08-03T11:15:47.173

@Harrichael Thanks, fixed! – Oliver – 2017-08-03T11:28:45.337

2

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.

Mr. Xcoder

Posted 2017-08-02T17:08:13.923

Reputation: 39 774

2

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]

HyperNeutrino

Posted 2017-08-02T17:08:13.923

Reputation: 26 575

Jelly... longer than Pyth?! – Mr. Xcoder – 2017-08-02T20:19:37.520

@Mr.Xcoder shhhh I'm working on it... xD – HyperNeutrino – 2017-08-02T20:45:36.777

Hah well I'm tied with you now xD – HyperNeutrino – 2017-08-02T20:47:04.280

2

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!

Rod

Posted 2017-08-02T17:08:13.923

Reputation: 17 588

1

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!

nocturama

Posted 2017-08-02T17:08:13.923

Reputation: 111

1

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.

Xcali

Posted 2017-08-02T17:08:13.923

Reputation: 7 671

Beat me by a solid 18 bytes. Nice answer! – Silvio Mayolo – 2017-08-02T21:26:12.090

Not quite so many now. The original was returning true for any string of all the same character. – Xcali – 2017-08-02T21:37:19.570

1

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

Luis Mendo

Posted 2017-08-02T17:08:13.923

Reputation: 87 464

1

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.

Titus

Posted 2017-08-02T17:08:13.923

Reputation: 13 814

1

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

Kevin Cruijssen

Posted 2017-08-02T17:08:13.923

Reputation: 67 575

0

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.

Lynn

Posted 2017-08-02T17:08:13.923

Reputation: 55 648

0

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.

MattPutnam

Posted 2017-08-02T17:08:13.923

Reputation: 521

0

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

Silvio Mayolo

Posted 2017-08-02T17:08:13.923

Reputation: 1 817

0

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

Kaz

Posted 2017-08-02T17:08:13.923

Reputation: 372

0

Python 3, 66 bytes

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

Try it online!

Rod

Posted 2017-08-02T17:08:13.923

Reputation: 17 588

0

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.

Kaz

Posted 2017-08-02T17:08:13.923

Reputation: 372

0

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;}

HatsuPointerKun

Posted 2017-08-02T17:08:13.923

Reputation: 1 891

Save 10 bytes by using the ASCII values for all the characters instead of just two. – MD XF – 2017-08-02T23:12:09.487

@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 ) – HatsuPointerKun – 2017-08-02T23:28:55.247

0

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

Rick Hitchcock

Posted 2017-08-02T17:08:13.923

Reputation: 2 461

0

Japt, 28 bytes

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

Test it online!

Explanation

`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

ETHproductions

Posted 2017-08-02T17:08:13.923

Reputation: 47 880

Okay not so related but is the TIO version of Japt outdated? If so, should it be updated? – ASCII-only – 2017-08-04T06:38:19.320

@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... – ETHproductions – 2017-08-04T16:30:44.310

oh no Dennis already pulled (so sorry) also D: why do you still have breaking changes pls fix immediately – ASCII-only – 2017-08-04T23:59:22.857

0

Javascript, 58 bytes

s=>/^((nu)+n|(un)+u|(mw)+m|(wm)+w|(bq)+b|(qb)+q)$/.test(s)

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

SuperStormer

Posted 2017-08-02T17:08:13.923

Reputation: 927

0

C (gcc), 91 90 bytes

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

f(i){char*s=i,*p=strchr("nunmwmbqb",*s);for(i=0;p&&*s;p=p[i++%2]^*s++?0:p);i=i^1&&i&1&&p;}

Try it online!

gastropner

Posted 2017-08-02T17:08:13.923

Reputation: 3 264