You are here:   ArielOrtiz.com > Programming Languages > Sequence Exercises

Sequence Exercises

Note: This is a class activity, nothing needs to be delivered.

Solve the following problems by using the Clojure sequence API. Place all the requested functions and unit tests in a name space called seqs.

  1. The function enlist surrounds in a list every upper-level element of the list it takes as input. Unit tests:
    (deftest test-enlist
      (is (= () (enlist '())))
      (is (= '((a) (b) (c)) (enlist '(a b c))))
      (is (= '(((1 2 3)) (4) ((5)) (7) (8)) (enlist '((1 2 3) 4 (5) 7 8)))))
  2. The function list-of-symbols? takes a list lst as its argument. It returns true if all the elements (possibly zero) contained in lst are symbols, or false otherwise. Unit tests.
    (deftest test-list-of-symbols?
      (is (list-of-symbols? ()))
      (is (list-of-symbols? '(a)))
      (is (list-of-symbols? '(a b c d e)))
      (is (not (list-of-symbols? '(a b c d 42 e))))
      (is (not (list-of-symbols? '(42 a b c)))))
    
  3. Write a function called average that takes a list of numbers lst as an argument and computes the average of all those numbers. Throws an ArithmeticException if lst is empty. Unit tests:
    (deftest test-average
      (is (= 5.0 (average '(5.0))))
      (is (= 5.12 (average '(3.1 4.2 7.3 9.8 1.2))))
      (is (thrown? ArithmeticException (average ()))))
  4. Write a function called fact that takes n as an argument and computes the factorial of n. The factorial of n is the product of all positive integers less than or equal to n. Factorial of 0 is 1. Unit tests:
    (deftest test-fact
      (is (= 1 (fact 0)))
      (is (= 1 (fact 1)))
      (is (= 2 (fact 2)))
      (is (= 6 (fact 3)))
      (is (= 120 (fact 5)))
      (is (= 3628800 (fact 10)))
      (is (= 265252859812191058636308480000000 (fact 30))))
  5. The function invert-pairs takes as an argument a list 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 (= '([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])))))
  6. The function pack takes a list lst as its argument. If lst contains consecutive repeated elements they should be placed in separate sublists. Unit tests:
    (deftest test-pack
      (is (= () (pack ())))
      (is (= '((a a a a) (b) (c c) (a a) (d) (e e e e))
             (pack '(a a a a b c c a a d e e e e))))
      (is (= '((1) (2) (3) (4) (5)) (pack '(1 2 3 4 5))))
      (is (= '((9 9 9 9 9 9 9 9 9)) (pack '(9 9 9 9 9 9 9 9 9)))))
  7. The function compress takes a list lst as its argument. If lst contains consecutive repeated elements, they should be replaced with a single copy of the element. The order of the elements should not be changed. Unit tests:
    (deftest test-compress
      (is (= () (compress ())))
      (is (= '(a b c d) (compress '(a b c d))))
      (is (= '(a b c a d e) (compress '(a a a a b c c a a d e e e e))))
      (is (= '(a) (compress '(a a a a a a a a a a)))))
  8. The function encode takes a list lst as its argument. Consecutive duplicates of elements in lst are encoded as vectors [n e], where n is the number of duplicates of the element e. Unit tests:
    (deftest test-encode
      (is (= () (encode ())))
      (is (= '([4 a] [1 b] [2 c] [2 a] [1 d] [4 e])
             (encode '(a a a a b c c a a d e e e e))))
      (is (= '([1 1] [1 2] [1 3] [1 4] [1 5]) (encode '(1 2 3 4 5))))
      (is (= '([9 9]) (encode '(9 9 9 9 9 9 9 9 9)))))
  9. The function encode-modified takes a list lst as its argument. It works the same as the previous problem, but if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are converted to [n e] vectors. Unit tests:
    (deftest test-encode-modified
      (is (= () (encode-modified ())))
      (is (= '([4 a] b [2 c] [2 a] d [4 e])
             (encode-modified '(a a a a b c c a a d e e e e))))
      (is (= '(1 2 3 4 5) (encode-modified '(1 2 3 4 5))))
      (is (= '([9 9]) (encode-modified '(9 9 9 9 9 9 9 9 9)))))
  10. The function decode takes as its argument an encoded list lst that has the same structure as the resulting list from the previous problem. It returns the decoded version of lst. Unit tests:
    (deftest test-decode
      (is (= () (decode ())))
      (is (= '(a a a a b c c a a d e e e e)
             (decode '([4 a] b [2 c] [2 a] d [4 e]))))
      (is (= '(1 2 3 4 5) (decode '(1 2 3 4 5))))
      (is (= '(9 9 9 9 9 9 9 9 9) (decode '([9 9])))))
© 1996-2011 by Ariel Ortiz (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid HTML5 | Valid CSS