This commit is contained in:
Arkitu 2025-12-12 11:57:16 +01:00
parent 363950478d
commit 17230c0cd0
6 changed files with 218 additions and 0 deletions

32
dm5/dm5.opam Normal file
View File

@ -0,0 +1,32 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "A short synopsis"
description: "A longer description"
maintainer: ["Maintainer Name <maintainer@example.com>"]
authors: ["Author Name <author@example.com>"]
license: "LICENSE"
tags: ["add topics" "to describe" "your" "project"]
homepage: "https://github.com/username/reponame"
doc: "https://url/to/documentation"
bug-reports: "https://github.com/username/reponame/issues"
depends: [
"dune" {>= "3.20"}
"ocaml"
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/username/reponame.git"
x-maintenance-intent: ["(latest)"]

26
dm5/dune-project Normal file
View File

@ -0,0 +1,26 @@
(lang dune 3.20)
(name dm5)
(generate_opam_files true)
(source
(github username/reponame))
(authors "Author Name <author@example.com>")
(maintainers "Maintainer Name <maintainer@example.com>")
(license LICENSE)
(documentation https://url/to/documentation)
(package
(name dm5)
(synopsis "A short synopsis")
(description "A longer description")
(depends ocaml)
(tags
("add topics" "to describe" your project)))
; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html

3
dm5/lib/dune Normal file
View File

@ -0,0 +1,3 @@
(library
(name lib)
(public_name dm5))

131
dm5/lib/lib.ml Normal file
View File

@ -0,0 +1,131 @@
(* Exercice 0 *)
let zip ta tb = Array.map2 (fun a b -> a,b) ta tb;;
(* Exercice 1 *)
let tirette t =
let n = Array.length t in
Array.init (n/2) (fun i -> t.(i) + t.(n-i))
;;
(* Exercice 2 *)
let quarante_deux t =
let n = Array.length t in
let count = ref 0 in
let i = ref 0 in
while !i < n do
if t.(!i) mod 42 = 0 then
count := !count + 1
else ()
done;
!count
;;
(* Exercice 3 *)
let croissant t =
let n = Array.length t in
let i = ref 0 in
while !i+1 < n && t.(!i) <= t.(!i+1) do
i := !i+1
done;
!i
;;
(* Exercice 4 *)
let bitonique t =
let n = Array.length t in
let grows = ref true in
let i = ref 0 in
let res = ref true in
while !res && !i+1 < n do
if !grows then
(if t.(!i) > t.(!i+1) then
grows := false)
else
if t.(!i) < t.(!i+1) then
res := false
done;
!res
;;
(* Exercice 5 *)
let psrn ta tb = Array.fold_left
(fun acc (a,b) -> acc + (a*b))
0
(Array.map2
(fun a b -> a,b)
ta
tb
)
;;
(* Exercice 6 *)
let rotation m =
let nx = Array.length m in
if nx = 0 then [||] else
let ny = Array.length m.(0) in
Array.init_matrix
ny
nx
(fun x y -> m.(y).(ny-x-1))
;;
(* Exercice 7 *)
let plsci t =
let n = Array.length t in
let current_start = ref 0 in
let start = ref 0 in
let max_length = ref (min 1 n) in
for i = 0 to n-1 do
if i+1 = n || t.(i) != t.(i+1) then (
let length = i - !current_start + 1 in
if !max_length < length then (
max_length := length;
start := !current_start
);
current_start := i+1
)
done;
!start, !max_length
;;
(* Exercice 8 *)
let sorteren t =
let zero, one, two = Array.fold_left
(fun (zero, one, two) a -> match a with
| 0 -> zero+1,one,two
| 1 -> zero,one+1,two
| 2 -> zero,one,two+1
| _ -> invalid_arg ""
)
(0,0,0)
t
in
Array.init
(zero+one+two)
(fun i ->
if i < zero then 0
else if i < zero+one then 1
else 2
)
;;
(* Exercice 9 *)
let dominant s =
let counts = Array.make 0 126 in
String.iter (fun c ->
let c = Char.code c in
counts.(c) <- counts.(c) + 1;
) s;
let _, max_c =
Array.fold_left
(fun (c, max_c) a ->
c+1,
if a > counts.(max_c) then c else max_c
)
(0,0)
counts
in
Char.chr max_c
;;

4
dm5/test/dune Normal file
View File

@ -0,0 +1,4 @@
(executable
(name test)
(public_name test)
(libraries lib))

22
dm5/test/test.ml Normal file
View File

@ -0,0 +1,22 @@
open Lib;;
assert (rotation
[|
[| 1; 5; 9 |];
[| 2; 6; 10 |];
[| 3; 7; 11 |];
[| 4; 8; 12 |]
|]
=
[|
[| 9; 10; 11; 12 |];
[| 5; 6; 7; 8 |];
[| 1; 2; 3; 4 |]
|]
);;
assert (rotation [||] = [||]);;
assert (rotation [|[||]; [||]|] = [||]);;
assert (plsci [||] = (0,0));;
assert (plsci [|1|] = (0,1));;
assert (plsci [|1; 1|] = (0,2));;