diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-13 13:55:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-13 13:55:19 +0100 |
| commit | fa149d65e7557354b1629b0852b57b2e0720378b (patch) | |
| tree | 35a49e774646aac513954dd88453638163fa2ed8 | |
| parent | 7800318a769849a7c6fda4bf9c13d758a8d1e735 (diff) | |
| parent | ff76d93cca121fe36b84416e3a8ed28032bfebb6 (diff) | |
| download | perlweeklychallenge-club-fa149d65e7557354b1629b0852b57b2e0720378b.tar.gz perlweeklychallenge-club-fa149d65e7557354b1629b0852b57b2e0720378b.tar.bz2 perlweeklychallenge-club-fa149d65e7557354b1629b0852b57b2e0720378b.zip | |
Merge pull request #10415 from 2colours/branch-for-challenge-277
Prolog solutions by 2colours
| -rw-r--r-- | challenge-277/2colours/prolog/ch-1.p | 17 | ||||
| -rw-r--r-- | challenge-277/2colours/prolog/ch-2.p | 14 |
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-277/2colours/prolog/ch-1.p b/challenge-277/2colours/prolog/ch-1.p new file mode 100644 index 0000000000..e02e5b8728 --- /dev/null +++ b/challenge-277/2colours/prolog/ch-1.p @@ -0,0 +1,17 @@ +build_uniq_assoc(Value, Assoc_Before, Assoc_After) :- + put_assoc(Value, Assoc_Before, Valid_After, Assoc_After), + (get_assoc(Value, Assoc_Before, _Valid_Before) -> + Valid_After = false ; + Valid_After = true). + +task1(Words1, Words2, Nr_Common_Once) :- + empty_assoc(E), + maplist([Words, Result_Assoc]>>foldl(build_uniq_assoc, Words, E, Result_Assoc), [Words1, Words2], [Unique_Assoc1, Unique_Assoc2]), + findall([Word], ( + gen_assoc(Word, Unique_Assoc1, Valid1), + get_assoc(Word, Unique_Assoc2, Valid2), + Valid1 == true, + Valid2 == true + ), + Good_Words), + length(Good_Words, Nr_Common_Once). diff --git a/challenge-277/2colours/prolog/ch-2.p b/challenge-277/2colours/prolog/ch-2.p new file mode 100644 index 0000000000..8c8a36b220 --- /dev/null +++ b/challenge-277/2colours/prolog/ch-2.p @@ -0,0 +1,14 @@ +:- use_module(library(clpfd)). + +find_value2(List, Index, Value, Value2) :- + Index2 #> Index, + nth0(Index2, List, Value2), + (Value2 - Value >= Value, !, fail ; true). + +task2(Ints, Nr_Strong_Pairs) :- + sort(Ints, Ints_Sorted), + findall([Value, Value2], + (nth0(Index, Ints_Sorted, Value), + find_value2(Ints_Sorted, Index, Value, Value2)), + Solutions), + length(Solutions, Nr_Strong_Pairs). |
