Difference between revisions of "SML Practice Problems A"
Line 21: | Line 21: | ||
==addOpt== | ==addOpt== | ||
− | Write a function <code>addOpt : int option * int option -> int option</code> that given two "optional" integers, adds them if they are both present (returning | + | Write a function <code>addOpt : int option * int option -> int option</code> that given two "optional" integers, adds them if they are both present (returning <code>SOME</code> of their sum), or returns <code>NONE</code> if at least one of the two arguments is <code>NONE</code>. |
==addAllOpt== | ==addAllOpt== | ||
− | Write a function <code>addAllOpt : int option list -> int option</code> that given a list of "optional" integers, adds those integers that are there (i.e. adds all the | + | Write a function <code>addAllOpt : int option list -> int option</code> that given a list of "optional" integers, adds those integers that are there (i.e. adds all the <code>SOME i</code>). For example: <code>addAllOpt ([SOME 1, NONE, SOME 3]) = SOME 4</code>. If the list does not contain any <code>SOME i</code> is in it, i.e. they are all <code>NONE</code> or the list is empty, the function should return <code>NONE</code>. |
==any== | ==any== | ||
− | Write a function <code>any : bool list -> bool</code> that given a list of booleans returns | + | Write a function <code>any : bool list -> bool</code> that given a list of booleans returns <code>true</code> if there is at least one of them that is <code>true</code>, otherwise returns <code>false</code>. (If the list is empty it should return <code>false</code> because there is no <code>true</code>.) |
==all== | ==all== | ||
− | Write a function <code>all : bool list -> bool</code> that given a list of booleans returns \verb|true|true if all of them \verb|true|true, otherwise returns | + | Write a function <code>all : bool list -> bool</code> that given a list of booleans returns \verb|true|true if all of them \verb|true|true, otherwise returns <code>false</code>. (If the list is empty it should return <code>true</code> because there is no <code>false</code>.) |
==zip== | ==zip== | ||
− | Write a function <code>zip : int list * int list -> int * int</code> that given two lists of integers creates consecutive pairs, and stops when one of the lists is empty. For example: | + | Write a function <code>zip : int list * int list -> int * int</code> that given two lists of integers creates consecutive pairs, and stops when one of the lists is empty. For example: <code>zip ([1,2,3], [4, 6]) = [(1,4), (2,6)]</code>. |
==zipRecycle== | ==zipRecycle== |
Revision as of 06:56, 17 September 2020
credit: Charilaos Skiadas
Coursera Week 2 Extra Practice Problems
Contents
Code To Implement
file: | src/main/sml/practice1/practice1.sml | |
functions: | alternate min_max cumsum greeting repeat addOpt addAllOpt any all ... |
alternate
Write a function alternate : int list -> int
that takes a list of numbers and adds them with alternating sign. For example alternate [1,2,3,4] = 1 - 2 + 3 - 4 = -2
.
min_max
Write a function min_max : int list -> int * int
that takes a non-empty list of numbers, and returns a pair (min, max)
of the minimum and maximum of the numbers in the list.
cumsum
Write a function cumsum : int list -> int list
that takes a list of numbers and returns a list of the partial sums of those numbers. For example cumsum [1,4,20] = [1,5,25]
.
greeting
Write a function greeting : string option -> string
that given a string option \verb|SOME|SOME name returns the string "Hello there, ...!"
where the dots would be replaced by name. Note that the name is given as an option, so if it is NONE
then replace the dots with "you"
.
repeat
Write a function repeat : int list * int list -> int list
that given a list of integers and another list of nonnegative integers, repeats the integers in the first list according to the numbers indicated by the second list. For example: repeat ([1,2,3], [4,0,3]) = [1,1,1,1,3,3,3]
.
addOpt
Write a function addOpt : int option * int option -> int option
that given two "optional" integers, adds them if they are both present (returning SOME
of their sum), or returns NONE
if at least one of the two arguments is NONE
.
addAllOpt
Write a function addAllOpt : int option list -> int option
that given a list of "optional" integers, adds those integers that are there (i.e. adds all the SOME i
). For example: addAllOpt ([SOME 1, NONE, SOME 3]) = SOME 4
. If the list does not contain any SOME i
is in it, i.e. they are all NONE
or the list is empty, the function should return NONE
.
any
Write a function any : bool list -> bool
that given a list of booleans returns true
if there is at least one of them that is true
, otherwise returns false
. (If the list is empty it should return false
because there is no true
.)
all
Write a function all : bool list -> bool
that given a list of booleans returns \verb|true|true if all of them \verb|true|true, otherwise returns false
. (If the list is empty it should return true
because there is no false
.)
zip
Write a function zip : int list * int list -> int * int
that given two lists of integers creates consecutive pairs, and stops when one of the lists is empty. For example: zip ([1,2,3], [4, 6]) = [(1,4), (2,6)]
.
zipRecycle
Challenge: Write a version zipRecycle
of zip
, where when one list is empty it starts recycling from its start until the other list completes. For example: \verb|zipRecycle ([1,2,3], [1, 2, 3, 4, 5, 6, 7]) =|zipRecycle ([1,2,3], [1, 2, 3, 4, 5, 6, 7]) = \verb|[(1,1), (2,2), (3, 3), (1,4), (2,5), (3,6), (1,7)]|[(1,1), (2,2), (3, 3), (1,4), (2,5), (3,6), (1,7)].
zipOpt
Lesser challenge: Write a version zipOpt
of zip
with return type \verb|(int * int) list option|(int * int) list option. This version should return \verb|SOME|SOME of a list when the original lists have the same length, and \verb|NONE|NONE if they do not.
Write a function \verb|lookup : (string * int) list * string -> int option|lookup : (string * int) list * string -> int option that takes a list of pairs \verb|(s, i)|(s, i) and also a string \verb|s2|s2 to look up. It then goes through the list of pairs looking for the string \verb|s2|s2 in the first component. If it finds a match with corresponding number \verb|i|i, then it returns \verb|SOME i|SOME i. If it does not, it returns \verb|NONE|NONE.
splitup
Write a function splitup : int list -> int list * int list
that given a list of integers creates two lists of integers, one containing the non-negative entries, the other containing the negative entries. Relative order must be preserved: All non-negative entries must appear in the same order in which they were on the original list, and similarly for the negative entries.
splitAt
Write a version splitAt : int list * int -> int list * int list
of the previous function that takes an extra "threshold" parameter, and uses that instead of 0 as the separating point for the two resulting lists.
isSorted
Write a function isSorted : int list -> boolean
that given a list of integers determines whether the list is sorted in increasing order.
isAnySorted
Write a function isAnySorted : int list -> boolean
, that given a list of integers determines whether the list is sorted in either increasing or decreasing order.
sortedMerge
Write a function sortedMerge : int list * int list -> int list
that takes two lists of integers that are each sorted from smallest to largest, and merges them into one sorted list. For example: \verb|sortedMerge ([1,4,7], [5,8,9]) = [1,4,5,7,8,9]|sortedMerge ([1,4,7], [5,8,9]) = [1,4,5,7,8,9].
qsort
Write a sorting function qsort : int list -> int list
that works as follows: Takes the first element out, and uses it as the "threshold" for \verb|splitAt|splitAt. It then recursively sorts the two lists produced by \verb|splitAt|splitAt. Finally it brings the two lists together. (Don't forget that element you took out, it needs to get back in at some point). You could use \verb|sortedMerge|sortedMerge for the "bring together" part, but you do not need to as all the numbers in one list are less than all the numbers in the other.)
divide
Write a function divide : int list -> int list * int list
that takes a list of integers and produces two lists by alternating elements between the two lists. For example: \verb|divide ([1,2,3,4,5,6,7]) = ([1,3,5,7], [2,4,6])|divide ([1,2,3,4,5,6,7]) = ([1,3,5,7], [2,4,6]).
Write another sorting function not_so_quick_sort : int list -> int list
that works as follows: Given the initial list of integers, splits it in two lists using divide, then recursively sorts those two lists, then merges them together with \verb|sortedMerge|sortedMerge.
fullDivide
Write a function fullDivide : int * int -> int * int
that given two numbers \verb|k|k and \verb|n|n it attempts to evenly divide \verb|k|k into \verb|n|n as many times as possible, and returns a pair \verb|(d, n2)|(d, n2) where \verb|d|d is the number of times while \verb|n2|n2 is the resulting \verb|n|n after all those divisions. Examples: \verb|fullDivide (2, 40) = (3, 5)|fullDivide (2, 40) = (3, 5) because \verb|2*2*2*5 = 40|2*2*2*5 = 40 and \verb|fullDivide((3,10)) = (0, 10) |fullDivide((3,10)) = (0, 10) because \verb|3|3 does not divide \verb|10|10.
factorize
Using fullDivide
, write a function factorize : int -> (int * int) list
that given a number \verb|n|n returns a list of pairs \verb|(d, k)|(d, k) where \verb|d|d is a prime number dividing \verb|n|n and \verb|k|k is the number of times it fits. The pairs should be in increasing order of prime factor, and the process should stop when the divisor considered surpasses the square root of \verb|n|n. If you make sure to use the reduced number \verb|n2|n2 given by \verb|fullDivide|fullDivide for each next step, you should not need to test if the divisors are prime: If a number divides into \verb|n|n, it must be prime (if it had prime factors, they would have been earlier prime factors of \verb|n|n and thus reduced earlier). Examples: \verb|factorize(20) = [(2,2), (5,1)]|factorize(20) = [(2,2), (5,1)]; \verb|factorize(36) = [(2,2), (3,2)]|factorize(36) = [(2,2), (3,2)]; \verb|factorize(1) = []|factorize(1) = [].
multiply
Write a function multiply : (int * int) list -> int
that given a factorization of a number \verb|n|n as described in the previous problem computes back the number \verb|n|n. So this should do the opposite of \verb|factorize|factorize.
all_products
Challenge (hard): Write a function all_products : (int * int) list -> int list
that given a factorization list result from \verb|factorize|factorize creates a list all of possible products produced from using some or all of those prime factors no more than the number of times they are available. This should end up being a list of all the divisors of the number \verb|n|n that gave rise to the list. Example: \verb|all_products([(2,2), (5,1)]) = [1,2,4,5,10,20]|all_products([(2,2), (5,1)]) = [1,2,4,5,10,20]. For extra challenge, your recursive process should return the numbers in this order, as opposed to sorting them afterwards.
Test
file: | unit_test_practice1.sml | |
source folder: | src/test/sml/practice1 |