0

Artificial intelligence with prolog program

Write a prolog program that solves the following problems 1. Construct a rule palindrome which takes a word as a list or characters and checks to see whether it as a palindrome or not. (A palindrome is a word or phrase that is the same forwards as it is backwards e.g. radar). It should works as follows: ? - palindrome ( [ n, a, v, a, n ] ). Yes ? - palindrome ( [ d, u, b, l, i, n ] ). No 2. Write a predicate replace/4 to replace all occurrences of a given element (second argument) by another given element (third argument) in a given list (first argument). It should work as follows: ?- replace ([1, 2, 3, 4, 3, 5, 6, 3], 3, x, List). List = [1, 2, x, 4, x, 5, 6, x] Yes 3. Implement a Prolog predicate remove_duplicates/2 that removes all duplicate elements from a list given in the first argument and returns the result in the second argument position. It should work as follows: ?- remove_duplicates([a, b, a, c, d, d], List). List = [b, a, c, d] Yes Answer**in commentI want to make sure that the answer is correct

20th Nov 2020, 11:38 AM
Bâÿăň Ã Øťöŏm
Bâÿăň Ã Øťöŏm - avatar
2 odpowiedzi
0
Answer**Q1) Function chekpalindrome(str){ Let str1=”anna Let str2=”banana” Let str 3=”kayak” Checkplindrome(str1) //true Checkplindrome(str2) //false Checkplindrome(str3) //true //0.52ms } Function chekpalindrome(str){ Let reversed= [..str].reverse().jonin(“”) return str====reversed.} Return str===reversed} Q2) funcition checkpladrome(str){ Let le=str.length; If(le===o||le==1){ return true; } If(str[0]===str[le-1]){ Returncheckpalindrome(str.slise(1)} Return false; };//0.30 Q3) Function chekpalindrome(str){ Let1=str.length;8uy For(leti=1;i<1/2;i++) {if(str[1]==str[l-1-i]){ Return false}} return true; }//0.20
20th Nov 2020, 11:39 AM
Bâÿăň Ã Øťöŏm
Bâÿăň Ã Øťöŏm - avatar
0
I want to make sure that the answer is correct or wrong
20th Nov 2020, 11:41 AM
Bâÿăň Ã Øťöŏm
Bâÿăň Ã Øťöŏm - avatar