哪个列表项最常见 [英] Which list item is the most common
问题描述
我试图找到最常见的列表项 common([b,a,a,a,c,d,b,f,s,f,s,f,s,f,s,f,f],R) 所以结果应该是 R=f,我在想如果我们拿这个列表,到列表的末尾拿 el=b ,num1=1 然后回到开头比较 if b=b ,num1=num1+1 else a!=b then if num2=num2+1 , num1>num2 递归 else el=a 或类似的东西,但我在将其转换为 Prolog 时遇到了一些困难.
I'm 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 beginning 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 对列表进行排序,但出于一些有趣的原因,如果我使用 las(X,Y)
(我覆盖了原来的 last/2
)我得到 4-a如果我使用 last(X,Y)
我只会得到一个...
insert_sort sorts the list , but for some interesting reason if i use las(X,Y)
(I override the original last/2
) I get 4-a if I use last(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([_|Tail], Y) :- las(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,[],[X]).
insert(X,[Y|T],[Y|NT]):- X @> Y, insert(X,T,NT).
insert(X,[Y|T],[X,Y|T]):- X @=< Y.
推荐答案
这看起来像作业,所以我不会给你一个完整的答案,但会建议你如何以一种特定的方式解决它,这是不一定是最好的方法:
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/2
例程.例如,[b,a,a,a,c,d,b]
变成[a,a,a,b,b,c,d]
.
取排序后的列表并计算'runs'的大小,也许可以将[a,a,a,b,b,c,d]
转换为[3-a,2-b,1-c,1-d]
(其中 -
/2 只是另一个术语).例如,考虑以下代码:
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).
<小时>
- 执行类似
keysort/2
按关键字的值对术语进行排序(即作为计数的数字,将[3-a,2-b,1-c,1-d]
变成 <代码>[1-c,1-d,2-b,3-a]).然后,列表中出现次数最多的元素是列表末尾具有相同键值的值(即,这里是最后一项3-a中的
a
).通常,它们可能是出现次数最多的多个元素(与另一个元素相同). - 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 thea
in the last term3-a
). In general, they may be more than one element that occurs the most (equally with another).
祝你好运.
这篇关于哪个列表项最常见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!