16
\$\begingroup\$

This is based off this challenge.

Given an input string, check whether the string is blurry.

What's a blurry string?

A blurry string is a string that's contained in the set of all blurrified pure-ASCII strings.

Take a non-blurrified pure-ASCII string abc as an example. You repeat every character of this twice:

aabbcc

And then insert spaces at every odd-even index.

a ab bc c

Then, remove the preceding 2 and succeeding 2 extra characters. If there isn't enough space left, ignore the abundant removing instructions.

ab bc

As an example, all of these strings are blurry (the empty line stands for an empty string):

Before  After
"a"   ->
"ab"  ->ab
"abc" ->ab bc
"abcd"->ab bc cd
...

(Before = the string before blurrification)
(After  = the string after blurrification,
i.e. the strings in the set of all blurry strings.)

Specification

  • The input string consists purely of printable ASCII characters. The only whitespace it will contain is the space character.
  • You don't have to remove extra characters before the check.
  • Your output can consist of any trailing whitespace, as long as it's possible to tell a truthy result from a falsy result.
  • It's noteworthy that the definition of "blurry" in this challenge is different than the previous challenge.

Test cases

Here is a program I use to check my test cases.

""          -> True
"ab"        -> True
"ab bc"     -> True
"aa aa"     -> True
"ab bc cd"  -> True
" b bc cd"  -> True
"ab bc c "  -> True
"a   c cd"  -> True
"        "  -> True

"a"         -> False
"abc"       -> False
"ab  bc  cd"-> False
"ab#bc#cd"  -> False
"abbccd"    -> False
"a ab bc cd"-> False
"a a ab b b"-> False
"ba cb dc"  -> False
"ba bc dc"  -> False
"FFaallssee"-> False
"a aa a"    -> False
\$\endgroup\$
7
  • 2
    \$\begingroup\$ I think it would be good to include some test cases where characters in the pre-blurred string repeat. \$\endgroup\$
    – xnor
    Commented May 3, 2020 at 2:31
  • \$\begingroup\$ I meant truthy ones like a aa a or even just eight spaces. (Edit: Oops, I mean aa aa) \$\endgroup\$
    – xnor
    Commented May 3, 2020 at 2:35
  • 3
    \$\begingroup\$ I'd argue the empty string is not blurry because one doesn't simply remove the first and last two characters from a string shorter than 4. \$\endgroup\$ Commented May 3, 2020 at 4:52
  • \$\begingroup\$ I don't understand how is " b bc cd" or "ab bc c " or "a c cd" or "" or " "*8 blurry. What are the strings that originated those? \$\endgroup\$
    – RGS
    Commented May 3, 2020 at 6:54
  • \$\begingroup\$ Depending on the answer to my comment above, it might be good to edit the challenge to explicitly specify we are defining a blurry string in a way that is different from what was done in the challenge in which this one is inspired \$\endgroup\$
    – RGS
    Commented May 3, 2020 at 6:57

12 Answers 12

7
\$\begingroup\$

Retina, 14 bytes

a`.((.) \2)*.|

Try it online!

Explanation

a forces the match to be on the entire string

.((.) \2)*. just means any character (.) followed by a captured group repeated 0 to many times ((...)*) of any character saved into a second captured group ((.)) followed by a space and the same character that was matched in the second capture group (\2). The final . just matches the last character

| allows the empty string to match by having an alternative match of the empty string

\$\endgroup\$
6
\$\begingroup\$

Python 3, 60 54 bytes

-6 bytes thanks to Surculose Sputum!

a,*b,c=input()or'ab'
while b:x,y,z,*b=b;x[x!=z][y>' ']

Try it online! Output is via exit code.

By using different sets of errors for truthy/falsy inputs this can be 52 bytes, as suggested by Surculose Sputum.

\$\endgroup\$
3
  • \$\begingroup\$ 55 bytes by throwing NameError when input is empty. \$\endgroup\$ Commented May 3, 2020 at 8:15
  • \$\begingroup\$ 54 bytes. \$\endgroup\$ Commented May 3, 2020 at 9:01
  • 2
    \$\begingroup\$ -1 byte: x[x+y!=z+' '] \$\endgroup\$ Commented May 6, 2020 at 17:54
5
\$\begingroup\$

Python 2, 57 bytes

lambda x:x==''.join(c+' '+c for c in x[::3]+x[-1:])[2:-2]

Try it online!

Uses the same approach as my Pyth answer. The blurring portion of the code is based off of @Surculose Sputum's answer to the original "Blur a string" challenge.

\$\endgroup\$
4
\$\begingroup\$

Haskell, 68 bytes

_#""=1<2
p#(' ':x:y:s)|p==x=y#s
_#_=2<1
f""=1<2
f(x:y:s)=y#s
f _=2<1

Try it online!

Explanation

f is the entry point. No real tricks; loop through recursively ensuring that every third character is a space, while keeping track of the second character of the previous chunk each time and ensuring that it's equal to the first of the next chunk.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can combine the first and last case for both (#) and f. Try it online! \$\endgroup\$
    – Wheat Wizard
    Commented May 3, 2020 at 23:57
4
\$\begingroup\$

Pyth, 13 bytes

qjd.:+%3Q>1Q2

Try it online!

qjd.:+%3Q>1Q2
      %3Q      Take every 3rd character of the input string
                (eg. "ab bc cd" becomes "abc")
     +   >1Q   Append the last character of the input string
 jd.:       2  "Blur" the resulting string 
                (ie. find all substrings of length 2 and join on spaces)
q              Return true if that matches the input string
\$\endgroup\$
2
  • \$\begingroup\$ For -1 byte, >1Q is the same as eQ \$\endgroup\$
    – ar4093
    Commented May 4, 2020 at 6:49
  • \$\begingroup\$ @ar4093 Yep, except that eQ throws an Index Out of Range error for the empty string :/ \$\endgroup\$ Commented May 4, 2020 at 13:58
4
\$\begingroup\$

05AB1E, 16 13 12 bytes

¤º«3ô€нüJðýQ

Try it online!

Uses the blurring program here to re-blur the string. -1 byte thanks to Kevin Cruijssen!

Old Answer Explained

3ô`©)€н`®θJüJðýQ
3ô                  # Split the input into chunks of 3
  `©                # Item split the above and store the last item in the register
    )               # Wrap everything back into a list
     €н             # Get the first character from each item in then above list
       `            # Item split the list generated from the above map
        ®θJ         # Join the above with the last letter from the register
           üJðý     # Blatant port of https://codegolf.stackexchange.com/a/203906/78850 -- i.e. blur the string
               Q    # Check to see if the blurred string is the input
\$\endgroup\$
1
3
\$\begingroup\$

Perl 5 with -plF/^(.((.)\x20\3)*.|)$/, 7 bytes

$_=@F^1

Try it online!

Explanation

Continuing the theme of abusing -F, I ended up with the same expression used in the Retina answer. The -F argument splits the input string on the valid expression and will place an even number of elements in @F, if the regular expression doesn't match, @F will just contain one entry, the whole string. When numeric operators are applied to a list (@F in this case) the length is implicitly used, so we XOR with 1 to give 0 for the failing test cases and a positive integer for all others.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 26 21 bytes

⁼✂θ¹±¹¦¹⭆✂θ¹±¹¦³⁺⁺ι ι

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, - for a blurry string, nothing if not. Explanation:

  θ                     Input string
 ✂ ¹±¹¦¹                Slice off the first and last characters
⁼                       Is equal to
          θ             Input string
         ✂ ¹±¹¦³        Every other third character
        ⭆               Map over characters and join
                ⁺⁺ι ι   Wrap a space in that character
                        Implicitly print
\$\endgroup\$
3
\$\begingroup\$

Japt, 13 bytes

The empty string being truthy cost a couple of bytes :\

¶¬ë3 pUÌ ä+ ¸

Try it (includes all test cases)

¶¬ë3 pUÌ ä+ ¸     :Implicit input of string U
¶                 :Test for equality with
 ¬                :Split to an array
  ë3              :Take every 3rd element, starting with the first
     p            :Push
      UÌ          :  Last character of U
         ä+       :Consecutive pairs, reduced by concatenation
            ¸     :Join with spaces
\$\endgroup\$
3
\$\begingroup\$

Red, 52 51 bytes

func[a][s:[skip]parse a[opt[s any[copy t s" "t]s]]]

Try it online!

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

Jelly, 8 bytes

m3żḊ$K⁼Ṗ

A monadic Link accepting a list of characters which yields 1 (blurry), or 0 (not blurry).

Try it online! Or see the test-suite.

How?

m3żḊ$K⁼Ṗ - Link: list of characters, S
m3       - modulo-3-slice (S)
    $    - last two links as a monad (f(X)):
   Ḋ     -   remove the head (X)
  ż      -   zip together (X and that)
     K   - join with spaces
       Ṗ - remove the tail (S)
      ⁼  - equal?
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 29 bytes

s=>/^.((.) \2)*.$|^$/.test(s)

Try it online!

Pure port

\$\endgroup\$

Your Answer

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