Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider bfc0391c rédigé par David CHOCHO's avatar David CHOCHO
Parcourir les fichiers

Update 18 files

- /BUT2/Dev_efficace/src/main/kotlin/Main-Eculclide.txt.kt
- /BUT2/Dev_efficace/src/main/kotlin/Main-Monegasque.txt.kt
- /BUT2/Dev_efficace/src/main/kotlin/Main-TriParSelection.kt
- /BUT2/Dev_efficace/src/main/kotlin/Main.kt
- /BUT2/Dev_efficace/src/main/kotlin/rendu.md
- /BUT2/Dev_efficace/.idea/codeStyles/Project.xml
- /BUT2/Dev_efficace/.idea/codeStyles/codeStyleConfig.xml
- /BUT2/Dev_efficace/.idea/inspectionProfiles/Project_Default.xml
- /BUT2/Dev_efficace/.idea/libraries/KotlinJavaRuntime.xml
- /BUT2/Dev_efficace/.idea/.gitignore
- /BUT2/Dev_efficace/.idea/kotlinc.xml
- /BUT2/Dev_efficace/.idea/misc.xml
- /BUT2/Dev_efficace/.idea/modules.xml
- /BUT2/Dev_efficace/.gitignore
- /BUT2/Dev_efficace/Dev_efficace.iml
- /BUT2/Dev_efficace/Drapeau_hollandais.kt
- /BUT2/Dev_efficace/Tri_par_selection.kt
- /BUT2/Dev_efficace/rendu.md
parent 95c4f39d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 186 ajouts et 324 suppressions
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ReplaceUntilWithRangeUntil" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.8" />
<option name="languageVersion" value="1.8" />
</component>
</project>
\ No newline at end of file
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.21" />
<CLASSES>
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.21/kotlin-stdlib-jdk8-1.8.21.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.8.21/kotlin-stdlib-1.8.21.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.21/kotlin-stdlib-common-1.8.21.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.21/kotlin-stdlib-jdk7-1.8.21.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.21/kotlin-stdlib-jdk8-1.8.21-javadoc.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.8.21/kotlin-stdlib-1.8.21-javadoc.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.21/kotlin-stdlib-common-1.8.21-javadoc.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.21/kotlin-stdlib-jdk7-1.8.21-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.21/kotlin-stdlib-jdk8-1.8.21-sources.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.8.21/kotlin-stdlib-1.8.21-sources.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.21/kotlin-stdlib-common-1.8.21-sources.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$USER_HOME$/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.21/kotlin-stdlib-jdk7-1.8.21-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Dev_efficace.iml" filepath="$PROJECT_DIR$/Dev_efficace.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>
\ No newline at end of file
fun DrapeauHollandais(couleurs : Array<String>):Array<String>{
var r = 0
var i = 0
var b = couleurs.size-1
while(i<=b){
if (couleurs[i] == "W"){
i += 1
}else if (couleurs[i] == "R"){
val temp = couleurs[i]
couleurs[i] = couleurs[r]
couleurs[r] = temp
r += 1
i += 1
}else{
val temp = couleurs[i]
couleurs[i] = couleurs[b]
couleurs[b] = temp
b -= 1
}
}
return couleurs
}
fun main() {
var liste = arrayOf ("R","R","R","R","R","R","R","R","W","W","W","W","W","W","W","W","B","B","B","B","B","B","B","B")
liste.shuffle()
print("Le drapeau en entrée est :")
for (s in liste) {
print(" $s")
}
println()
var drapeau = DrapeauHollandais(liste)
println("Le drapeau en sortie est :")
for (e in drapeau.iterator()) print (e.toString()+" ")
}
\ No newline at end of file
import java.util.stream.IntStream.range
import kotlin.random.Random
fun tri_select (liste : MutableList<Int>):MutableList<Int>{
val t = liste.size
var i = 0
while (i < t) {
var min = i
var j = i+1
while (j < t) {
if (liste[j] < liste[min]) {
min = j
}
j+=1
}
if (min != i) {
val temp = liste[i]
liste[i] = liste[min]
liste[min] = temp
}
i += 1
}
return liste
}
fun main(args: Array<String>){
val liste_a_trier = mutableListOf<Int>()
for (k in 0..1000){
liste_a_trier.add(k)
}
liste_a_trier.shuffle()
println("Liste non triée : "+liste_a_trier)
val selection = tri_select(liste_a_trier)
println("Liste triée : "+selection)
}
\ No newline at end of file
# CHOCHO David, Groupe 2
### Compte rendu exercices maison (tri par insertion et drapeau hollandais)
# Tri par insertion
Le tri par insertion consiste à trier une liste d'élements comparables.
Il en existe deux versions :
- Une version **en place** c'est à dire qui effectue le tri sur la liste actuelle en la modifiant
- Une version **pas en place** qui crée une seconde liste permettant de garder l'originale intact
Ici nous étudierons le tri par insertion en place d'une liste de nombres entiers.
## Principe
Le principe de ce tri est de rechercher à chaque fois l'élement le plus petit de la partie non triée de la liste puis de le deplacer au bout de la partie triée de celle-ci.
Ensuite, on réitère l'opération jusqu'à arriver au bout de la liste, une fois cela fais, s'il n'y a pas eu d'erreurs, la liste est complètement trié.
## Boucle de tri
### précondition
une liste L de n nombres entiers
### postcondition
la liste L trié dans l'ordre croissant
### invariant
pour tout i,mi appartenant à 0 .. L.size-1 | mi<=i && L[mi]<=L[i]
### initialisation
N = L.size
i = 0
### progression
Boucle de recherche du plus petit element non trié -> mi;
echanger dans L les élements aux index i et mi;
i avance dans le tableau;
### condition d'arret
i = N-1 #car on souhaite s'arreter à l'avant dernier element
### terminaison
N-i
## Boucle de recherche du plus petit element non trié
### invariant
L[mi] < L[k]
### initialistion
mi = i
j = mi
### progression
comparer dans L les élements aux index mi et j
si l'element à l'index j est le plus petit alors j devient mi
j avance dans le tableau
### condition d'arret
j = N
### terminaison
N-j
# Drapeau Hollandais
L'algorithme du drapeau hollandais(également connu sous le nom de tri des rois couleurs) consiste à prendre une liste d'élements pouvant prendre 3 valeurs distinctes (0,1,2), (R,G,B), ... et de la trier de manière à avoir un patterne du genre "111122223333".
***PS : Une version avec le drapeau Monegasque existe également.***
## Principe
Le principe de ce tri va être de mettre à jour les trois parties du tableau simultanement au fur et à mesure du parcours de celui-ci.
## Boucle de tri
### précondition
un tableau composé de n élements répartissables en exactement trois catégories, dans notre cas ("R","W","B").
### postcondition
le tableau T trié de manière à avoir tout les "R" au début du tableau, les "W" au milieu et les "B" à la fin.
### invariant
pour i,r,b appartenant
à 0 .. T.size |
si i<=r alors T[i] = R,
si i>b alors T[i] = B
### initialisation
t = T.size
r = 0
i = 0
b = t- 1
### progression
Si T[i] = "R" alors echanger T[i] avec T[r] puis incrémenter r
Sinon si T[i] = "B" alors echanger T[i] avec T[b] puis décrementer b
Sinon ne rien faire
Ensuite dans tout les cas incrémenter i
### condition d'arret
i = t
### terminaison
t-i
# Conclusion
Ce pré travail algorithmique permet de faire apparaitre clairement tout les composants de nos boucles de manière à avoir quasiment notre code entier fait sans meme avoir à écrire une ligne de code ce qui peut etre utile dans tes programmes ou boucles de grande envergure ou des algorithmes complexes ou abstraits.
/*--------------------------
Implementation in Kotlin of the Euclidian Division
December 7th, 2022
JCA, IUT, Nantes Université
--------------------------*/
fun euclideNat(a:Int, b: Int): Pair<Int,Int> {
// divides A per B, returns <quotient, rest>
var q:Int
var r:Int
var previousR : Int // Varaint Value: to deal with *Termination*
/*---precondition---*/
require((a>= 0) && (b > 0)) {"precondition not respected"}
/*---init---*/
q = 0
r = a
previousR = r
println("q= $q, r= $r, a= $a, b= $b")
while (r >= b){
/*invariant*/
assert((a==q*b+r) && (r>=b)){"invariant not true"}
/*---progress---*/
previousR = r
r -= b
q += 1
/*--- termination---*/
assert(previousR > r) {
"terminaision terms does not decrease"
}/* r should decrease at each iteration step */
}
/*---postcondition---*/
assert(a==q*b+r) {"postcondition not true"}
return Pair(q,r)
}
fun main(args: Array<String>) {
println("Euclide World!")
// Try adding program arguments via Run/Debug configuration.
// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
val a = 57
val b = 11
val re :Pair<Int, Int> = euclideNat(a,b) /* returns a pair with Q, R */
println("Résultat de la division de $a par $b est : ${re.first} et reste ${re.second}")
}
/*------------------------------------------
Correct construction of Monaco's flag
with systematic loop construction :
(invariant, stop condition, initialisation, progress, termination variant)
JCA - IUT - Nantes Université - Décembre 2022
------------------------------------------*/
fun makeMonacoFlag (flag : Array<String>):Array<String> {
/* rearrange Flag to make it as Monaco's Flag Red|White */
/*---preconditionn--- : Flag should contain either White or Red---*/
require(flag.isNotEmpty()) {"Input Flag should not be empty"}/* at least 1 element*/
require(flag.all { it == "W" || it == "R"}){"Flag data should contain only W or R"}
/*---init---*/
var posR = 0 /* it is endRed+1 */
val nbElt= flag.size /* it is startW-1*/
var posW = nbElt-1
var previousExpr : Int /* to check the decreasing variant*/
previousExpr = posW-posR
assert(previousExpr in -1..nbElt) {"Variant out of bounds"}
while(posR<=posW) {
/*---loop invariant ---*/
assert(
(flag.sliceArray(0..posR - 1)
.all { it == "R" }) /* from begining to endR-1 : there are red from startW+1 to end there are White*/
&& (flag.sliceArray(posW + 1..nbElt-1).all { it == "W" })
) {"Invariant violated"}
previousExpr = posW-posR
/*---progress---*/
if (flag[posW] == "W") {
posW-=1
} else { /* flag[posW] = "R" */
if (flag[posR] == "W") {
var tmp: String
tmp = flag[posW]
flag[posW] = flag[posR]
flag[posR] = tmp
posW -= 1
posR += 1
} else {
/* flag[posR]= "R"*/
posR+=1
}
}
/*---termination, VARIANT should decrease---*/
println("previous value = $previousExpr")
assert(previousExpr > posW-posR) {"Termination not ensured"}
}
return flag
}
fun main(args: Array<String>) {
println("Monaco's Flag! come on!")
//var flagM = Array<String>(12){"W";"R";"R";"W";"R";"W";"R";"W";"W"}
var flagM = arrayOf ("W","R","W","R","W","R","W","W","R","R","W","R")
println("Input Flag is, f")
for (s in flagM) {
print(" $s")
}
println()
/*
for (elt in flagM.iterator()) print (elt.toString()+" ")
println()
for (i in 0..flagM.size-1) print(flagM[i].toString()+" ")
*/
flagM = makeMonacoFlag(flagM)
println("Output Flag is, f")
for (elt in flagM.iterator()) print (elt.toString()+" ")
}
/*--------------------------
Implementation in Kotlin of the Euclidian Division
December 7th, 2022
JCA, IUT, Nantes Université
--------------------------*/
fun aeuclideNat(a:Int, b: Int): Pair<Int,Int> {
// divides A per B, returns <quotient, rest>
var q:Int
var r:Int
var previousR : Int // Varaint Value: to deal with *Termination*
/*---precondition---*/
require((a>= 0) && (b > 0)) {"precondition not respected"}
/*---init---*/
q = 0
r = a
previousR = r
println("q= $q, r= $r, a= $a, b= $b")
while (r >= b){
/*invariant*/
assert((a==q*b+r) && (r>=b)){"invariant not true"}
/*---progress---*/
previousR = r
r -= b
q += 1
/*--- termination---*/
assert(previousR > r) {
"terminaision terms does not decrease"
}/* r should decrease at each iteration step */
}
/*---postcondition---*/
assert(a==q*b+r) {"postcondition not true"}
return Pair(q,r)
}
fun main(args: Array<String>) {
println("Euclide World!")
// Try adding program arguments via Run/Debug configuration.
// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
val a = 57
val b = 11
val re :Pair<Int, Int> = euclideNat(a,b) /* returns a pair with Q, R */
println("Résultat de la division de $a par $b est : ${re.first} et reste ${re.second}")
}
fun main(args: Array<String>) {
println("Hello World!")
// Try adding program arguments via Run/Debug configuration.
// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
println("Program arguments: ${args.joinToString()}")
}
\ No newline at end of file
# Boucle de tri
## invariant :
pour tout i appartenant à T[0]..T[i] | T[mi]=>T[i]
## initialisation :
T = liste
i = 0
N = T.size
## progression :
Boucle de recherche du plus petit element non trié -> mi
echanger dans T i et mi
i avance dans le tableau
## condition d'arret :
i = N
## terminaison :
N-i
# Boucle de recherche du plus petit element non trié
## invariant
T[mi] < T[k]
## initialistion :
mi = i
k = mi
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter