During this activity, students should be able to:
This activity helps the student develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.
Individually, solve the following set of programming exercises using the Clojure sequence API.
You may not use explicit recursion nor the loop/recur
special forms. Additionally, you may only use functions defined in the clojure.core
namespace.
Place all your functions in a name space called sequences
.
positives
takes a list of numbers lst as its argument, and returns a new list that only contains the positive numbers of lst. Unit tests:
(deftest test-positives (is (= () (positives '()))) (is (= () (positives '(-4 -1 -10 -13 -5)))) (is (= '(3 6) (positives '(-4 3 -1 -10 -13 6 -5)))) (is (= '(4 3 1 10 13 6 5) (positives '(4 3 1 10 13 6 5)))))
dot-product
takes two arguments: the lists a and b. It returns the result of performing the dot product of a times b. The dot product is an algebraic operation that takes two equal-length sequences of numbers and returns a single number obtained by multiplying corresponding entries and then summing those products. Unit tests:
(deftest test-dot-product (is (= 0 (dot-product () ()))) (is (= 32 (dot-product '(1 2 3) '(4 5 6)))) (is (= 21.45 (dot-product '(1.3 3.4 5.7 9.5 10.4) '(-4.5 3.0 1.5 0.9 0.0)))))
insert
takes two arguments: a number n and a list of numbers lst in ascending order. It returns a new list with the same elements as lst but inserting n in its corresponding place. Unit tests:
(deftest test-insert (is (= '(14) (insert 14 ()))) (is (= '(4 5 6 7 8) (insert 4 '(5 6 7 8)))) (is (= '(1 3 5 5 5 6 7 9 16) (insert 5 '(1 3 5 5 6 7 9 16)))) (is (= '(1 5 6 10) (insert 10 '(1 5 6)))))
replic
takes two arguments: a list lst and an integer number n, where n ≥ 0. It returns a new list that replicates n times each element contained in lst. Unit tests:
(deftest test-replic (is (= () (replic 7 ()))) (is (= () (replic 0 '(a b c)))) (is (= '(a a a) (replic 3 '(a)))) (is (= '(1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4) (replic 4 '(1 2 3 4)))))
expand
takes a list lst as its argument. It returns a list where the first element of lst appears one time, the second elements appears two times, the third element appears three times, and so on. Unit test:
(deftest test-expand (is (= () (expand ()))) (is (= '(a) (expand '(a)))) (is (= '(1 2 2 3 3 3 4 4 4 4) (expand '(1 2 3 4)))) (is (= '(a b b c c c d d d d e e e e e) (expand '(a b c d e)))))
largest
takes as argument a nonempty list of numbers lst. It returns the largest value contained in lst. Do not use the predefined max
or min
functions. Unit tests:
(deftest test-largest (is (= 31 (largest '(31)))) (is (= 5 (largest '(1 2 3 4 5)))) (is (= -1 (largest '(-1 -2 -3 -4 -5)))) (is (= 52 (largest '(32 -1 45 12 -42 52 17 0 21 2)))))
drop-every
takes two arguments: an integer number n, where n ≥ 1, and a list lst. It returns a new list that drops every n-th element from lst. Unit tests:
(deftest test-drop-every (is (= () (drop-every 5 ()))) (is (= '(1 2 3) (drop-every 4 '(1 2 3 4)))) (is (= '(1 3 5 7) (drop-every 2 '(1 2 3 4 5 6 7 8)))) (is (= '(1 3 5 7 9) (drop-every 2 '(1 2 3 4 5 6 7 8 9)))) (is (= '(a b d e g h j) (drop-every 3 '(a b c d e f g h i j)))) (is (= '(a b c d e f g h i j) (drop-every 20 '(a b c d e f g h i j)))) (is (= () (drop-every 1 '(a b c d e f g h i j)))))
rotate-left
takes two arguments: an integer number n and a list lst. It returns the list that results from rotating lst a total of n elements to the left. If n is negative, it rotates to the right. Unit tests:
(deftest test-rotate-left (is (= () (rotate-left 5 ()))) (is (= '(a b c d e f g) (rotate-left 0 '(a b c d e f g)))) (is (= '(b c d e f g a) (rotate-left 1 '(a b c d e f g)))) (is (= '(g a b c d e f) (rotate-left -1 '(a b c d e f g)))) (is (= '(d e f g a b c) (rotate-left 3 '(a b c d e f g)))) (is (= '(e f g a b c d) (rotate-left -3 '(a b c d e f g)))) (is (= '(a b c d e f g) (rotate-left 7 '(a b c d e f g)))) (is (= '(a b c d e f g) (rotate-left -7 '(a b c d e f g)))) (is (= '(b c d e f g a) (rotate-left 8 '(a b c d e f g)))) (is (= '(g a b c d e f) (rotate-left -8 '(a b c d e f g)))) (is (= '(d e f g a b c) (rotate-left 45 '(a b c d e f g)))) (is (= '(e f g a b c d) (rotate-left -45 '(a b c d e f g)))))
join
takes two arguments: any object sep and a list lst. It returns a string created by converting each element of lst to a string, separated by sep. Unit tests:
(deftest test-join (is (= "" (join \$ ()))) (is (= "a" (join "..." '(a)))) (is (= "alphabetagamma" (join nil '("alpha" "beta" "gamma")))) (is (= "alpha$beta$gamma" (join \$ '("alpha" "beta" "gamma")))) (is (= "1 and 2 and 3 and 4" (join " and " '(1 2 3 4)))) (is (= "a,b,c,d,e,f" (join \, '(a b c d e f)))))For this problem, you will need to use the
str
function. This function is equivalent to Java's toString
method. Examples:
(str) ⇒ "" (str 42) ⇒ "42" (str nil) ⇒ "" (str '(1 2 3)) ⇒ "(1 2 3)" (str 'a-symbol 100 "hello" true) ⇒ "a-symbol100hellotrue"
The function pi
takes an integer argument n and returns an approximation of π using the following series composed of n terms:
π = 4(1 − 1/3 + 1/5 − 1/7 + 1/9 − 1/11 + ...)
Here, the terms are: 1, −1/3, 1/5, −1/7, 1/9, −1/11, and so on.
Unit tests:
(deftest test-pi (is (= 4.0000000000000000 (pi 1))) (is (= 2.6666666666666670 (pi 2))) (is (= 3.0418396189294032 (pi 10))) (is (= 3.1405926538397940 (pi 1000))) (is (= 3.1414926535900345 (pi 10000))) (is (= 3.1415826535897198 (pi 100000))) (is (= 3.1415916535897743 (pi 1000000))))
Source of several of these problems: Ninety-Nine Lisp Problems
Using the Online
Assignment Delivery System (SETA), deliver the file called
sequences.clj
containing your solutions and the unit
tests. No assignments will be accepted through
e-mail or any other means.
IMPORTANT: The program source file must include at the top the author's personal information (name and student id) within comments. For example:
;;; ITESM CEM, September 21, 2012. ;;; Clojure Source File ;;; Activity: Using the Sequence API ;;; Author: Steve Rogers, 1166611 . . (The rest of the program goes here) .
Due date: Friday, September 21.
This activity will be evaluated using the following criteria:
-10 | The program doesn't contain within comments the author's personal information. |
---|---|
10 | The program contains syntax errors. |
DA | The program was plagiarized. |
10-100 | Depending on the amount of exercises that were solved correctly. |