vault backup: 2023-10-06 14:15:02

This commit is contained in:
Louis Gallet 2023-10-06 14:15:02 +02:00
parent e8e6e80444
commit 0b9230c8cb
Signed by: lgallet
SSH Key Fingerprint: SHA256:qnW7pk4EoMRR0UftZLZQKSMUImbEFsiruLC7jbCHJAY

View File

@ -64,3 +64,22 @@ let rec max_value list = match list with
## 1.6 - Bonus second
```Ocaml
let rec second_smallest list =
match list with
| [] | [_] -> failwith "La liste ne contient pas au moins deux éléments distincts"
| [x; y] -> if x < y then y else x
| hd1 :: hd2 :: tl ->
let min1, min2 =
if hd1 < hd2 then (hd1, hd2) else (hd2, hd1)
in
let rec find_second_smallest rest =
match rest with
| [] -> min2
| hd :: tl ->
if hd < min1 then find_second_smallest (min1 :: tl)
else if hd < min2 && hd > min1 then find_second_smallest (hd :: min1 :: tl)
else find_second_smallest (min2 :: tl)
in
find_second_smallest tl
```