Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Koffi Gantchou
AP
Commits
b4067ceb
Commit
b4067ceb
authored
5 months ago
by
Koffi Gantchou
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File tp 2 en cours
parent
28c7c9b3
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
recursivite.py
+345
-0
345 additions, 0 deletions
recursivite.py
with
345 additions
and
0 deletions
recursivite.py
0 → 100644
+
345
−
0
View file @
b4067ceb
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Noms :gantchou
# Prenoms :koffi
# Groupe :13
# Date :23/01/2025
"""
:mod: module `recursivite`
:author: FIL - Faculté des Sciences et Technologies - Univ. Lille
:link: <http://portail.fil.univ-lille1.fr>_
:date: Mars 2020
:dernière révision: janvier 2024
"""
from
ap_decorators
import
count
,
trace
# l'instruction suivante permet d'annoter des paramètres qui sont des functions.
from
collections.abc
import
Callable
def
taille_binaire
(
naturel
:
int
)
->
int
:
"""
Renvoie le nombre de chiffres dans l
'
écriture binaire de l
'
entier naturel `naturel`
Précondition :
naturel >= 0
Exemples :
$$$ taille_binaire(0)
1
$$$ taille_binaire(1)
1
$$$ taille_binaire(2)
2
$$$ taille_binaire(1023)
10
$$$ taille_binaire(1024)
11
$$$ from random import randrange
$$$ l = [randrange(1,2**100) for _ in range(100)]
$$$ all(taille_binaire(elt) == len(bin(elt))-2 for elt in l)
True
"""
res
=
1
while
naturel
>=
2
:
res
+=
1
naturel
//=
2
return
res
def
taille_binaire_recursive
(
naturel
:
int
)
->
int
:
"""
Renvoie le nombre de chiffres dans l
'
écriture binaire de l
'
entier naturel `naturel`
Précondition :
naturel >= 0
Exemples :
$$$ taille_binaire_recursive(0)
1
$$$ taille_binaire_recursive(1)
1
$$$ taille_binaire_recursive(2)
2
$$$ taille_binaire_recursive(1023)
10
$$$ taille_binaire_recursive(1024)
11
$$$ from random import randrange
$$$ l = [randrange(1,2**100) for _ in range(100)]
$$$ all(taille_binaire_recursive(elt) == len(bin(elt))-2 for elt in l)
True
"""
if
naturel
==
1
or
naturel
==
0
:
res
=
1
else
:
naturel
//=
2
res
=
taille_binaire_recursive
(
naturel
)
res
+=
1
return
res
def
poids_binaire
(
naturel
:
int
)
->
int
:
"""
Renvoie le nombre de chiffre 1 dans l
'
écriture binaire de l
'
entier naturel `naturel`
Précondition :
naturel >= 0
Exxemples :
$$$ poids_binaire(0)
0
$$$ poids_binaire(1)
1
$$$ poids_binaire(2)
1
$$$ poids_binaire(255)
8
$$$ from random import randrange
$$$ l = [randrange(1,2**100) for _ in range(100)]
$$$ all([poids_binaire(x)==bin(x).count(
'
1
'
) for x in l])
True
"""
res
=
naturel
%
2
while
naturel
>
0
:
naturel
//=
2
res
+=
naturel
%
2
return
res
def
poids_binaire_recursif
(
naturel
:
int
)
->
int
:
"""
Renvoie le nombre de chiffre 1 dans l
'
écriture binaire de l
'
entier naturel `naturel`
Précondition :
naturel >= 0
Exxemples :
$$$ poids_binaire_recursif(0)
0
$$$ poids_binaire_recursif(1)
1
$$$ poids_binaire_recursif(2)
1
$$$ poids_binaire_recursif(255)
8
$$$ from random import randrange
$$$ l = [randrange(1, 2**100) for _ in range(100)]
$$$ all([poids_binaire_recursif(x)==bin(x).count(
'
1
'
) for x in l])
True
"""
if
naturel
==
0
:
return
0
else
:
return
(
naturel
%
2
)
+
poids_binaire_recursif
(
naturel
//
2
)
def
puissance
(
x
:
int
|
float
,
n
:
int
)
->
int
|
float
:
"""
Calcule x élevé à la puissance n
Précondition :
n>=0
Exemples :
$$$ puissance(10, 0)
1
$$$ puissance(10, 1)
10
$$$ puissance(2, 10)
1024
"""
if
n
==
0
:
return
1
elif
n
%
2
==
0
:
half
=
puissance
(
x
,
n
//
2
)
return
half
*
half
else
:
return
x
*
puissance
(
x
,
n
-
1
)
def
puissance_v2
(
x
:
int
|
float
,
n
:
int
)
->
int
|
float
:
"""
calcule x élevé à la puissance n
Précondition : n>=0
Exemples :
$$$ puissance_v2(10,0)
1
$$$ puissance_v2(10,1)
10
$$$ puissance_v2(2,10)
1024
"""
if
n
==
0
:
return
1
elif
n
==
1
:
return
x
else
:
return
fois
(
x
,
puissance_v2
(
x
,
n
-
1
))
@count
def
fois
(
x
:
int
|
float
,
y
:
int
|
float
)
->
int
|
float
:
"""
renvoie le produit de x par y
Précondition : les mêmes que l
'
opérateur *
Exemples :
$$$ fois(8, 7)
56
"""
return
x
*
y
# le parametre de comptage est la fonction puissance
# le resultat est une liste d'entier comptant le nombre de solutions fournis par la puissance
def
comptage
(
puissance
:
Callable
[[
int
|
float
,
int
],
int
|
float
])
->
list
[
int
]:
"""
Renvoie une liste de longueur 100 contenant le nombre de multiplications
effectuées par la fonction ``puissance`` passée en paramètre
Précondition :
la fonction doit être implantée en utilisant la fonction ``fois``
"""
res
=
[]
for
i
in
range
(
100
):
fois
.
counter
=
0
_
=
puissance
(
2
,
i
)
res
.
append
(
fois
.
counter
)
return
res
#[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
#on obtient ce resultat car on a pas fait appel a la fonction fois decoré par "count"
# [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98]
# une expression en fonction de n est n-1
#
#@trace
def
puissance_calbuth
(
x
:
int
|
float
,
n
:
int
)
->
int
|
float
:
"""
calcule x élevé à la puissance n
Précondition :
n>=0
Exemples :
$$$ puissance_calbuth(10,0)
1
$$$ puissance_calbuth(10,1)
10
$$$ puissance_calbuth(2,10)
1024
"""
if
n
==
0
:
return
1
if
n
==
1
:
return
x
else
:
k
=
n
//
2
return
puissance_calbuth
(
x
,
k
)
*
puissance_calbuth
(
x
,
n
-
k
)
def
puissance_calbuth_v2
(
x
:
int
|
float
,
n
:
int
)
->
int
|
float
:
"""
calcule x élevé à la puissance n
Précondition :
n>=0
Exemples :
$$$ puissance_calbuth_v2(10,0)
1
$$$ puissance_calbuth_v2(10,1)
10
$$$ puissance_calbuth_v2(2,10)
1024
"""
if
n
==
0
:
return
1
if
n
==
1
:
return
x
else
:
k
=
n
//
2
return
fois
(
puissance_calbuth_v2
(
x
,
k
)
,
puissance_calbuth_v2
(
x
,
n
-
k
))
# oui ça permet de diminuer la complexite de multiplications
# ça permet de ne pas gerer les cas ou la puissance est paire ou impaire
def
puissance_calbuth_v2_amelioree
(
x
:
int
|
float
,
n
:
int
)
->
int
|
float
:
"""
calcule x élevé à la puissance n
Précondition :
n>=0
Exemples :
$$$ puissance_calbuth_v2_amelioree(10,0)
1
$$$ puissance_calbuth_v2_amelioree(10,1)
10
$$$ puissance_calbuth_v2_amelioree(2,10)
1024
"""
def
puissance_erronee
(
x
:
int
|
float
,
n
:
int
)
->
int
|
float
:
"""
aurait dû calculer x élevé à la puissance n
Précondition :
n >= 0
Exemples :
$$$ puissance_erronee(10, 0)
1
$$$ puissance_erronee(10, 1)
10
$$$ #$$$ puissance_erronee(2, 10)
$$$ #1024
"""
if
n
==
0
:
return
1
elif
n
==
1
:
return
x
else
:
r
=
n
%
2
q
=
n
//
2
return
puissance_erronee
(
x
,
r
)
*
puissance_erronee
(
puissance_erronee
(
x
,
q
),
2
)
def
puissance_reparee
(
x
:
int
|
float
,
n
:
int
)
->
int
|
float
:
"""
calcule x élevé à la puissance n
Précondition :
n>=0
Exemples :
$$$ puissance_reparee(10,0)
1
$$$ puissance_reparee(10,1)
10
$$$ puissance_reparee(2,10)
1024
"""
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment