- rtshkmr's digital garden/
- References/
- Algo Reference/
- 99 OCaml Problems/
- 99 OCaml: Intermediate Exercises/
99 OCaml: Intermediate Exercises
Table of Contents
Intermediate problems taken from here. Instead of just being a store for solutions, this aims to actually go deeper and float all the learning takeaways that I had when completing the problem set. The code-snippets here can be tangled in emacs I will expose this code as an org-file publicly once I’ve completed the 99 problems. It will be hosted on Codeberg, hopefully it helps others. and can be executed directly.

TODO Lists #
1. Flatten a List #
Flatten a nested list structure.
| |
2. Eliminate Duplicates #
Eliminate consecutive duplicates of list elements.
| |
3. Pack Consecutive Duplicates #
Pack consecutive duplicates of list elements into sublists.
| |
4. Decode a Run-Length Encoded List #
Given a run-length code list generated as specified in the previous problem, construct its uncompressed version.
| |
5. Run-Length Encoding of a List (Direct Solution) #
Implement the so-called run-length encoding data compression method directly.
I.e. don’t explicitly create the sublists containing the duplicates, as in problem “Pack consecutive duplicates of list elements into sublists”, but only count them.
As in problem “Modified run-length encoding”, simplify the result list by replacing the singleton lists (1 X) by X.
6. Replicate the Elements of a List a Given Number of Times #
Replicate the elements of a list a given number of times.
| |
7. Drop Every N’th Element From a List #
Drop every N’th element from a list.
| |
8. Extract a Slice From a List #
Given two indices,
iandk, the slice is the list containing the elements between thei‘th andk‘th element of the original list (both limits included).Start counting the elements with 0 (this is the way the List module numbers elements).
| |
9. Rotate a List N Places to the Left #
Rotate a list
Nplaces to the left.
| |
10. Extract a Given Number of Randomly Selected Elements From a List #
The selected items shall be returned in a list. We use the Random module and initialise it with
Random.init 0at the start of the function for reproducibility and validate the solution. To make the function truly random, however, one should remove the call toRandom.init 0.
| |
11. Generate the Combinations of K Distinct Objects Chosen From the N Elements of a List #
Generate the combinations of K distinct objects chosen from the N elements of a list.
In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficients). For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.
| |
12. Group the Elements of a Set Into Disjoint Subsets #
Group the elements of a set into disjoint subsets
- In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities and returns them in a list.
- Generalize the above function in a way that we can specify a list of group sizes and the function will return a list of groups.
| |
13. Sorting a List of Lists According to Length of Sublists #
Sorting a list of lists according to length of sublists.
We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of this list according to their length. E.g. short lists first, longer lists later, or vice versa.
Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements of this list according to their length frequency; i.e., in the default, where sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length come later.
| |
TODO Arithmetic #
14. Determine Whether a Given Integer Number Is Prime #
Determine whether a given integer number is prime.
| |
15. Determine the Greatest Common Divisor of Two Positive Integer Numbers #
Determine the greatest common divisor of two positive integer numbers.
Use Euclid’s algorithm.
my v0 GCD solution (works, but inefficient)
| |
| |
| |
abs only one time on entry (negative handling), recursion is purely arithmetic16. Calculate Euler’s Totient Function \(\phi(m)\) #
Euler’s so-called totient function \(\phi(m)\) is defined as the number of positive integers
r(1 ≤ r < m) that are coprime tom. We let \(\phi(1)\) = 1.Find out what the value of \(\phi(m)\) is if
mis a prime number.Euler’s totient function plays an important role in one of the most widely used public key cryptography methods (RSA). In this exercise you should use the most primitive method to calculate this function (there are smarter ways that we shall discuss later).
| |
| |
17. Determine the Prime Factors of a Given Positive Integer #
Construct a flat list containing the prime factors in ascending order.
| |
18. Determine the Prime Factors of a Given Positive Integer (2) #
Construct a list containing the prime factors and their multiplicity.
Hint: The problem is similar to problem Run-length encoding of a list (direct solution).
| |
19. Calculate Euler’s Totient Function \(\phi(m)\) (Improved) #
See problem “Calculate Euler’s totient function \(\phi(m)\)” for the definition of Euler’s totient function.
If the list of the prime factors of a number
mis known in the form of the previous problem then the functionphi(m)can be efficiently calculated as follows: Let[(p1, m1); (p2, m2); (p3, m3); ...]be the list of prime factors (and their multiplicities) of a given numberm. Then \(\phi(m)\) can be calculated with the following formula:\[ \phi(m) \newline = (p1 - 1) \times p1^{m1 - 1} \times (p2 - 1) \times p2^{m2 - 1} \times (p3 - 1) \times p3^{m3 - 1} \times … \]
| |
20. Goldbach’s Conjecture #
Goldbach’s conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been numerically confirmed up to very large numbers. Write a function to find the two prime numbers that sum up to a given even integer.
| |
21. A List of Goldbach Compositions #
Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.
In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000.
| |