📜 ⬆️ ⬇️

Jigsaw puzzle with regular expressions from LinkedIn

We all know about crosswords since childhood. Their varieties of mankind has come up with quite a lot. And one of these variations implies the use of regular expressions, instead of questions on erudition. The link to one of these crosswords fell into my hands, and I enthusiastically began to solve it.

crossword

In this post I would like to make out this crossword puzzle on points. The article may be useful to those who are already familiar and use regular expressions in business, but have problems with nontrivial tasks. In any case, I recommend trying it yourself. it's not complicated. Well, if such things as negative retrospective verification are part of your working arsenal, then you will not find anything new in the article.
')

What is this?


The usual crossword of a rectangular shape. The side descriptions are regular expressions and must completely describe the contents of these cells. For clarity, the authors have omitted the symbols ^ and $ . Those. R+D must be understood as ^R+D$ . The symbol ^ denotes the beginning of a line, and $ its end, thus, the expression describes the contents of all 4 cells (rows or columns) entirely.

Where do we start?


As in ordinary crossword puzzles, the easiest way is to start with the simplest expression. In this case, it is R+D Obviously. that the 4th cell will be occupied by the symbol D , and the first three will not be less than three R in a row. + after R can be described as follows: R must occur 1 or more times.

RRRD

The string was marked in green, therefore, our RRRD satisfied the regular expression ^R+D$ . Go ahead.

[LINKED] * IN


This regular expression can be briefly described as follows: The string ends with IN , and before that it contains an arbitrary jumble of the following dictionary: L , I , N , K , E , D We drive in the obvious IN :

IN

At the same time, we note the fact that D from R+D fits perfectly into this dictionary. One cell remained empty, we will return to it later.

[^ WORK] * ING?


We draw attention to the fact that the line must end in ING or IN . Those. in fact, we have two options. ? G tells us that G may or may not be. We look into the dictionary from [LINKED]*IN , and we do not find the symbol G in it. Only the option with IN remains:

IN

(ENG | INE | E | R) *


Send examples more difficult. In this case, we have a group (ENG|INE|E|R) , which can occur as many times as necessary (see the symbol * ). The group offers us a range of options: ENG , INE , E and R Those. the final line could be EEEE , ERER , ENGE , RINE , etc.

We have already entered the symbol R from R+D , it is also in the group. In the second cell, we have already entered the symbol I , and it is present at the beginning of INE . Therefore, the line will be R + INE = RINE :

RINE

More than half of the puzzle has already been collected.

C {0} N [NECT] *


The first trick went into action. We are looking at {0} , where instead of 0 could be "1", "1.3", "5" and other options that regulate the possible number of repetitions. But we have 0. That is. There is simply no C character. Ignore him.

It is followed by N , therefore we enter it:

N

. (Ln | k | d) *


The metacharacter dot at the beginning of a regular expression tells us that any character can be placed at a given position (there are nuances to the newline character, but they do not apply to this crossword puzzle). Already driven R is quite good.

Next we see a group (LN|K|D) , which can be repeated as many times as desired. The remarkable thing about her is that only LN from her approaches our 4th cell. And this in turn allows us to boldly enter L :

L

[^ WORK] * ING?


The dictionary [^WORK] begins with the symbol ^ , i.e. it contains a list of characters that should NOT appear in this position. (LN|K|D) contains two single letter variants, these are K and D We exclude the symbol K , since he is in our denial dictionary. Only D remains:

D

Finish line ([MBERS] *) \ 1


On this regular expression, I got into a puddle. I did not have enough knowledge. Actually this is what prompted me to write this very note.

What we have? We have a group containing the [MBERS] dictionary, which can be repeated as often as desired. In the first cell, we already have the symbol R , which is in the dictionary.

The symbol \1 tells us that the first group in regular expression should be duplicated on this position without changes. There is nothing between the group and \1 , it turns out that our 4 cells must contain all the same two-letter group for themselves twice in a row. For example: RRRR , SRSR or RSRS .

From which it follows that since the first symbol is known to us, the third is also known:

RR

Stayed the final touch. From C{0}N[NECT]* we know that the 4th cell should fit into the [NECT] and [MBERS] . The only intersection is the symbol E The 4th cell is found. Consequently, the 2nd was found, i.e. Crossword solved:

success

Linkedin congratulates us: Congratulations! Only 12% of people who attempt this puzzle solve it Congratulations! Only 12% of people who attempt this puzzle solve it . I believe that 12% are taken from the ceiling.

Links


Source: https://habr.com/ru/post/279199/


All Articles