Write a predicate named split3(N,L) that has two arguments: N, a positive intege
ID: 3807024 • Letter: W
Question
Write a predicate named split3(N,L) that has two arguments: N, a positive integer, and L, a list of positive integers. You may assume that the list L is flat, i.e., it does not contain sublists. The predicate split3/2 returns true (or yes) if the list L can be partitioned into three sublists (with elements in the same order), such that the sum of the integers in each subset is less than or equal to N. Otherwise, it returns false (or no). Examples:
?-split3(5,[]).
false
?-split3(6,[5, 5, 12]).
false
?-split3(14,[6, 5, 10, 1, 1, 1, 14]).
true
?-split3(5,[3, 1, 4, 1, 2]).
true
?-split3(6,[4, 3, 5, 2, 1]).
false
?-split3(7,[4, 5, 7, 2, 3]).
false
?-split3(8,[3,5,4,2,7,1]).
true
?-split3(3,[1, 2, 2, 2, 1, 1]).
false
Explanation / Answer
split3(L,Xs) :- list_length(Xs,0,L) .
split3( L ,[] , L ) .
split3( L ,[_|Xs] , T ) :-
T1 is T+1 ,
split3(L ,Xs,T1)
%Output:
%split3(L,[1,2,3,4])
%L = 4
%split3(6,[1,2,3,4])
%false
%split3(4,[1,2,3,4])
%true
%split3(4,[1,2,3,4])
%true
%split3(4,[1,2,1,44,3,4])
%false
%split3(6,[1,2,1,44,3,4])
%true
.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.