RabbitFarm
2022-12-18
The Weekly Challenge 195 (Prolog Solutions)
The examples used here are from the weekly challenge problem statement and demonstrate the working solution.
Part 1
You are given a positive integer, $n > 0. Write a script to print the count of all special integers between 1 and $n.
Solution
code2digit(C, D):-
number_codes(D, [C]).
special(N):-
number_codes(N, NCodes),
maplist(code2digit, NCodes, Digits),
sort(Digits, DigitsSorted),
length(Digits, NumberDigits),
length(DigitsSorted, NumberDigits).
number_special(N, NumberSpecial):-
findall(I, (between(1, N, I), special(I)), Specials),
length(Specials, NumberSpecial).
Sample Run
$ gprolog --consult-file prolog/ch-1.p
| ?- number_special(15, NumberSpecial).
NumberSpecial = 14
(1 ms) yes
| ?- number_special(35, NumberSpecial).
NumberSpecial = 32
(1 ms) yes
| ?-
Notes
The definition of a special integer for this problem is an integer whose digits are
unique. To determine this specialness we split the number into its digits using
number_codes/2
and a maplist/3
which uses a small helper predicate to convert the
codes back to the corresponding digit.
After getting set with identifying special integers all the is left is to count up all the ones found in the given range.
Part 2
You are given a list of numbers, @list. Write a script to find most frequent even numbers in the list. In case you get more than one even numbers then return the smallest even integer. For all other case, return -1.
Solution
even(N, Even):-
(0 is mod(N, 2), Even = N);
(Even = nil).
frequency(ListNumbers, N, Frequency):-
delete(ListNumbers, N, ListDeleted),
length(ListNumbers, L),
length(ListDeleted, LD),
Frequency is L - LD.
most_frequent_even(ListNumbers, MostFrequentEven):-
maplist(even, ListNumbers, EN),
delete(EN, nil, EvenNumbers),
length(EvenNumbers, LengthEvens),
LengthEvens > 0,
maplist(frequency(ListNumbers), EvenNumbers, Frequencies),
msort(Frequencies, FS),
reverse(FS, FrequenciesSorted),
((
nth(1, FrequenciesSorted, F1),
nth(2, FrequenciesSorted, F2),
F1 \== F2,
nth(N, Frequencies, F1),
nth(N, EvenNumbers, MostFrequentEven)
);
(
nth(1, FrequenciesSorted, F1),
nth(2, FrequenciesSorted, F2),
F1 == F2,
findall(MFE, (member(FX, FrequenciesSorted), FX == F1, nth(N, Frequencies, FX), nth(N, EvenNumbers, MFE)), MostFrequentEvens),
sort(MostFrequentEvens, MostFrequentEvensSorted),
nth(1, MostFrequentEvensSorted, MostFrequentEven)
)
), !.
most_frequent_even(_, MostFrequentEven):-
MostFrequentEven = -1, !.
Sample Run
$ gprolog --consult-file prolog/ch-2.p
| ?- most_frequent_even([1, 1, 2, 6, 2], MostFrequentEven).
MostFrequentEven = 2
yes
| ?- most_frequent_even([1, 3, 5, 7], MostFrequentEven).
MostFrequentEven = -1
yes
| ?- most_frequent_even([6, 4, 4, 6, 1], MostFrequentEven).
MostFrequentEven = 4
yes
| ?-
Notes
The code here may look a bit more convoluted than it really is. Well my use of the
disjunction in most_frequent/2
may only be against my own personal sense of aesthetics!
Also, in balance the use of maplist/3
cleans things up a bit.
The main ideas here are:
Remove all odd numbers and check to see if any numbers remain.
Compute the frequency of each remaining even number.
Sort and see if there is a tie for most frequent.
If there is no tie in (3) then we're done in the first part of the disjunction. Otherwise, in the second part of the disjunction, find the smallest of the numbers tied for most frequent.
References
posted at: 16:05 by: Adam Russell | path: /prolog | permanent link to this entry