During this activity, students should be able to:
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.
Individually or in pairs, solve the following set of sequential programming exercises using the Erlang programming language. Place all your functions in a module called sequential
.
NOTE: For this activity, you are not allowed to use any of the functions defined in Erlang's lists
module.
The function but_last
returns a list with the same elements as its input list but excluding the last element. Assume that the input list contains at least one element. For example:
> sequential:but_last([a, b, c, d]). [a,b,c] > sequential:but_last([1]). [] > sequential:but_last([first, middle, last]). [first,middle]
The function merge
returns the list that results from combining in ascending order all the elements contained in the two lists of numbers taken as arguments. The input lists should be in ascending order. For example:
> sequential:merge([1, 4], [1, 2, 8]). [1,1,2,4,8] > sequential:merge([], []). [] > sequential:merge([35, 62, 81, 90, 91], [3, 83, 85, 90]). [3,35,62,81,83,85,90,90,91]
The function insert
takes two arguments: a number N and a list of numbers L in ascending order. It returns a new list with the same elements as L but inserting N in its corresponding place. For example:
> sequential:insert(5, [1, 3, 6, 7, 9, 16]). [1,3,5,6,7,9,16] > sequential:insert(10, [1, 5, 6]). [1,5,6,10] > sequential:insert(14, []). [14]
The function sort
takes an unordered list of numbers as an argument, and returns a new list with the same elements but in ascending order. You must use the insert
function defined in the previous problem. For example:
> sequential:sort([4, 3, 6, 8, 3, 0, 9, 1, 7]). [0,1,3,3,4,6,7,8,9] > sequential:sort([]). []
The function binary
takes an integer N as input (assume that N ≥ 0). If N is equal to zero, it returns an empty list. If N is greater than zero, it returns a list with a sequence of ones and zeros equivalent to the binary representation of N. For example:
> sequential:binary(0). [] > sequential:binary(30). [1,1,1,1,0] > sequential:binary(45123). [1,0,1,1,0,0,0,0,0,1,0,0,0,0,1,1]
The function bcd
takes an integer N as input (assume that N ≥ 0), and returns a list of strings with the BCD representation of N. A BCD (Binary Coded Decimal) number is an encoding for decimal numbers in which each decimal digit is represented by its own 4-bit binary sequence. Examples:
> sequential:bcd(27). ["0010","0111"] > sequential:bcd(1093). ["0001","0000","1001","0011"] > sequential:bcd(0). ["0000"]
The function prime_factors
takes an integer N as input (assume that N > 0), and returns a list containing the prime factors of N in ascending order. The prime factors are the prime numbers that divide a number exactly. If you multiply all the prime factors you get the original number. Examples:
> sequential:prime_factors(6). [2,3] > sequential:prime_factors(17). [17] > sequential:prime_factors(96). [2,2,2,2,2,3] > sequential:prime_factors(666). [2,3,3,37] > sequential:prime_factors(1). []
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. For example:
> sequential:compress([a, a, a, a, b, c, c, a, a, d, e, e, e, e]). [a,b,c,a,d,e]
The function encode
takes a list Lst as its argument. Consecutive duplicates of elements in Lst are encoded as tuples {N, E} where N is the number of duplicates of the element E. If an element has no duplicates, it is simply copied into the result list. For example:
> sequential:encode([a, a, a, a, b, c, c, a, a, d, e, e, e, e]). [{4,a},b,{2,c},{2,a},d,{4,e}]
The function decode
takes as its argument an encoded list Lst like the output from the previous problem. It returns the decoded version of Lst. For example:
> sequential:decode([{4, a}, b, {2, c}, {2, a}, d, {4, e}]). [a,a,a,a,b,c,c,a,a,d,e,e,e,e]
No report needs to be handed in.
The program source file must include at the top the authors’ personal information (name and student ID) within comments. For example:
%---------------------------------------------------------- % Práctica #5: Erlang secuencial % Date: March 17, 2019. % Authors: % A01166611 Pepper Pots % A01160611 Anthony Stark %----------------------------------------------------------
Para entregar el archivo sequential.erl
, ingresa los siguientes datos:
Only one team member needs to upload the file.
Due date is Sunday, March 17.
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. |
1 | The program was plagiarized in whole or in part. |
10-100 | Depending on the amount of exercises that were solved correctly. |