Programming Exercises (in Picat)

  1. is_prime(n): a predicate that returns true if n is a prime number, and false otherwise.

  2. sphere_volume(r) : a function that computes the volume of a sphere, given its radius r.

  3. quadratic_equation(a,b,c): a function that computes the real roots of a given quadratic equation a*X^2+b*X+c=0.

  4. number_of_zeros(lst): a function that returns the number of zeros in a given simple list of numbers lst.

  5. draw_pascal(n): a function that takes an integer n as a parameter and prints the first n rows of the Pascal's triangle.

  6. days(date1,date2): a function that takes two dates, date1 and date2, in some format, and returns the number of days from date1 to date2, inclusive.

  7. is_palindrome(lst): a predicate that returns true if lst is a palindrome, and false otherwise.

  8. remove_consecutive_dups(lst): a function that returns a copy of lst with consecutive duplicates of elements eliminated. For example, for lst = [a,a,a,a,b,c,c,a,a,d,e,e,e,e], the returned list is [a,b,c,a,d,e].

  9. replicate(lst,n): a function that replicates each of the elements of lst a given number of times. For example, for lst = [a,b,c] and n = 3, the returned list is [a,a,a,b,b,b,c,c,c].

  10. split_list(lst,n): a function that splits lst into two parts with the first part having n elements, and returns a list that contains these two parts.

  11. (optional) bc(n,k): a function that returns the binomial coefficient "n choose k". Can you figure out a method that is less likely to cause an overflow than using the formula (n*(n-1)*...*(n-k+1))/(k*(k-1)*...*2)?

  12. (optional) subsets(s,n): a function that returns the set of n-element subsets of s.

  13. (optional) max_subarray(arr): a function that returns a contiguous subarray within arr which has the largest sum.