vault backup: 2023-10-06 14:15:02
This commit is contained in:
parent
e8e6e80444
commit
0b9230c8cb
@ -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
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user