# פרולוג תרגיל 4

```

%1
min_max(X,Y,Z,MIN,MID,MAX):- X>=Y,Y>=Z,MAX is X,MID is Y,MIN is Z.
min_max(X,Y,Z,MIN,MID,MAX):- X>=Z,Z>=Y,MAX is X,MID is Z,MIN is Y.
min_max(X,Y,Z,MIN,MID,MAX):- Y>=Z,Z>=X,MAX is Y,MID is Z,MIN is X.
min_max(X,Y,Z,MIN,MID,MAX):- Y>=X,X>=Z,MAX is Y,MID is X,MIN is Z.
min_max(X,Y,Z,MIN,MID,MAX):- Z>=X,X>=Y,MAX is Z,MID is X,MIN is Y.
min_max(X,Y,Z,MIN,MID,MAX):- Z>=Y,Y>=X,MAX is Z,MID is Y,MIN is X.

/*
3 ?- min_max(3,4,5,MIN,MID,MAX).
MIN = 3,
MID = 4,
MAX = 5 .
*/

%1 !
min_max1(X,Y,Z,MIN,MID,MAX):- X>=Y,Y>=Z,MAX is X,MID is Y,MIN is Z,!.
min_max1(X,Y,Z,MIN,MID,MAX):- X>=Z,Z>=Y,MAX is X,MID is Z,MIN is Y,!.
min_max1(X,Y,Z,MIN,MID,MAX):- Y>=Z,Z>=X,MAX is Y,MID is Z,MIN is X,!.
min_max1(X,Y,Z,MIN,MID,MAX):- Y>=X,X>=Z,MAX is Y,MID is X,MIN is Z,!.
min_max1(X,Y,Z,MIN,MID,MAX):- Z>=X,X>=Y,MAX is Z,MID is X,MIN is Y,!.
min_max1(X,Y,Z,MIN,MID,MAX):- MAX is Z,MID is Y,MIN is X.

/*
12 ?- min_max1(3,4,5,MIN,MID,MAX).
MIN = 3,
MID = 4,
MAX = 5.
*/

% 1 ירוק
min_max2(X,Y,Z,MIN,MID,MAX):- X>=Y,Y>=Z,MAX is X,MID is Y,MIN is Z,!.
min_max2(X,Y,Z,MIN,MID,MAX):- X>=Z,Z>=Y,MAX is X,MID is Z,MIN is Y,!.
min_max2(X,Y,Z,MIN,MID,MAX):- Y>=Z,Z>=X,MAX is Y,MID is Z,MIN is X,!.
min_max2(X,Y,Z,MIN,MID,MAX):- Y>=X,X>=Z,MAX is Y,MID is X,MIN is Z,!.
min_max2(X,Y,Z,MIN,MID,MAX):- Z>=X,X>=Y,MAX is Z,MID is X,MIN is Y,!.
min_max2(X,Y,Z,MIN,MID,MAX):- Z>=Y,Y>=X,MAX is Z,MID is Y,MIN is X.

/*
1 ?- min_max2(5,3,4,MIN,MID,MAX).
MIN = 3,
MID = 4,
MAX = 5.
*/

% 2
/*
1 קאט מיותר
2 קאט מזיק
3 קאט מזיק
4 קאט מיותר ואם היו עוד מכוניות שמחירם קטן מ1000 זה היה מזיק
5 קאט מזיק
6 קאט מיותר
7 קאט ירוק
8 קאט מיותר
*/

% 3
intersection([],L2,[]).
intersection(L1,[],[]).
intersection([H|T],L2,Z):-
member(H,L2),intersection(T,L2,Z1),append([H],Z1,Z).
intersection([H|T],L2,Z):-
not(member(H,L2)),intersection(T,L2,Z).

/*
5 ?- intersection([1,3,4,*],[5,2,2,&],Z).
Z = []

intersection([1,3,4,*],[5,3,2,&],Z).
Z = [3]
*/

% 4
rgbsort(L,[R,G,B]):- rgb_help(L,R,G,B,0,0,0).
rgb_help([red|REST],R,G,B,RC,GC,BC):- !,RC1 is RC+1, rgb_help(REST,R,G,B,RC1,GC,BC).
rgb_help([green|REST],R,G,B,RC,GC,BC):- !,GC1 is GC+1, rgb_help(REST,R,G,B,RC,GC1,BC).
rgb_help([blue|REST],R,G,B,RC,GC,BC):- !,BC1 is BC+1, rgb_help(REST,R,G,B,RC,GC,BC1).
rgb_help([],R,G,B,R,G,B).

/*
1 ?- rgbsort([red,blue,red,green,red,red],S).
S = [4, 1, 1]

2 ?- rgbsort([green,blue,red,green,red,red],S).
S = [3, 2, 1]
*/

% 5
flatten([H|T],L):- atomic(H),!,flatten(T,L1),append([H],L1,L).
flatten([H|T],L):- flatten(H,L1),flatten(T,L2),append(L1,L2,L).
flatten([],[]).

/*
1 ?- flatten([1,[3,*],&,7,[7,0],\$],L).
L = [1, 3, (*), &, 7, 7, 0, (\$)]

*/

```

benny

פיתרון 2

```

% ---שאלה 1---
% without !

max_min(X,X,X,MIN, MID, MAX):- MIN is X, MAX is X, MID is X.

max_min(X,Y,Z,MIN,MID,MAX):- X>Y, Y>Z, MIN is Z, MID is Y, MAX is X.
max_min(X,Y,Z,MIN,MID,MAX):- X>Z, Z>Y, MIN is Y, MID is Z, MAX is X.
max_min(X,Y,Z,MIN,MID,MAX):- Y>X, X>Z, MIN is Z, MID is X, MAX is Y.
max_min(X,Y,Z,MIN,MID,MAX):- Y>Z, Z>Y, MIN is X, MID is Z, MAX is Y.
max_min(X,Y,Z,MIN,MID,MAX):- Z>X, X>Y, MIN is Y, MID is X, MAX is Z.
max_min(X,Y,Z,MIN,MID,MAX):- Z>Y, Y>X, MIN is X, MID is Y, MAX is Z.

max_min(X,X,Y,MIN,MID,MAX):- X>Y, MAX is X, MIN is Y, MID is X.
max_min(X,Y,X,MIN,MID,MAX):- X>Y, MAX is X, MIN is Y, MID is X.
max_min(Y,X,X,MIN,MID,MAX):- X>Y, MAX is X, MIN is Y, MID is X.

max_min(X,X,Y,MIN,MID,MAX):- X<Y, MAX is Y, MIN is X, MID is X.
max_min(X,Y,X,MIN,MID,MAX):- X<Y, MAX is Y, MIN is X, MID is X.
max_min(Y,X,X,MIN,MID,MAX):- X<Y, MAX is Y, MIN is X, MID is X.

%with green !

max_min2(X,Y,Z,MIN,MID,MAX):- X>=Y,Y>=Z,MAX is X,MID is Y,MIN is Z,!.
max_min2(X,Y,Z,MIN,MID,MAX):- X>=Z,Z>=Y,MAX is X,MID is Z,MIN is Y,!.
max_min2(X,Y,Z,MIN,MID,MAX):- Y>=Z,Z>=X,MAX is Y,MID is Z,MIN is X,!.
max_min2(X,Y,Z,MIN,MID,MAX):- Y>=X,X>=Z,MAX is Y,MID is X,MIN is Z,!.
max_min2(X,Y,Z,MIN,MID,MAX):- Z>=X,X>=Y,MAX is Z,MID is X,MIN is Y,!.
max_min2(X,Y,Z,MIN,MID,MAX):- Z>=Y,Y>=X,MAX is Z,MID is Y,MIN is X.

%with red !

max_min2(X,Y,Z,MIN,MID,MAX):- X>=Y,Y>=Z,MAX is X,MID is Y,MIN is Z,!.
max_min2(X,Y,Z,MIN,MID,MAX):- X>=Z,Z>=Y,MAX is X,MID is Z,MIN is Y,!.
max_min2(X,Y,Z,MIN,MID,MAX):- Y>=Z,Z>=X,MAX is Y,MID is Z,MIN is X,!.
max_min2(X,Y,Z,MIN,MID,MAX):- Y>=X,X>=Z,MAX is Y,MID is X,MIN is Z,!.
max_min2(X,Y,Z,MIN,MID,MAX):- Z>=X,X>=Y,MAX is Z,MID is X,MIN is Y,!.
max_min2(X,Y,Z,MIN,MID,MAX):- MAX is Z,MID is Y,MIN is X.
/* ---הרצה---
?- max_min(5, 1, 9,MIN, MID, MAX).
MIN = 1,
MID = 5,
MAX = 9
*/

%---שאלה 2---
/*
1. מיותר
2,3. מזיק
4. מזיק כי יתכן שיש כמה מכוניות שאפשר לקנות בפחות מ-1000, אבל אם נניח שיש רק 1 אז זה היה מיותר
5. מזיק
6. מיותר
7. ירוק עם ההנחה שיש רק מכונית אחד
8. מיותר
*/

% ---שאלה 3---
intersection([], L, []).
intersection(L, [], []).
intersection([H|T], L, RESULT):- member(H, L), intersection(T, L, RESULT1), append([H], RESULT1, RESULT).
intersection([H|T], L, RESULT):- not(member(H, L)), intersection(T, L, RESULT).
/* ---הרצה---
ANSWER = [1, 2, 3, 5, 6]
*/

% ---שאלה 4---
rgbsort(L,[R,G,B]):-rgb_counter(L,R,G,B,0,0,0).
rgb_counter([red|REST],R,G,B,RC,GC,BC):-!, RC1 is RC+1, rgb_counter(REST,R,G,B,RC1,GC,BC).
rgb_counter([green|REST],R,G,B,RC,GC,BC):-!, GC1 is GC+1, rgb_counter(REST,R,G,B,RC,GC1,BC).
rgb_counter([blue|REST],R,G,B,RC,GC,BC):-!, BC1 is BC+1, rgb_counter(REST,R,G,B,RC,GC,BC1).
rgb_counter([],R,G,B,R,G,B).
/* ---הרצה---
?- rgbsort(['green', 'red', 'blue', 'blue'], RES).
RES = [1, 1, 2].

?- rgbsort(['green','green','green','green'], RES).
RES = [0, 4, 0].
*/

% ---שאלה 5---
flatten([H|T],L):- atomic(H),!,flatten(T,L1),append([H],L1,L).
flatten([H|T],L):- flatten(H,L1),flatten(T,L2),append(L1,L2,L).
flatten([],[]).
/* ---הרצה---
?- flatten([[@,#],[[[\$]]],[2,3,4,5],[0],3],RES).
RES = [@, #, \$, 2, 3, 4, 5, 0, 3].
*/

```

y.levental