# Problem Set #2: Repetitions

### Objective

During this activity, students should be able to:

• Write Clojure functions that involve using repetition constructs such as: recursion, loop/recur, and the sequence API.

This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

## Activity Description

Solve with your team the Clojure exercises described in this problem set. Make sure each function passes all the unit tests.

Create a namespace called repetitions. At the begining of the file add the following code in order to declare the namespace and import the required external functions:

(ns repetitions
(:require [clojure.test :refer [deftest is run-tests]])
(:require [clojure.math.numeric-tower :refer [sqrt]])
(:require [clojure.algo.generic.math-functions
:refer [sqr approx=]]))


and at the end add:

(run-tests)

All the code you write should go between these two instructions.

1. The function enlist surrounds in a list every upper-level element of the sequence it takes as input.

Unit tests:

(deftest test-enlist
(is (= () (enlist ())))
(is (= '((a) (b) (c)) (enlist '(a b c))))
(is (= '((1) (2) (3) (4)) (enlist [1 2 3 4])))
(is (= '(((1 2 3)) (4) ((5)) (7) (8))
(enlist '((1 2 3) 4 (5) 7 8)))))

2. The function positives takes a sequence of numbers $$s$$ as its input, and returns a new list that only contains the positive numbers of $$s$$.

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]))))

3. The function add-squares returns the sum of the squares of all numbers contained in its input list, or 0 if its empty.

Unit tests:

(deftest test-add-squares
(is (= 0 (add-squares [])))
(is (= 25 (add-squares [5])))
(is (= 30 (add-squares [2 4 1 3])))
(is (= 385 (add-squares [1 2 3 4 5 6 7 8 9 10]))))

4. The function duplicate takes a sequence $$s$$ as its input and returns a new list with each element of $$s$$ duplicated.

Unit tests:

(deftest test-duplicate
(is (= [1 1 2 2 3 3 4 4 5 5]
(duplicate [1 2 3 4 5])))
(is (= ()
(duplicate ())))
(is (= '(a a)
(duplicate '(a))))
(is (= '(a a b b c c d d e e f f g g h h)
(duplicate '(a b c d e f g h)))))

5. The function fib takes a positive integer $$n$$ as its input and returns the corresponding element of the Fibonacci sequence, which can be defined mathematically as:

$$\text{fib}(n)=\begin{cases} n & \text{ if } n \le 1 \\ \text{fib}(n-1) + \text{fib}(n-2) & \text{ if } n>1 \end{cases}$$

Unit tests:

(deftest test-fib
(is (= 0
(fib 0)))
(is (= 1
(fib 1)))
(is (= 1
(fib 2)))
(is (= 5
(fib 5)))
(is (= [0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
987 1597 2584 4181 6765]
(map fib (range 21))))
(is (= 267914296
(fib 42))))

6. The function pow takes two inputs: a number $$a$$ and a positive integer $$b$$. It returns the result of computing $$a^b$$ ($$a$$ raised to the power $$b$$). Do not use any of the predefined power functions (Math/pow, clojure.math.numeric-tower/expt, clojure.algo.generic.math-functions/pow, etc.) to solve this problem.

Unit tests:

(deftest test-pow
(is (= 1 (pow 0 0)))
(is (= 0 (pow 0 1)))
(is (= 1 (pow 5 0)))
(is (= 5 (pow 5 1)))
(is (= 125 (pow 5 3)))
(is (= 25 (pow -5 2)))
(is (= -125 (pow -5 3)))
(is (= 1024 (pow 2 10)))
(is (= 525.21875 (pow 3.5 5)))
(is (= 129746337890625 (pow 15 12)))
(is (= 3909821048582988049 (pow 7 22)))
(is (= 1267650600228229401496703205376N (pow 2 100))))

7. The function only-symbols? takes a sequence $$s$$ as its input. It returns true if all the elements (possibly zero) contained in $$s$$ are symbols, or false otherwise. Use the symbol? predicate to determine if some object is a symbol.

Unit tests:

(deftest test-only-symbols?
(is (= true (only-symbols? [])))
(is (= true (only-symbols? '(a))))
(is (= true (only-symbols? '(a b c d e))))
(is (= false (only-symbols? '(a b c d 42 e))))
(is (= false (only-symbols? '(42 a b c))))
(is (= false (only-symbols? [4 8 15 16 23 42]))))

8. The function invert-pairs takes as an input a sequence of vectors containing two elements each. It returns a new list with every vector pair inverted.

Unit tests:

(deftest test-invert-pairs
(is (= () (invert-pairs ())))
(is (= '([y x]) (invert-pairs '([x y]))))
(is (= '([1 a][2 a][1 b][2 b])
(invert-pairs '([a 1][a 2][b 1][b 2]))))
(is (= '([1 January][2 February][3 March])
(invert-pairs '([January 1][February 2][March 3])))))

9. The function replic takes two inputs: an integer number $$n$$ and a sequence $$s$$, where $$n \ge 0$$. It returns a new list that replicates $$n$$ times each element contained in $$s$$.

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]))))

10. The function dot-product takes two inputs: the sequences $$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. In mathematical terms:

$$a \cdot b = \sum_{i=1}^{n}a_ib_i$$

Unit tests:

(deftest test-dot-product
(is (= 0 (dot-product [] [])))
(is (= 42 (dot-product [6] [7])))
(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]))))

11. The function average takes a sequence of numbers $$s$$ as its input. It returns the arithmetic mean of the numbers contained in $$s$$, or nil if $$s$$ is empty. The arithmetic mean ($$\bar{x}$$) is defined as:

$$\bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_i$$

Unit tests:

(deftest test-average
(is (nil? (average [])))
(is (= 4
(average [4])))
(is (= 3
(average [5 6 1 6 0 1 2])))
(is (= 2.5
(average [1.7 4.5 0.0 2.0 3.4 5.0 2.5 2.2 1.2]))))

12. The function standard-deviation takes a sequence of numbers $$s$$ as its input. It returns the population standard deviation of the numbers contained in $$s$$, or nil if $$s$$ is empty. The population standard deviation ($$\sigma$$) is defined as:

$$\sigma = \sqrt{\frac{1}{n} \sum_{i=1}^{n}(x_i - \bar{x})^2}$$

Unit tests:

(deftest test-standard-deviation
(is (nil? (standard-deviation [])))
(is (approx= 1.87
(standard-deviation [6 2 3 1])
0.01))
(is (approx= 12.3153
(standard-deviation [4 8 15 16 23 42])
0.0001))
(is (approx= 7.07106
(standard-deviation [110 105 90 100 95])
0.00001))
(is (approx= 2.983
(standard-deviation [9 2 5 4 12 7 8 11
9 3 7 4 12 5 4 10
9 6 9 4])
0.001)))


## Deliverables

The program source file must include at the top the authors’ personal information (student ID and name) within comments. For example:

;----------------------------------------------------------
; Problem Set #2: Repetitions
; Date: March 8, 2023.
; Authors:
;          A01770771 Sylvie Laufeydottir
;          A01777771 Loki Laufeyson
;----------------------------------------------------------


Also, each function should include a documentation string (docstring) with a brief description of its behavior. For example:

(defn max2
"Returns the largest of the two numbers x and y."
[x y]
(if (> x y) x y))


### Instrucciones para subir archivo

Para entregar el archivo repetitions.clj, ingresa los siguientes datos:

Only one team member needs to upload the file.

Due date is Wednesday, March 8.

## Evaluation

This activity will be evaluated using the following criteria:

 -10 The program doesn't contain within comments the authors’ personal information. A docstring is missing in one or more functions. The program contains syntax errors. The program was plagiarized in whole or in part. Depending on the amount of exercises that were solved correctly.