Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compress a list that contains segments of continuous numbers to a list which contains list of range

Status
Not open for further replies.

S.K

Programmer
Apr 29, 2024
1
AT
I'm new in prolog and I'm trying to solve this exercise but I can not get my desired output.Compressing a list of integers to a compressed list. List contains segments of continuous integers.A segment with length greater than 2 will be encoded to list [start, end], end is inclusive. If the length is not greater than 2 will be kept no change.

Sample:

compress([3,4,6,7,8,9,10,15,16,17,20,22,23,24,25], CompressedList).

output(CompressedList) = [3, 4, [6,10],[15, 17],20, [22,25]].

My incorrect output:

CompressedList = [[3, [4]], [6,[10]],[15, [17]],20, [22,[25]]].

My code is:

compress([], []).
compress([X], [X]).

compress([X, Y | T], [X | CompressedTail]) :-
X + 1 =\= Y,
compress([Y | T], CompressedTail).


compress([X, Y | T], [[X, YLast] | CompressedTail]) :-
compress_consecutive([Y | T], YLast, Next),
compress(Next, CompressedTail).


compress_consecutive([], Last, []) :- Last = [].
compress_consecutive([X], [X], []) :- !.
compress_consecutive([X, Y | T], Last, Next) :-
X + 1 =:= Y,
compress_consecutive([Y | T], Last, Next).


compress_consecutive([X, Y | T], [X], [Y | T]) :- X + 1 =\= Y. ```
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top