-
Notifications
You must be signed in to change notification settings - Fork 1
/
1_99.pl
271 lines (187 loc) · 4.93 KB
/
1_99.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
%Find the last element of a list
last_element([],[]).
last_element([L],E) :-
is_list(L),
last_element(L,E).
last_element([E],E) :-
\+ is_list(E).
last_element([_|T],Element) :-
last_element(T,Element).
%last one but element in a list.
last_butone([L,T],L) :- \+ is_list(T).
last_butone([_|T],Element) :-
last_butone(T,Element).
last_butone([_,T],Element) :-
is_list(T),
last_butone(T,Element).
%Find the kth element of a list.
k_element(L,K,Element) :-
k_element(L , 1 , K , Element ).
k_element([H|_] ,Count, K , H) :-
\+ is_list(H),
Count =:= K.
k_element([H|_] ,Count, K , Element) :-
is_list(H),
length(H,Len),
K =< Len,
k_element(H, Count , K , Element).
k_element([H|T] ,Count, K , Element) :-
is_list(H),
length(H,Len),
K > Len,
NewK is K-Len,
k_element(T, Count , NewK , Element).
k_element([_|T] , Count, K , Element) :-
Count \= K,
NewK is K-1,
k_element(T , Count , NewK , Element).
%Number of elements in a list.
numberE([],0).
numberE([H|T],N) :-
\+ is_list(H),
numberE(T,NewN),
N is NewN + 1.
numberE([H|T],N) :-
is_list(H),
numberE(H,N1),
numberE(T,N2),
N is N1 + N2.
tnumberE(List , N) :- tnumberE_aux(List, 0 , N).
tnumberE_aux([] , N , N).
tnumberE_aux([H|T] , Temp , N) :-
is_list(H),
tnumberE_aux(H , Temp , NewTemp ),
tnumberE_aux(T, NewTemp , N).
tnumberE_aux([H|T] , Temp , N) :-
\+ is_list(H),
NewTemp is Temp+1,
tnumberE_aux(T, NewTemp , N).
reverseList([],[]).
reverseList([H|T] , Result) :-
\+ is_list(H),
reverseList(T,Temp),
append(Temp,[H],Result).
reverseList([H|T] , Result) :-
is_list(H),
reverseList(H,Temp1),
reverseList(T,Temp2),
append(Temp2,Temp1,Result).
treverseList(List , Result) :- treverseList(List, [] , Result).
treverseList([] , Result , Result).
treverseList([H|T] , Temp ,Result) :-
\+ is_list(H),
append([H],Temp, NewTemp),
treverseList(T, NewTemp , Result).
treverseList([H|T] , Temp ,Result) :-
is_list(H),
treverseList(H,Temp,Temp1),
treverseList(T, Temp1 , Result).
%Find out whether list is palindrome.
check_palindrome([]).
check_palindrome(List) :-
reverseList(List,Result),
List == Result.
% Run -length encoding of a list.
rl_encoder([],[]).
rl_encoder([H|T], Result) :-
rl_encoder_aux(T,H,1,[],Result). -
rl_encoder_aux([H|T] ,H, Acc , M , Result) :-
NewAcc is Acc + 1,
rl_encoder_aux(T,H,NewAcc,M,Result).
rl_encoder_aux([H|T] , E , Acc , M, Result) :-
H \== E,
append(M,[Acc,E],NewM),
rl_encoder_aux(T,H,1,NewM,Result).
rl_encoder_aux([],E,Acc,M,Result) :-
append(M,[Acc,E],Result).
%Tail recursive code that scans a list of unique positive numbers and finds max.
my_max(List, Max) :- my_max_aux(List, 0 , Max).
my_max_aux([],Max,Max).
my_max_aux([H|T], Accumulator , Max) :-
H >= Accumulator,
!,
my_max_aux(T,H,Max).
my_max_aux([H|T], Accumulator , Max) :-
H < Accumulator,
!,
my_max_aux(T,Accumulator,Max).
% Putting an element in a sorted list.
insertE(Element,[],[Element]).
insertE(Element,[H|T],[H|Tresult]) :-
Element > H,
insertE(Element,T,Tresult).
insertE(Element,[H|T],[Element,H|T]) :-
Element =< H.
%Sorting a list in descending order
de_insert([],[]).
de_insert(List,Result) :- de_insert_aux(List,[],Result).
de_insert_aux([H|T],Acc,Result) :-
insertD(H , Acc , Temp),
de_insert_aux(T , Temp , Result).
de_insert_aux([],Result,Result).
insertD(Element , [H|T] , [Element,H|T]) :-
Element > H.
insertD(Element , [H|T] , [H|TResult]) :-
Element < H,
insertD(Element , T , TResult).
insertD(Element,[],[Element]).
%sort lists based on the length of the sublists
lsort([],[]).
lsort(List, Result) :-
lsort_aux(List,[],Result).
lsort_aux([H|T],Accumulator,Result):-
insertH(H,Accumulator,Temp),
lsort_aux(T,Temp,Result).
lsort_aux([],Result,Result).
insertH(H,[],[H]).
insertH(H,[Head|Tail],[H,Head|Tail]):-
length(H,N1),
length(Head,N2),
N1 >= N2.
insertH(H,[Head|Tail] , [Head|TailResult]):-
length(H,N1),
length(Head,N2),
N1<N2,
insertH(H,Tail,TailResult).
%creating a list containing all elements within a given range.
range(N,N,[N]).
range(N1,N2,[N1|L]) :-
N1 < N2 ,
NewN1 is N1+1,
range(NewN1,N2,L).
%insert element at kth location of a list.
insert_at(E,List,1,[E|List]).
insert_at(E , [H|T] , N , [H|Result]) :-
NewN is N-1,
insert_at(E, T , NewN , Result).
%various forms of permutations.
%form1
perm_1(List1,List2) :-
length(List1,N),
length(List2,N),
subset1(List1,List2).
subset1([],_).
subset1([H|T],List) :-
member(H,List),
subset1(T,List).
%form2
perm2([],[]).
perm2(List,[H|T]) :-
append(First,[H|Rest] ,List),
append(First,Rest,NewList),
perm2(NewList,T).
%chosing N elements from list
choose1(_,0,[]).
choose1(List,N,Result) :-
length(Result,N),
member(X,List),
append(_,[X|Tail],List),
[X|TempRes] = Result,
choose1(Tail,_,TempRes).
%split a list into two parts. Length of the first part is given.
split(L,0,[],L).
split([H|T],N,[H|Result1],Result2) :-
N > 0,
!,
NewN is N-1,
split(T,NewN,Result1,Result2).