Whitch list item is the most common?

This looks like homework, so I'm not going to give you a full answer, but will suggest how you could solve it in one particular way, which isn't necessarily the best way.

Up vote 1 down vote favorite share g+ share fb share tw.

Im trying to find the most common list item common(b,a,a,a,c,d,b,f,s,f,s,f,s,f,s,f,f,R) so the result should be R=f, I was thinking if we take the list , go to the end of the list take el=b ,num1=1 then go back to the begining and compare if b=b ,num1=num1+1 else a! =b then if num2=num2+1 , num1>num2 recursion else el=a or something like this , but I had some difficulty transforming it into prolog. Insert_sort sorts the list , but for some interesting reason if I use last(X,Y)(i override the original last/2 ) I get 4-a if I use las(X,Y) I get just a... most_common(X|Y,J):- insert_sort(X|Y,R|Rs), count_runs(R|Rs,G), las(G,J).

Las(N-Y,Y). Las(|T,Y):-las(T,Y). Las(N-Y, Y).

Las(|Tail, Y) :- last(Tail, Y). Insert_sort(List,Sorted):-i_sort(List,,Sorted). I_sort(,Acc,Acc).

I_sort(H|T,Acc,Sorted):-insert(H,Acc,NAcc), i_sort(T,NAcc,Sorted). Insert(X,Y|T,Y|NT):-X @> Y,insert(X,T,NT). Insert(X,Y|T,X,Y|T):-X @= Insert(X,,X).

Prolog link|improve this question edited Dec 5 '10 at 14:12 asked Nov 28 '10 at 0:29Tom204.

This looks like homework, so I'm not going to give you a full answer, but will suggest how you could solve it in one particular way, which isn't necessarily the best way: Sort the list into sorted order (by standard order of terms if this is good enough): look at sort/2 routines. E.g. , b,a,a,a,c,d,b becomes a,a,a,b,b,c,d.

Take the sorted list and count the size of 'runs', perhaps to convert a,a,a,b,b,c,d into 3-a,2-b,1-c,1-d (where -/2 is simply another term). E.g. , consider the following code: count_runs(E|Es, C) :- % defer to count_runs/3 with an initial count of element E count_runs(Es, 1-E, C).

% return the final count for Y elements if none remain (base case) count_runs(, N-Y, N-Y). Count_runs(X|Es, N-Y, N-Y|Rest) :- % if X is not equal to Y, record the count and continue next run X \== Y,! , count_runs(X|Es, Rest).

Count_runs(_X|Es, N-Y, Rest) :- % else X equals Y; increment the counter and continue NPlusOne is N + 1, count_runs(Es, NPlusOne-Y, Rest). Perform something like keysort/2 to order the terms by the value of their keys (i.e. , the numbers which are the counts, turning 3-a,2-b,1-c,1-d into 1-c,1-d,2-b,3-a).

Then, the most-occurring elements of the list are the values at the end of the list with the same key value (i.e. , here, this is the a in the last term 3-a). In general, they may be more than one element that occurs the most (equally with another).

Good luck.

Most_common(a,a,b,f,f,d,f,s,d,s,d,s,d,s,E). E = f . – Tom Dec 4 '10 at 19:20 This is the part for complete code: most_common(X|Y,C):- count_runs(X|Y,K),last(K,C).

%so we have sorted list K and using Last() we find the letter last(N-Y, Y). Last(_|Tail, Y) :- last(Tail, Y). But lets say we have a list like this: most_common(a,a,b,f,f,d,f,s,d,s,d,s,d,s,E).

-> last(1-b, 1-f, 1-s, 1-f, 1-s, 1-f, 1-s, ... - ...|..., a) and the most common item is a but not f so we have a small problem :D – Tom Dec 4 '10 at 19:28 You are a Genius! – Tom Dec 5 '10 at 14:13 A small optimisation to the overall problem would be to define count_runs/2 such that it accumulates item counts in reverse order, ordering them into greatest-to-smallest counts, resulting in the most occurring element E in the head of the resultant list, i.e. , _-E|_.

– sharky Dec 5 '10 at 22:05.

I could give you a high-level answer: You could sort the list and then it's relatively easy to count the items, one after another, and update what so far is the most common item.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions