Created
May 16, 2015 10:20
-
-
Save MarkLavrynenko/93c12dbd0ef5636e154b to your computer and use it in GitHub Desktop.
Artificial Intelligence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# 1) a, b- min(x) a^x <= b | |
# 2) list - a, b; (a - b ) union (b - a) | |
# 3) a - find last atom | |
# 4) depth - the deepest sublist with sublists | |
# 5) sublists count | |
# 6) лінеаризація | |
# 7) min-max tree optonal | |
*/ | |
start():- | |
/* task1(2, 1023, Ans), */ | |
/* task2([1, 2, 3], [3, 2, 8], Ans), */ | |
/* task3([23, [3, [7, 5, [3, atom]]]], Ans), */ | |
/* * * */ | |
/* task4([ [1, 2], 3, [2, [5, 8], 5], 4], Ans), */ | |
task5([ [1, 2], 3, [2, 5, 8, 5], 4], Ans), | |
/* task6([23, [3, [7], 5, [3]], 6], Ans), */ | |
writeln(Ans). | |
/* --------------------------------------------------------- */ | |
task1_inner(Initial, A, Limit, I, Ans):- | |
A * Initial > Limit, Ans is I. | |
task1_inner(Initial, A, Limit, I, Ans):- | |
Next is Initial * A, | |
task1_inner(Initial, Next, Limit, I + 1, Ans). | |
task1(A, B, Ans):- | |
task1_inner(A, 1, B, 0, Ans). | |
/* --------------------------------------------------------- */ | |
sym_diff(A, [], Result):- | |
Result = A. | |
sym_diff(A, [F | Rest], Result):- | |
delete(A, F, Filtered), | |
sym_diff(Filtered, Rest, Result). | |
task2(A, B, C):- | |
sym_diff(A, B, P1), | |
sym_diff(B, A, P2), | |
append(P1, P2, C). | |
/* --------------------------------------------------------- */ | |
findLastAtom([H], H):-atomic(H). | |
findLastAtom([H], R):-findLastAtom(H, R). | |
findLastAtom([_|T], Ans):-findLastAtom(T, Ans). | |
task3(List, Ans):-findLastAtom(List, Ans). | |
/* --------------------------------------------------------- */ | |
flatten([],[]). | |
flatten([H|T1], [H|T2]):- atomic(H), flatten(T1, T2). | |
flatten([H|T], R):- flatten(T,T1), flatten(H, T2), append(T2, T1, R). | |
task6(ListA, Ans):- flatten(ListA, Ans). | |
/* --------------------------------------------------------- */ | |
depth([], 1). | |
depth([H| T], Ans):- atomic(H), depth(T, Ans). | |
depth([H| T], Ans):- depth(H, X), depth(T, Y), Ans is max(X + 1, Y). | |
task4(List, Ans):- depth(List, Ans). | |
/* --------------------------------------------------------- */ | |
cnt([], 0). | |
cnt([H| T], Ans):- atomic(H), cnt(T, Ans). | |
cnt([H| T], Ans):- cnt(H, X), cnt(T, Y), Ans is X + Y + 1. | |
task5(List, Ans):- cnt(List, Ans). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment