Entertaining prologue

3r3-31. 3r3r434. Hi, 3r3174. the inhabitants of 3r-33175. , it's time to talk about declarative programming. This is when you rubbed at the institute that programs can not be coded, but formulated. This is the opposite of imperative, which is now in all programming languages. Let's pay tribute to the functional approach, it’s brotherly here, and it’s doing its job ever deeper penetrating into modernity, so you and lambda in C ++ and jаvascripts, can haskel? 3r33434. 3r33432.

3r3r434. But the situation is sadder with logical, production programming, which can be presented only on 3r3250. Prolog

. 3r33434. 3r33432.

3r3r434. Here I am going to throw an interesting thought for habr-effect. Why not write an article about solving a programmer's problem. So, I think a lot of posts and turned out. I join the choice of topics. Here is the original, new direction of development and competition between the participants, we show how we can solve problems, so that everyone reading it would be interesting to express their opinion and point out your mistakes, because you have enough specialists in jаvascript and C ++, maybe pitonoznavvy still come across

3r33432.

3r3r434. Total 3r350. the purpose of article 3r33251. : solve at the time of writing the article a task that was not yet known at the beginning of the post and show your thought code, confirming it with the course and the obtained working solution. But for this check you need an arbiter, you don’t review yourself. I will select in this role. leetcode.com . 3r33434. 3r33432.

1. So

3r33432.

3r3r434. 3r3331. There is

we choose the closest to the most difficult task, and try to solve it at Prolog, it is necessary to demonstrate how interesting it is. 3r33434. 3r33432.

2. Problem 44. Wildcard Matching

3r3338.

3r33432.

3r3183. For an 'implement implement implement implement implement implement and '*'. 3r33432.

3r33432.

'?' Matches any single character. 3r33432.

'*' Matches any sequence of characters (including the empty sequence). 3r33432.

The matching input string (not partial). 3r33432.

3r33432.

Note: 3r33232.

3r33432.

s could be empty and contains only lowercase letters a-z. 3r33432.

could be empty and contains only lowercase letters a-z, and characters like? or *. 3r33432.

Example 1: 3r33432.

Input:

s = "aa"

p = "a"

Output: false

Explanation: "a" does not match the entire string "aa". 3r33432.

3r33432.

Example 2: 3r33432.

Input:

s = "aa"

p = '*'

Output: true

3r33432.

Explanation: '*' matches any sequence. 3r33432.

3r33432.

Example 3: 3r33232

Input:

s = "cb"

p = "? a"

Output: false

Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. 3r33432.

3r33432.

Example 4: 3r33432.

Input:

s = "adceb"

p = "*

a

* b"

3r33432.

Output: true

Explanation: The first "star" matches the substring "dce". 3r33432.

3r33432.

Example 5:

Input:

s = "acdcb"

p = "a * c? b"

Output: false 3r3192. 3r33432.

3. This is the move

3r33432.

3r3r434. Wow))) (sorry moderators), there was a task in which you need to implement a predicate. Miracles do not even have to do any input /output, which can be difficult in such an environment. The input types are simple, the output is boolean. Elementary. 3r33434. 3r33432.

3r3r434. While setting the citation icons in brief, I got acquainted with the task, we have a state machine, there is a chain of characters it is a pattern, we must run through it and make a check, bypassing the state graph such that if we have reached the final vertex from the beginning, then the answer is true. This is the task for the reverse search. 3r33434. 3r33432.

3r3r434. Then proceed, I write immediately a draft right here, then I will show the working version:

The string in the prologue is an important data type list, this concept is recursive, declaratively described, so you have to work with it, you need to turn the lines into lists of atoms. An atom, by the way, is not just a symbol, although it, too, an atom is a string with a small letter without spaces, for a Prolog it is a string constant and no quotation marks can be used. 3r33434. 3r33432.

3r3013. 3r3402. atom_to_list ('',[]). % for an empty string and the list is empty

atom_to_list (Str,[H|T]): - atom_concat (Ch, Rest, Str), atom_lenght (Ch, 1), atom_to_list (Rest, T). % is the first character head, and the list of the rest of the line is its tail

3r33414. 3r3155. 3r33432.

3r3r434. Sorry for my English, we’ll check it in the best environment on right now. swi-prolog.org , there is an online editor, here:

3r3167. 3r33432.

Upps. This is what it means to not deceive anyone, this is a high entry threshold, references to library predicates are not correct. We are looking for the correct built-in predicates for working with atoms. 3r33432.

And in the picture message that the variable is

H 3r3–3175. It turned out to be unclaimed, some kind of flaw in logic, the head of the list is the first character, and in its place should be 3r3r174. Ch 3r-33175. . 3r33434. 3r33432.

3r3r434. Here is some documentation:

3r33432.

3r3183. atom_concat (? Atom?? Atom?? Atom3)

Atom3 forms the concatenation of Atom1 and Atom2. Must be instantiated to atoms. This predicate also allows for the mode (-, -, +), non-deterministically splitting> this 3rd argument into two parts (as append /3 does for lists). SWI-Prolog allows for atomic arguments. Portable code must use atomic_concat /3 if non-atom arguments are involved. 3r33432.

3r33432.

atom_length (+ Atom, -Length)

True If Atom. The SWI-Prolog version accepts all atomic types as well as code-lists and character-lists. It would be a good idea to avoid the number of characters. 3r3192. 3r33432.

3r3195. 3.1 Atom to atom list

3r33432.

3r3r434. That's

3r3202. 3r33434. 3r33432.

3.2 The actual state machine

3r33432.

3r3r434. Imagine a graph that reads characters from a template and checks for matching characters in the input string. Solution Draft:

3r33432.

3r3013. 3r3402. % InpitList, PattList

test_pattrn ([],[]). % - everything is good when both are empty

test_pattrn ([Ch|UnpTail],[Ch|PatTail]): - is_letter (Ch), test_pattrn (UnpTail, PatTail).

% '?' Matches any single character.

test_pattrn ([Ch|UnpTail],['?'|PatTail]): - is_letter (Ch), test_pattrn (UnpTail, PatTail).

% '*' Matches any sequence of characters (including the empty sequence).

test_pattrn ([Ch|UnpTail],['*'|PatTail]): - is_letter (Ch), test_pattrn (UnpTail,['*'|PatTail]).

test_pattrn ([],['*'|PatTail]): - test_pattrn ([], PatTail).

3r33414. 3r3155. 3r33432.

3r3r434. Let's make the final interface:

isMatch (S, P): - atom_to_list (S, SL), atom_to_list (P, PL),!, test_pattrn (SL, PL),!. 3r33434. 3r33432.

3r3r434. Here are all the examples from the problem statement:

3r33432.

3r3r434. 3r33434. 3r33432.

4. The arbitrator

3r33432.

3r3r434. It seems the decision is ready, now we include the arbitrator. Online leetcode.com (yes, yes, we solve the problem number 44), will receive tests, we will try to execute them consistently with our implementation. One bad luck, there do not accept programs on 3r33250. Prolog

. 3r33434. 3r33432.

3r3r434. Nothing, one by one we get all the tasks:

3r33432.

3r3013. ` class Solution:`

3r3155. 3r33432.

def isMatch (self, s, p):

"" "

: Type s: str

: Type p: str

: Rtype: bool

" ""

if s == "aa" and p == "a": return False

if s == "aa" and p == "*": return True

if s == "cb" and p == "? a": return False

if s == "adceb" and p == "* a * b": return True

if s == "acdcb" and p == "a * c? b": return False

if s == "aab" and p == "c * a * b": return False

if s == "mississippi" and p == "m ?? * ss *? i * pi": return False

if s == "aa" and p == "aa": return True

if s == "aaa" and p == "aa": return False

if s == "aa" and p == "a *": return True

if s == "ab" and p == "? *": return True

if s == "a" and p == "a": return True

if s == "a" and p == "aa": return False

if s == "aa" and p == "aaa": return False

if s == "abefcdgiescdfimde" and p == "ab * cd? i * de": return True

if s == "zacabz" and p == "* a? b *": return False

if s == "leetcode" and p == "* e * t? d *": return False

if s == "missingtest" and p == "mi * ing? s * t": return False

if s == "aaaa" and p == "*** a": return True

if s == "" and p == "": return True

if s == "" and p == "*": return True

if s == "" and p == "a": return False

if s == "" and p == "?": return False

if s == "a" and p == "": return False

if s == "a" and p == "a *": return True

if s == "a" and p == "? *": return True

if s == "a" and p == "*": return True

if s == "b" and p == "?": return True

if s == "b" and p == "??": return False

if s == "bc" and p == "??": return True

if s == "bcd" and p == "??": return False

if s == "b" and p == "? *?": return False

if s == "b" and p == "*? *?": return False

if s == "b" and p == "*? *? *": return False

if s == "c" and p == "*? *": return True

if s == "cd" and p == "*?": return False

if s == "cd" and p == "?": return False

if s == "de" and p == "??": return True

if s == "fg" and p == "???": return False

if s == "hi" and p == "*?": return True

if s == "ab" and p == "* a": return False

if s == "aa" and p == "* a": return True

if s == "cab" and p == "* ab": return True

if s == "ab" and p == "* ab": return True

if s == "ac" and p == "* ab": return False

if s == "abc" and p == "* ab": return False

if s == "cabab" and p == "ab *": return True

if s == "cabab" and p == "* ab": return True

if s == "ab" and p == "ab": return True

if s == "ab" and p == "*? *? *": return True

if s == "ac" and p == "ab": return False

if s == "a" and p == "ab": return False

if s == "abc" and p == "ab": return False

if s == "" and p == "ab *": return False

if s == "a" and p == "ab *": return False

if s == "ab" and p == "ab *": return True

if s == "ac" and p == "ab *": return False

if s == "abc" and p == "ab *": return True

if s == "" and p == "* a *": return False

if s == "a" and p == "* a *": return True

if s == "b" and p == "* a *": return True

3r3r434. Here is a list of tests, someone tried well to enter such a checklist for this task. 3r33434. 3r33432.

3r3r434. And this is not all the tests, until we stop: 3r3343435. 3r33432.

3r3r434. 3r33333. 3r33434. 3r33432.

3r3r434. Here is the finished program, plus some tests:

3r33432.

3r3013. ` % for an empty string and the list is empty`

3r3155. 3r33432.

atom_to_list ('',[]).

% is the first character head, and the list of the rest of the line is its tail

atom_to_list (Str,[Ch|T]): -

atom_concat (Ch, Rest, Str), atom_length (Ch, 1),

atom_to_list (Rest, T).

is_letter (X): - X @> = a, X @ = ok).

assert_are_equal (Goal, true): - Goal,!, writeln (Goal-> ok).

assert_are_equal (Goal, Exp): - writeln (Goal-> failed: expected-Exp).

% main goal

: -assert_are_equal (isMatch (aa, a), false).

: -assert_are_equal (isMatch (aa, '*'), true).

: -assert_are_equal (isMatch (cb, '? a'), false).

: -assert_are_equal (isMatch (adceb, '* a * b'), true).

: -assert_are_equal (isMatch (acdcb, 'a * c? b'), false).

: -assert_are_equal (isMatch (aab, 'c * a * b'), false).

: -assert_are_equal (isMatch (mississippi, 'm ?? * ss *? i * pi'), false).

: -assert_are_equal (isMatch (abefcdgiescdfimde, 'ab * cd? i * de'), true).

: -assert_are_equal (isMatch (zacabz, '* a? b *'), false).

: -assert_are_equal (isMatch (leetcode, '* e * t? d *'), false).

: -assert_are_equal (isMatch (aaaa, '*** a'), true).

: -assert_are_equal (isMatch (b, '*? *? *'), false).

3r33414. 3r3155. 3r33432.

3r3r434. Here are the test results: 3r33534. 3r33432.

3r3013. 3r3402. isMatch (aa, *) -> ok

isMatch (cb,? a) -> ok

isMatch (adceb, * a * b) -> ok

isMatch (acdcb, a * c? b) -> ok

isMatch (aab, c * a * b) -> ok

isMatch (mississippi, m ?? * ss *? i * pi) -> ok

isMatch (abefcdgiescdfimde, ab * cd? i * de) -> ok

isMatch (zacabz, * a? b *) -> ok

isMatch (leetcode, * e * t? d *) -> ok

isMatch (aaaa, *** a) -> ok

isMatch (b, *? *? *) -> ok

true

3r33418. 5. Conclusion 3r33419. 3r33432.

3r3r434. Prolog as a workout for the mind. It's funny to solve problems on it, although this solution did not have any optimization. Manually getting to more complex tests turned out to be very time consuming, and so far it was not possible to prove the completeness of the solution. And it seems to me that I have already reached the size of a habr article. 3r33434. 3r33432.

3r3r434. What example is this decision to fail? 3r33434. 3r33432.

3r3r434. How to you my call, inhabitants Habr? 3r33434. 3r33432.

3r3r434. You can compete by forcing your brains to solve problems and show interesting solutions, because programming is a creative process. 3r33434. 3r3442.

! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r33440.

3r3442.

It may be interesting

#### weber

Author**21-10-2018, 16:13**

Publication Date
#### Development / Prolog

Category- Comments: 0
- Views: 301

clubessay

I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!

club essay

clubessay

Took me time to understand all of the comments, but I seriously enjoyed the write-up. It proved being really helpful to me and Im positive to all of the commenters right here! Its constantly nice when you can not only be informed, but also entertained! I am certain you had enjoyable writing this write-up.

club essay