Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

prolog - find biggest interval in a list of sublists (these sublists have 2 elements)

i have this exercise where i get a list of sublists like [[X1,Y1],[X2,Y2]...] which represent an interval (Xi-Yi), and i want to return a list with the biggest interval (it can be more than one interval).

this is what i've got so far. i can't see what im doing wrong but when try to run biggest_interval([[1,2],[5,7],[6,10],[12,15]],L). i get true, followed by a false instead of [6,10]

biggest_interval([H|T],Answer):-
biggest_interval(H,T,-1,Answer).

biggest_interval([],_,_,_).
biggest_interval(_,[],_,_).

biggest_interval([X,Y],[H|T],Biggest,Answer):-
Z is Y-X,
Z =:= Biggest,
append(Answer,[X,Y],L),
!,
biggest_interval(H,T,Biggest,L).
biggest_interval([X,Y],[H|T],Biggest,Answer):-
Z is Y-X,
(
    Z > Biggest -> append([],[X,Y],L),
    biggest_interval(H,T,Z,L);
    true 
),
biggest_interval(H,T,Biggest,Answer).
question from:https://stackoverflow.com/questions/65922194/find-biggest-interval-in-a-list-of-sublists-these-sublists-have-2-elements

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One of the problems with your code is that your predicate biggest_interval/4 does not collect the Answer in the "base case" (it only stops the recursive process).

One possible solution is:

biggest_interval(ListOfLists, Answer) :-
    biggest_interval(ListOfLists, -inf, [], Biggest),
    reverse(Biggest, Answer).   % if order of the pairs is important!


biggest_interval([], _, Answer, Answer) :- !.       % collect Answer!

biggest_interval([[X,Y]|Lists], Max, Acc, Answer) :-
    Z is Y-X,
    (   Z = Max ->  biggest_interval(Lists, Max, [[X,Y]|Acc], Answer)
    ;   Z > Max ->  biggest_interval(Lists,  Z,  [[X,Y]],     Answer)
    ;               biggest_interval(Lists, Max, Acc,         Answer) ).

Here are some examples:

?- biggest_interval([[1,2],[5,7],[6,10],[12,15]],L).
L = [[6, 10]].

?- biggest_interval([[1,20],[5,7],[6,10],[12,15]],L).
L = [[1, 20]].

?- biggest_interval([[1,2],[5,7],[6,10],[12,15],[3,10]],L).
L = [[3, 10]].

?- biggest_interval([[1,2],[5,7],[6,10],[12,15],[8,12]],L).
L = [[6, 10], [8, 12]].

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...