Julia
Recherche dans un ensemble par position
Note: avec Julia la position du premier élément d’une collection est 1
Première forme:
function rechercheParPosition_1(ensemble,position) index = 1 existe = false Initialiser(ensemble) while Enumeration(ensemble) && index < position && position >= 1 index = index + 1 Element_Suivant(ensemble) end if Enumeration(ensemble) && (position > 0) existe = true end return existe end
Deuxième forme: parcours sur ensemble itérable
function rechercheParPosition_2(ensemble,position) existe = false index = 1 for _ in Elements(ensemble) if index == position existe = true break else index = index + 1 end end return existe end
Tests
La fonction booléen utilisée dans le test ci-dessous
function booléen(valeur) if valeur Afficher("VRAI") else Afficher("FAUX") end end
ensemble = File(10,20,30)
position = 0
println("Test recherche à la position ", position)
booléen(rechercheParPosition_1(ensemble,position))
booléen(rechercheParPosition_2(ensemble,position))
position = 1
println("Test recherche à la position ", position)
booléen(rechercheParPosition_1(ensemble,position))
booléen(rechercheParPosition_2(ensemble,position))
position = 3
println("Test recherche à la position ", position)
booléen(rechercheParPosition_1(ensemble,position))
booléen(rechercheParPosition_2(ensemble,position))
position = 4
println("Test recherche à la position ", position)
booléen(rechercheParPosition_1(ensemble,position))
booléen(rechercheParPosition_2(ensemble,position))
Swift
Recherche dans un ensemble par position
Note: avec Swift la position du premier élément d’une collection est 0
Première forme:
func rechercheParPosition_1 <_Element>(_ ensemble: Ensemble<_Element>
,_ position: Int)
-> Bool {
var index = 1
var existe = false
Initialiser(ensemble)
while Enumeration(ensemble) && index < position && position >= 1{
index = index + 1
Element_Suivant(ensemble)
}
if Enumeration(ensemble) && (position == index) { existe = true }
return existe
}
Deuxième forme: parcours sur ensemble itérable
func rechercheParPosition_2 <_Element>(_ ensemble: Ensemble<_Element>
,_ position: Int)
-> Bool {
var existe = false
var index = 0
for _ in ensemble {
if index == position {
existe = true
break
} else {
index = index + 1
}
}
return existe
}
Tests
func Test_RechercheParPosition() {
var ensemble = File(10,20,39) // Création d'un ensemble
var position = 4
Afficher(ensemble,rechercheParPosition_1(ensemble,position),position)
position = 1
Afficher(ensemble,rechercheParPosition_1(ensemble,position),position)
position = 0
Afficher(ensemble,rechercheParPosition_2(ensemble,position),position)
position = 3
Afficher(texte,rechercheParPosition_2(texte,position),position)
}
copyright A rchitectures A pplicatives A vancées A3-Soft