mirror of
https://github.com/BreizhHardware/TD-R.git
synced 2026-01-18 16:47:34 +01:00
Initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
.idea
|
||||||
|
.idea/
|
||||||
45
TD/.Rhistory
Normal file
45
TD/.Rhistory
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
print("Hello World")
|
||||||
|
x+2
|
||||||
|
print(x+2)
|
||||||
|
x+2
|
||||||
|
(5^2+5)/3
|
||||||
|
sum(log(32,2), 8^4, 4+5, 12/3)
|
||||||
|
sum(50, 3, 5)
|
||||||
|
log10(90+5*2)
|
||||||
|
variable2
|
||||||
|
variable2 + 2
|
||||||
|
mean(variable2)
|
||||||
|
variable2 <- c(12, 1, 15, 3, 20, 6, 11)
|
||||||
|
variable2
|
||||||
|
mean(variable2)
|
||||||
|
variable2 + 2
|
||||||
|
variable2 <- c(12, 1, 15, 3, 20, 6, 11)
|
||||||
|
variable2
|
||||||
|
mean(variable2)
|
||||||
|
variable2 + 2
|
||||||
|
x = c(1, 3, 5, 7, 9)
|
||||||
|
y = c(2, 3, 5, 7, 11, 13)
|
||||||
|
z = c(9, 3, 2, 5, 9, 2, 3, 9, 1)
|
||||||
|
x + 2
|
||||||
|
y * 3
|
||||||
|
length(x)
|
||||||
|
x + y
|
||||||
|
sum(x > 5)
|
||||||
|
sum(x[x > 5])
|
||||||
|
sum(x > 5 | x < 3)
|
||||||
|
y[3]
|
||||||
|
y[-3]
|
||||||
|
y[x]
|
||||||
|
(y > 7)
|
||||||
|
y[y > 7]
|
||||||
|
sort(z)
|
||||||
|
sort(z, dec = TRUE)
|
||||||
|
rev(z)
|
||||||
|
order(z)
|
||||||
|
unique(z)
|
||||||
|
duplicated(z)
|
||||||
|
table(z)
|
||||||
|
rep(z, 3)
|
||||||
|
vector1 <- c(1, 2, 3)
|
||||||
|
vector2 <- c(4, 5, 6)
|
||||||
|
vector3 <- c(vector1[1:2], vector2, vector1[3:length(vector1)])
|
||||||
9
TD/.Rproj.user/F9ECB55E/pcs/files-pane.pper
Normal file
9
TD/.Rproj.user/F9ECB55E/pcs/files-pane.pper
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"sortOrder": [
|
||||||
|
{
|
||||||
|
"columnIndex": 2,
|
||||||
|
"ascending": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"path": "C:/Users/BreizhHardware/Nextcloud/Programation/R/TD"
|
||||||
|
}
|
||||||
3
TD/.Rproj.user/F9ECB55E/pcs/source-pane.pper
Normal file
3
TD/.Rproj.user/F9ECB55E/pcs/source-pane.pper
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"activeTab": 2
|
||||||
|
}
|
||||||
14
TD/.Rproj.user/F9ECB55E/pcs/windowlayoutstate.pper
Normal file
14
TD/.Rproj.user/F9ECB55E/pcs/windowlayoutstate.pper
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"left": {
|
||||||
|
"splitterpos": 393,
|
||||||
|
"topwindowstate": "NORMAL",
|
||||||
|
"panelheight": 945,
|
||||||
|
"windowheight": 983
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"splitterpos": 589,
|
||||||
|
"topwindowstate": "NORMAL",
|
||||||
|
"panelheight": 945,
|
||||||
|
"windowheight": 983
|
||||||
|
}
|
||||||
|
}
|
||||||
5
TD/.Rproj.user/F9ECB55E/pcs/workbench-pane.pper
Normal file
5
TD/.Rproj.user/F9ECB55E/pcs/workbench-pane.pper
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"TabSet1": 0,
|
||||||
|
"TabSet2": 0,
|
||||||
|
"TabZoom": {}
|
||||||
|
}
|
||||||
5
TD/.Rproj.user/F9ECB55E/rmd-outputs
Normal file
5
TD/.Rproj.user/F9ECB55E/rmd-outputs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
1
TD/.Rproj.user/F9ECB55E/saved_source_markers
Normal file
1
TD/.Rproj.user/F9ECB55E/saved_source_markers
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"active_set":"","sets":[]}
|
||||||
26
TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA
Normal file
26
TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"id": "B62E70DA",
|
||||||
|
"path": "C:/Users/BreizhHardware/Nextcloud/Programation/R/TD/Exo3.R",
|
||||||
|
"project_path": "Exo3.R",
|
||||||
|
"type": "r_source",
|
||||||
|
"hash": "340809373",
|
||||||
|
"contents": "",
|
||||||
|
"dirty": false,
|
||||||
|
"created": 1733479137294.0,
|
||||||
|
"source_on_save": false,
|
||||||
|
"relative_order": 3,
|
||||||
|
"properties": {
|
||||||
|
"source_window_id": "",
|
||||||
|
"Source": "Source",
|
||||||
|
"cursorPosition": "0,0",
|
||||||
|
"scrollLine": "0"
|
||||||
|
},
|
||||||
|
"folds": "",
|
||||||
|
"lastKnownWriteTime": 1733479598,
|
||||||
|
"encoding": "UTF-8",
|
||||||
|
"collab_server": "",
|
||||||
|
"source_window": "",
|
||||||
|
"last_content_update": 1733479598,
|
||||||
|
"read_only": false,
|
||||||
|
"read_only_alternatives": []
|
||||||
|
}
|
||||||
14
TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA-contents
Normal file
14
TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA-contents
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# Crée des vecteurs
|
||||||
|
# y0 constitué de la suite des entiers de 0 à 10 par pas de 2
|
||||||
|
y0 <- seq(0, 10, by=2)
|
||||||
|
# y1 constitué de tous les entiers pairs entre 1 et 18
|
||||||
|
y1 <- seq(2, 18, by=2)
|
||||||
|
# y2 constitué de 20 fois de suite la valeur 4
|
||||||
|
y2 <- rep(4, 20)
|
||||||
|
# y3 constitué de 20 nombres entre 0 et 10
|
||||||
|
y3 <- runif(20, 0, 10)
|
||||||
|
|
||||||
|
# Extraire de y3
|
||||||
|
# le troisième élément
|
||||||
|
y33 <- y3[2]
|
||||||
|
# tous les éléments sauf le troisième
|
||||||
26
TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4
Normal file
26
TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"id": "C229FEB4",
|
||||||
|
"path": "C:/Users/BreizhHardware/Nextcloud/Programation/R/TD/Exo2.R",
|
||||||
|
"project_path": "Exo2.R",
|
||||||
|
"type": "r_source",
|
||||||
|
"hash": "3327678100",
|
||||||
|
"contents": "",
|
||||||
|
"dirty": false,
|
||||||
|
"created": 1733478990873.0,
|
||||||
|
"source_on_save": false,
|
||||||
|
"relative_order": 2,
|
||||||
|
"properties": {
|
||||||
|
"source_window_id": "",
|
||||||
|
"Source": "Source",
|
||||||
|
"cursorPosition": "3,63",
|
||||||
|
"scrollLine": "0"
|
||||||
|
},
|
||||||
|
"folds": "",
|
||||||
|
"lastKnownWriteTime": 1733479100,
|
||||||
|
"encoding": "UTF-8",
|
||||||
|
"collab_server": "",
|
||||||
|
"source_window": "",
|
||||||
|
"last_content_update": 1733479100521,
|
||||||
|
"read_only": false,
|
||||||
|
"read_only_alternatives": []
|
||||||
|
}
|
||||||
4
TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4-contents
Normal file
4
TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4-contents
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
vector1 <- c(1, 2, 3)
|
||||||
|
vector2 <- c(4, 5, 6)
|
||||||
|
|
||||||
|
vector3 <- c(vector1[1:2], vector2, vector1[3:length(vector1)])
|
||||||
26
TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E
Normal file
26
TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"id": "DA0B538E",
|
||||||
|
"path": "C:/Users/BreizhHardware/Nextcloud/Programation/R/TD/Exo1.R",
|
||||||
|
"project_path": "Exo1.R",
|
||||||
|
"type": "r_source",
|
||||||
|
"hash": "1460566234",
|
||||||
|
"contents": "",
|
||||||
|
"dirty": false,
|
||||||
|
"created": 1733478588935.0,
|
||||||
|
"source_on_save": false,
|
||||||
|
"relative_order": 1,
|
||||||
|
"properties": {
|
||||||
|
"source_window_id": "",
|
||||||
|
"Source": "Source",
|
||||||
|
"cursorPosition": "19,8",
|
||||||
|
"scrollLine": "0"
|
||||||
|
},
|
||||||
|
"folds": "",
|
||||||
|
"lastKnownWriteTime": 1733478955,
|
||||||
|
"encoding": "UTF-8",
|
||||||
|
"collab_server": "",
|
||||||
|
"source_window": "",
|
||||||
|
"last_content_update": 1733478955036,
|
||||||
|
"read_only": false,
|
||||||
|
"read_only_alternatives": []
|
||||||
|
}
|
||||||
24
TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E-contents
Normal file
24
TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E-contents
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
x = c(1, 3, 5, 7, 9)
|
||||||
|
y = c(2, 3, 5, 7, 11, 13)
|
||||||
|
z = c(9, 3, 2, 5, 9, 2, 3, 9, 1)
|
||||||
|
|
||||||
|
x + 2
|
||||||
|
y * 3
|
||||||
|
length(x)
|
||||||
|
x + y
|
||||||
|
sum(x > 5)
|
||||||
|
sum(x[x > 5])
|
||||||
|
sum(x > 5 | x < 3)
|
||||||
|
y[3]
|
||||||
|
y[-3]
|
||||||
|
y[x]
|
||||||
|
(y > 7)
|
||||||
|
y[y > 7]
|
||||||
|
sort(z)
|
||||||
|
sort(z, dec = TRUE)
|
||||||
|
rev(z)
|
||||||
|
order(z)
|
||||||
|
unique(z)
|
||||||
|
duplicated(z)
|
||||||
|
table(z)
|
||||||
|
rep(z, 3)
|
||||||
6
TD/.Rproj.user/F9ECB55E/sources/prop/1489AED4
Normal file
6
TD/.Rproj.user/F9ECB55E/sources/prop/1489AED4
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"source_window_id": "",
|
||||||
|
"Source": "Source",
|
||||||
|
"cursorPosition": "0,0",
|
||||||
|
"scrollLine": "0"
|
||||||
|
}
|
||||||
6
TD/.Rproj.user/F9ECB55E/sources/prop/57E49702
Normal file
6
TD/.Rproj.user/F9ECB55E/sources/prop/57E49702
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"source_window_id": "",
|
||||||
|
"Source": "Source",
|
||||||
|
"cursorPosition": "3,63",
|
||||||
|
"scrollLine": "0"
|
||||||
|
}
|
||||||
6
TD/.Rproj.user/F9ECB55E/sources/prop/74FBB217
Normal file
6
TD/.Rproj.user/F9ECB55E/sources/prop/74FBB217
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"source_window_id": "",
|
||||||
|
"Source": "Source",
|
||||||
|
"cursorPosition": "19,8",
|
||||||
|
"scrollLine": "0"
|
||||||
|
}
|
||||||
3
TD/.Rproj.user/F9ECB55E/sources/prop/INDEX
Normal file
3
TD/.Rproj.user/F9ECB55E/sources/prop/INDEX
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
C%3A%2FUsers%2FBreizhHardware%2FNextcloud%2FProgramation%2FR%2FTD%2FExo1.R="74FBB217"
|
||||||
|
C%3A%2FUsers%2FBreizhHardware%2FNextcloud%2FProgramation%2FR%2FTD%2FExo2.R="57E49702"
|
||||||
|
C%3A%2FUsers%2FBreizhHardware%2FNextcloud%2FProgramation%2FR%2FTD%2FExo3.R="1489AED4"
|
||||||
0
TD/.Rproj.user/shared/notebooks/patch-chunk-names
Normal file
0
TD/.Rproj.user/shared/notebooks/patch-chunk-names
Normal file
24
TD/Exo1.R
Normal file
24
TD/Exo1.R
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
x = c(1, 3, 5, 7, 9)
|
||||||
|
y = c(2, 3, 5, 7, 11, 13)
|
||||||
|
z = c(9, 3, 2, 5, 9, 2, 3, 9, 1)
|
||||||
|
|
||||||
|
x + 2
|
||||||
|
y * 3
|
||||||
|
length(x)
|
||||||
|
x + y
|
||||||
|
sum(x > 5)
|
||||||
|
sum(x[x > 5])
|
||||||
|
sum(x > 5 | x < 3)
|
||||||
|
y[3]
|
||||||
|
y[-3]
|
||||||
|
y[x]
|
||||||
|
(y > 7)
|
||||||
|
y[y > 7]
|
||||||
|
sort(z)
|
||||||
|
sort(z, dec = TRUE)
|
||||||
|
rev(z)
|
||||||
|
order(z)
|
||||||
|
unique(z)
|
||||||
|
duplicated(z)
|
||||||
|
table(z)
|
||||||
|
rep(z, 3)
|
||||||
4
TD/Exo2.R
Normal file
4
TD/Exo2.R
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
vector1 <- c(1, 2, 3)
|
||||||
|
vector2 <- c(4, 5, 6)
|
||||||
|
|
||||||
|
vector3 <- c(vector1[1:2], vector2, vector1[3:length(vector1)])
|
||||||
39
TD/Exo3.R
Normal file
39
TD/Exo3.R
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# Crée des vecteurs
|
||||||
|
# y0 constitué de la suite des entiers de 0 à 10 par pas de 2
|
||||||
|
y0 <- seq(0, 10, by=2)
|
||||||
|
# y1 constitué de tous les entiers pairs entre 1 et 18
|
||||||
|
y1 <- seq(2, 18, by=2)
|
||||||
|
# y2 constitué de 20 fois de suite la valeur 4
|
||||||
|
y2 <- rep(4, 20)
|
||||||
|
# y3 constitué de 20 nombres entre 0 et 10
|
||||||
|
y3 <- runif(20, 0, 10)
|
||||||
|
|
||||||
|
# Extraire de y3
|
||||||
|
# le troisième élément
|
||||||
|
y33 <- y3[3]
|
||||||
|
# tous les éléments sauf le troisième
|
||||||
|
y34 <- y3[-3]
|
||||||
|
|
||||||
|
# Comparer les commandes suivantes
|
||||||
|
matrix(y3, nrow = 2)
|
||||||
|
matrix(y3, byrow = TRUE)
|
||||||
|
|
||||||
|
# Construire une matrice A comportant quatre lignes et trois colonnes remplies par lignes
|
||||||
|
# successives avec les éléments du vecteur 1:12
|
||||||
|
A <- matrix(1:12, nrow = 4, byrow = TRUE)
|
||||||
|
|
||||||
|
# Construire une matrice B comportant quatre lignes et trois colonnes remplies par
|
||||||
|
# colonnes successives avec les éléments du vecteur 1:12
|
||||||
|
B <- matrix(1:12, nrow = 4, byrow = FALSE)
|
||||||
|
|
||||||
|
# Extraire l’élément situé en deuxième ligne et troisième colonne de A
|
||||||
|
A[2, 3]
|
||||||
|
|
||||||
|
# Extraire la première colonne de A, puis la deuxième ligne de A.
|
||||||
|
A[, 1]
|
||||||
|
A[2, ]
|
||||||
|
|
||||||
|
# Construire une matrice C constituée des lignes 1 et 4 de A.
|
||||||
|
C <- A[c(1, 4), ]
|
||||||
|
print(A)
|
||||||
|
print(C)
|
||||||
3
TD/Exo4.R
Normal file
3
TD/Exo4.R
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Construire une matrice comportant 9 lignes et 9 colonnes avec des 0 sur la diagonale et des 1
|
||||||
|
# partout ailleurs (on pourra utiliser la commande diag)
|
||||||
|
D <- matrix(1, nrow = 9, ncol = 9)
|
||||||
23
TD/Exo5.R
Normal file
23
TD/Exo5.R
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Créer un vecteur x = (x1, . . . , x11) contenant les réels compris entre 0 et 1 par pas de 0.1
|
||||||
|
x <- seq(0, 1, by=0.1)
|
||||||
|
|
||||||
|
# Afficher la longueur de x
|
||||||
|
print(length(x))
|
||||||
|
|
||||||
|
# En utilisant les opérations vectorielles, créer un vecteur y = 4x(1 − x)
|
||||||
|
y <- 4*x*(1-x)
|
||||||
|
|
||||||
|
# Tracer la courbe rejoignant les points (x1, y1), . . . , (x11, y11) avec la commande plot
|
||||||
|
plot(x, y)
|
||||||
|
|
||||||
|
# Calculer le maximum des y1, . . . , y11
|
||||||
|
print(max(y))
|
||||||
|
|
||||||
|
# En quel point le maximum est-il atteint ?
|
||||||
|
print(x[which.max(y)])
|
||||||
|
|
||||||
|
# Tracer la courbe de la fonction f (x) = 4x2(1 − x), x
|
||||||
|
# ∈ [−2, 1], en rouge
|
||||||
|
x <- seq(-2, 1, by=0.1)
|
||||||
|
y <- 4*x^2*(1-x)
|
||||||
|
plot(x, y, col="red")
|
||||||
20
TD/Exo6.R
Normal file
20
TD/Exo6.R
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Utiliser R pour donner les valeurs numériques attendues :
|
||||||
|
# Combien y-a-t-il de carrés (4 cartes de même valeur) dans un jeu de 32 cartes ?
|
||||||
|
nb_carres <- 8
|
||||||
|
nb_carres
|
||||||
|
|
||||||
|
# Combien d’anagrammes peut-on faire avec le mot "dinosaure" ?
|
||||||
|
anagrammes <- factorial(9) / factorial(2)
|
||||||
|
anagrammes
|
||||||
|
|
||||||
|
# Combien de chances a-t-on de gagner le super jackpot à l’euromillion ? (donc d’avoir 5
|
||||||
|
# bons numéros parmi 49, et 2 bons numéros étoilés parmi 10)
|
||||||
|
chances <- choose(49, 5) * choose(10, 2)
|
||||||
|
chances
|
||||||
|
|
||||||
|
# Chaque pièce d’un nouveau jeu de domino est de la forme : a b avec (a, b) ∈ {0, . . . , 9}2
|
||||||
|
# en sachant qu’un domino reste le même si on le tourne à 180 degrés (par exemple, 8 = 8
|
||||||
|
# est un, et un seul domino). Déterminer le nombre de pièces différentes que contient un
|
||||||
|
# jeu complet de dominos
|
||||||
|
nb_dominos <- choose(10, 2) + 10
|
||||||
|
nb_dominos
|
||||||
BIN
TD/Rplots.pdf
Normal file
BIN
TD/Rplots.pdf
Normal file
Binary file not shown.
13
TD/TD.Rproj
Normal file
13
TD/TD.Rproj
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
Version: 1.0
|
||||||
|
|
||||||
|
RestoreWorkspace: Default
|
||||||
|
SaveWorkspace: Default
|
||||||
|
AlwaysSaveHistory: Default
|
||||||
|
|
||||||
|
EnableCodeIndexing: Yes
|
||||||
|
UseSpacesForTab: Yes
|
||||||
|
NumSpacesForTab: 2
|
||||||
|
Encoding: UTF-8
|
||||||
|
|
||||||
|
RnwWeave: Sweave
|
||||||
|
LaTeX: pdfLaTeX
|
||||||
17
TD1/Exercice39.R
Normal file
17
TD1/Exercice39.R
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Question a
|
||||||
|
# Ici on cherche P(X <= 30) via une loi binomial avec 70 et 0.4
|
||||||
|
sum(dbinom(x=0:30,size=70,prob=0.4))
|
||||||
|
|
||||||
|
# Question b
|
||||||
|
# Ici on cherche P(X) >= 0.9 via la même loi binomial qu'avant
|
||||||
|
i <- 0
|
||||||
|
j <- 30
|
||||||
|
while (i < 0.9) {
|
||||||
|
i <- sum(dbinom(x=0:j, size=70, prob=0.4))
|
||||||
|
j <- j + 1
|
||||||
|
}
|
||||||
|
print(i)
|
||||||
|
print(j - 1)
|
||||||
|
|
||||||
|
# Bonus
|
||||||
|
print(dbinom(x=0:70, size=70, prob=0.4))
|
||||||
16
TD1/Exercice51.R
Normal file
16
TD1/Exercice51.R
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# Dans une station-service, la demande hebdomadaire en essence, en milliers de litres, est
|
||||||
|
# une variable aléatoire X de densité f (x) = c(1 − x)^4 1[0,1].
|
||||||
|
# a) Déterminer c
|
||||||
|
f <- function(x) {(1-x)^4}
|
||||||
|
result <- integrate(f, lower = 0, upper = 1)
|
||||||
|
# Je détermine C car intérale de c*f(x) = 1
|
||||||
|
c <- 1 / result$value
|
||||||
|
print(c)
|
||||||
|
# b) La station est réapprovisionnée chaque lundi à 20h. Quelle doit être la capacité du réservoir
|
||||||
|
# d’essence pour que la probabilité d’épuiser ce réservoir soit inférieure à 10^−5 ?
|
||||||
|
F <- function(x) {
|
||||||
|
integrate(function(t) c * (1 - t)^4, lower = 0, upper = x)$value
|
||||||
|
}
|
||||||
|
cible <- 1 - 10^(-5)
|
||||||
|
capacite <- uniroot(function(x) F(x) - cible, lower = 0, upper = 1)$root
|
||||||
|
print(capacite * 1000)
|
||||||
17
TD1/Exo1.R
Normal file
17
TD1/Exo1.R
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Une course oppose 20 concurrents, dont Émile
|
||||||
|
# a) Combien y-a-t-il de podiums possibles ?
|
||||||
|
podiums_total <- factorial(20) / factorial(20 - 3)
|
||||||
|
podiums_total
|
||||||
|
|
||||||
|
# b) Combien y-a-t-il de podiums possibles où Émile est premier ?
|
||||||
|
emile_premier <- 1 * (factorial(19) / factorial(17))
|
||||||
|
emile_premier
|
||||||
|
|
||||||
|
# c) Combien y-a-t-il de podiums possibles dont Émile fait partie ?
|
||||||
|
emile_podium <- 3 * (factorial(19) / factorial(17))
|
||||||
|
emile_podium
|
||||||
|
|
||||||
|
# d) On souhaite récompenser les 3 premiers en leur offrant un prix identique à chacun. Combien
|
||||||
|
# y-a-t-il de distributions de récompenses possibles ?
|
||||||
|
distributions <- choose(20, 3)
|
||||||
|
distributions
|
||||||
75
TD1/advent_of_code_06_12_2024.R
Normal file
75
TD1/advent_of_code_06_12_2024.R
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
simulate_guard <- function(input) {
|
||||||
|
# Transform the input into a matrix
|
||||||
|
rows <- unlist(strsplit(input, "\n"))
|
||||||
|
grid <- do.call(rbind, lapply(rows, function(row) unlist(strsplit(row, NULL))))
|
||||||
|
|
||||||
|
# Find the starting position and initial direction
|
||||||
|
start_pos <- which(grid %in% c("^", ">", "v", "<"), arr.ind = TRUE)
|
||||||
|
if (length(start_pos) == 0) {
|
||||||
|
stop("No starting position found in the input grid.")
|
||||||
|
}
|
||||||
|
if (is.vector(start_pos)) {
|
||||||
|
start_pos <- matrix(start_pos, nrow = 1)
|
||||||
|
}
|
||||||
|
directions <- list("^" = c(-1, 0), ">" = c(0, 1), "v" = c(1, 0), "<" = c(0, -1))
|
||||||
|
turns <- c("^" = ">", ">" = "v", "v" = "<", "<" = "^")
|
||||||
|
|
||||||
|
direction <- grid[start_pos[1, 1], start_pos[1, 2]]
|
||||||
|
visited <- list() # List of visited positions
|
||||||
|
|
||||||
|
# Initialize the position
|
||||||
|
x <- start_pos[1, 1]
|
||||||
|
print(x)
|
||||||
|
y <- start_pos[1, 2]
|
||||||
|
|
||||||
|
# Function to check the bounds of the grid
|
||||||
|
in_bounds <- function(x, y, grid) {
|
||||||
|
x > 0 && x <= nrow(grid) && y > 0 && y <= ncol(grid)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Mark the initial position as visited
|
||||||
|
visited[[paste(x, y, sep = ",")]] <- TRUE
|
||||||
|
|
||||||
|
# Simulate the movement
|
||||||
|
repeat {
|
||||||
|
# Calculate the next position in the current direction
|
||||||
|
dx <- directions[[direction]][1]
|
||||||
|
dy <- directions[[direction]][2]
|
||||||
|
nx <- x + dx
|
||||||
|
ny <- y + dy
|
||||||
|
|
||||||
|
# If out of bounds or obstacle ahead, turn right
|
||||||
|
if (!in_bounds(nx, ny, grid) || grid[nx, ny] == "#") {
|
||||||
|
direction <- turns[direction]
|
||||||
|
} else {
|
||||||
|
# Move forward
|
||||||
|
x <- nx
|
||||||
|
y <- ny
|
||||||
|
|
||||||
|
# Mark the new position as visited
|
||||||
|
visited[[paste(x, y, sep = ",")]] <- TRUE
|
||||||
|
|
||||||
|
# If out of bounds, stop the simulation
|
||||||
|
if (!in_bounds(x, y, grid)) break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Return the number of distinct positions visited
|
||||||
|
length(visited)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Input as a single string (provided)
|
||||||
|
input <- "....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#..^.....
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#..."
|
||||||
|
|
||||||
|
# Call the function
|
||||||
|
distinct_positions <- simulate_guard(input)
|
||||||
|
cat("Distinct positions visited:", distinct_positions, "\n")
|
||||||
Reference in New Issue
Block a user