commit 8bc7b2bf58c015c98c8abb93736ff7fe2211224f Author: Félix MARQUET Date: Wed Dec 18 10:13:48 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7224328 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.idea/ \ No newline at end of file diff --git a/TD/.Rhistory b/TD/.Rhistory new file mode 100644 index 0000000..115b06b --- /dev/null +++ b/TD/.Rhistory @@ -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)]) diff --git a/TD/.Rproj.user/F9ECB55E/pcs/files-pane.pper b/TD/.Rproj.user/F9ECB55E/pcs/files-pane.pper new file mode 100644 index 0000000..7d5ef91 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/pcs/files-pane.pper @@ -0,0 +1,9 @@ +{ + "sortOrder": [ + { + "columnIndex": 2, + "ascending": true + } + ], + "path": "C:/Users/BreizhHardware/Nextcloud/Programation/R/TD" +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/pcs/source-pane.pper b/TD/.Rproj.user/F9ECB55E/pcs/source-pane.pper new file mode 100644 index 0000000..ddca97d --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/pcs/source-pane.pper @@ -0,0 +1,3 @@ +{ + "activeTab": 2 +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/pcs/windowlayoutstate.pper b/TD/.Rproj.user/F9ECB55E/pcs/windowlayoutstate.pper new file mode 100644 index 0000000..de81620 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/pcs/windowlayoutstate.pper @@ -0,0 +1,14 @@ +{ + "left": { + "splitterpos": 393, + "topwindowstate": "NORMAL", + "panelheight": 945, + "windowheight": 983 + }, + "right": { + "splitterpos": 589, + "topwindowstate": "NORMAL", + "panelheight": 945, + "windowheight": 983 + } +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/pcs/workbench-pane.pper b/TD/.Rproj.user/F9ECB55E/pcs/workbench-pane.pper new file mode 100644 index 0000000..75e70e9 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/pcs/workbench-pane.pper @@ -0,0 +1,5 @@ +{ + "TabSet1": 0, + "TabSet2": 0, + "TabZoom": {} +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/rmd-outputs b/TD/.Rproj.user/F9ECB55E/rmd-outputs new file mode 100644 index 0000000..3f2ff2d --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/rmd-outputs @@ -0,0 +1,5 @@ + + + + + diff --git a/TD/.Rproj.user/F9ECB55E/saved_source_markers b/TD/.Rproj.user/F9ECB55E/saved_source_markers new file mode 100644 index 0000000..2b1bef1 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/saved_source_markers @@ -0,0 +1 @@ +{"active_set":"","sets":[]} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA b/TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA new file mode 100644 index 0000000..eef4266 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA @@ -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": [] +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA-contents b/TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA-contents new file mode 100644 index 0000000..392f752 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/per/t/B62E70DA-contents @@ -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 diff --git a/TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4 b/TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4 new file mode 100644 index 0000000..7ba97ca --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4 @@ -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": [] +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4-contents b/TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4-contents new file mode 100644 index 0000000..494704d --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/per/t/C229FEB4-contents @@ -0,0 +1,4 @@ +vector1 <- c(1, 2, 3) +vector2 <- c(4, 5, 6) + +vector3 <- c(vector1[1:2], vector2, vector1[3:length(vector1)]) \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E b/TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E new file mode 100644 index 0000000..16dc965 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E @@ -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": [] +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E-contents b/TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E-contents new file mode 100644 index 0000000..4495958 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/per/t/DA0B538E-contents @@ -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) \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/prop/1489AED4 b/TD/.Rproj.user/F9ECB55E/sources/prop/1489AED4 new file mode 100644 index 0000000..421141e --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/prop/1489AED4 @@ -0,0 +1,6 @@ +{ + "source_window_id": "", + "Source": "Source", + "cursorPosition": "0,0", + "scrollLine": "0" +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/prop/57E49702 b/TD/.Rproj.user/F9ECB55E/sources/prop/57E49702 new file mode 100644 index 0000000..d136f0a --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/prop/57E49702 @@ -0,0 +1,6 @@ +{ + "source_window_id": "", + "Source": "Source", + "cursorPosition": "3,63", + "scrollLine": "0" +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/prop/74FBB217 b/TD/.Rproj.user/F9ECB55E/sources/prop/74FBB217 new file mode 100644 index 0000000..895734f --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/prop/74FBB217 @@ -0,0 +1,6 @@ +{ + "source_window_id": "", + "Source": "Source", + "cursorPosition": "19,8", + "scrollLine": "0" +} \ No newline at end of file diff --git a/TD/.Rproj.user/F9ECB55E/sources/prop/INDEX b/TD/.Rproj.user/F9ECB55E/sources/prop/INDEX new file mode 100644 index 0000000..0603ee1 --- /dev/null +++ b/TD/.Rproj.user/F9ECB55E/sources/prop/INDEX @@ -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" diff --git a/TD/.Rproj.user/shared/notebooks/patch-chunk-names b/TD/.Rproj.user/shared/notebooks/patch-chunk-names new file mode 100644 index 0000000..e69de29 diff --git a/TD/Exo1.R b/TD/Exo1.R new file mode 100644 index 0000000..4495958 --- /dev/null +++ b/TD/Exo1.R @@ -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) \ No newline at end of file diff --git a/TD/Exo2.R b/TD/Exo2.R new file mode 100644 index 0000000..494704d --- /dev/null +++ b/TD/Exo2.R @@ -0,0 +1,4 @@ +vector1 <- c(1, 2, 3) +vector2 <- c(4, 5, 6) + +vector3 <- c(vector1[1:2], vector2, vector1[3:length(vector1)]) \ No newline at end of file diff --git a/TD/Exo3.R b/TD/Exo3.R new file mode 100644 index 0000000..533e249 --- /dev/null +++ b/TD/Exo3.R @@ -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) \ No newline at end of file diff --git a/TD/Exo4.R b/TD/Exo4.R new file mode 100644 index 0000000..2df4ce3 --- /dev/null +++ b/TD/Exo4.R @@ -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) \ No newline at end of file diff --git a/TD/Exo5.R b/TD/Exo5.R new file mode 100644 index 0000000..afaabc5 --- /dev/null +++ b/TD/Exo5.R @@ -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") \ No newline at end of file diff --git a/TD/Exo6.R b/TD/Exo6.R new file mode 100644 index 0000000..e734fa3 --- /dev/null +++ b/TD/Exo6.R @@ -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 \ No newline at end of file diff --git a/TD/Rplots.pdf b/TD/Rplots.pdf new file mode 100644 index 0000000..e91b9a1 Binary files /dev/null and b/TD/Rplots.pdf differ diff --git a/TD/TD.Rproj b/TD/TD.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/TD/TD.Rproj @@ -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 diff --git a/TD1/Exercice39.R b/TD1/Exercice39.R new file mode 100644 index 0000000..d301fa0 --- /dev/null +++ b/TD1/Exercice39.R @@ -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)) diff --git a/TD1/Exercice51.R b/TD1/Exercice51.R new file mode 100644 index 0000000..32ef563 --- /dev/null +++ b/TD1/Exercice51.R @@ -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) diff --git a/TD1/Exo1.R b/TD1/Exo1.R new file mode 100644 index 0000000..8f851d3 --- /dev/null +++ b/TD1/Exo1.R @@ -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 \ No newline at end of file diff --git a/TD1/advent_of_code_06_12_2024.R b/TD1/advent_of_code_06_12_2024.R new file mode 100644 index 0000000..9266927 --- /dev/null +++ b/TD1/advent_of_code_06_12_2024.R @@ -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") \ No newline at end of file