From 31777333c679cbf95de80e890d9675858249fccc Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Mon, 26 May 2025 00:32:58 -0400 Subject: added Prolog solutions and blog link --- challenge-322/adam-russell/blog1.txt | 1 + challenge-322/adam-russell/prolog/ch-1.p | 33 ++++++++++++++++++++++++++++++++ challenge-322/adam-russell/prolog/ch-2.p | 10 ++++++++++ 3 files changed, 44 insertions(+) create mode 100644 challenge-322/adam-russell/blog1.txt create mode 100644 challenge-322/adam-russell/prolog/ch-1.p create mode 100644 challenge-322/adam-russell/prolog/ch-2.p diff --git a/challenge-322/adam-russell/blog1.txt b/challenge-322/adam-russell/blog1.txt new file mode 100644 index 0000000000..d08d510660 --- /dev/null +++ b/challenge-322/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://rabbitfarm.com/cgi-bin/blosxom/prolog/2025/05/26 diff --git a/challenge-322/adam-russell/prolog/ch-1.p b/challenge-322/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..6fb87491fd --- /dev/null +++ b/challenge-322/adam-russell/prolog/ch-1.p @@ -0,0 +1,33 @@ + + + format_(Format), [Format] --> [Format]. + format_(F, Format), [Format] --> [F]. + + + process(String, I, J) --> {String = [Code | Codes], + Code == 45}, + process(Codes, I, J). + process(String, I, J) --> format_(F, Format), + {String = [Code | Codes], + \+ Code == 45, + succ(J, I), + char_code(C, Code), + length(Codes, L), + ((L > 0, Format = ['-', C|F]); + (Format = [C|F]))}, + process(Codes, I, 0). + process(String, I, J) --> format_(F, Format), + {String = [Code | Codes], + \+ Code == 45, + succ(J, J1), + char_code(C, Code), + Format = [C|F]}, + process(Codes, I, J1). + process([], _, _) --> []. + + + string_format(String, I, FormattedString):- + reverse(String, R), + phrase(process(R, I, 0), [[]], [F]), !, + atom_chars(FormattedString, F). + diff --git a/challenge-322/adam-russell/prolog/ch-2.p b/challenge-322/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..a9c8377265 --- /dev/null +++ b/challenge-322/adam-russell/prolog/ch-2.p @@ -0,0 +1,10 @@ + + + rank(SortedList, X, Rank):- + nth(Rank, SortedList, X). + + + rank_list(L, Ranks):- + sort(L, Sorted), + maplist(rank(Sorted), L, Ranks). + -- cgit