Artificial Intelligence: Tutorial 1 Questions Prolog

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

TRNG I HC BCH KHOA TP.

HCM Khoa Khoa hc & K thut My tnh

ARTIFICIAL INTELLIGENCE
Tutorial 1 Questions PROLOG
Use Prolog language to solve the following questions: Question 1. 1.1. Get the second element of a list L. 1.2. Reverse a list L. 1.3. Insert an element into the last position of a list L. 1.4. Append two lists L1 and L2 into L3. Question 2. 2.1. Write count1(L, R) predicate where L is a list of integers and the output R is the number of odd numbers in L. Ex: count1([1, 2, -1, 3, 2], R) => R = 3 2.2. Write count2(LL, R) predicate where LL is 2-dimentional list of integers and the output R is the number of odd numbers in LL. Ex: count2([[1, 2, -1, 3, 2], [3, 15], [2]], R) => R = 5 2.3. Write count3(LL, R) predicate where LL is 2-dimentional list of integers and the output R is the number of elements in LL that the sum of the numbers in that element ends with 2. Ex: count3([[1, 2, 3, 6], [2, 3, 4], [3, 4], [4, 5, 6, 7]], R) => R = 2 Question 3. Write separate(L, N, L1, L2) predicate where L is a list of integers, the output L1 is a list of the first N elements in L and the output L2 is a list of the elements left in L. Ex: separate([2, 4, 8, 10, 14, 1, 7], 3, L1, L2) => L1 = [2, 4, 8] L2 = [10, 14, 1, 7] Question 4. Write calculate predicate to calculate the value of a sequence of numbers defined as follows: m, n >= 0 a(0, n) = n + 1 a(m, 0) = a(m 1, 1) if m 0 a(m, n) = a(m 1, a(m, n - 1)) if m 0 and n 0 1

TRNG I HC BCH KHOA TP.HCM Khoa Khoa hc & K thut My tnh Ex: calculate(1, 1, X) => X = 3 Question 5. Write check predicate to check that a list of integers satisfies: the element at the position i is greater than the sum of two elements at two positions i 1 and i 2 (i >= 3, i is odd) the element at the position i is smaller than the sum of two elements at two positions i 1 and i 2 (i >= 4, i is even) Ex: check([2, 1, 4, 2, 9, 8]) => yes Question 6. Write trans predicate to transpose a matrix A. Ex: trans([[1, 2, 3], [4, 5, 6], [7, 8, 9]], X) => X = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Question 7. Write pack predicate to group the same consecutive elements of a list into sublists. Ex: pack([a, a, a, a, b, c, c, a, a, d, e, e, e, e], X) => X = [[a, a, a, a], [b], [c, c], [a, a], [d], [e, e, e, e]] -- End --

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy