Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| teaching:progappchim:suite_de_fibonacci-3 [2013/10/24 08:43] – créée villersd | teaching:progappchim:suite_de_fibonacci-3 [2017/02/24 08:52] (Version actuelle) – villersd | ||
|---|---|---|---|
| Ligne 3: | Ligne 3: | ||
| Voici la structure que doit avoir un programme pour lequel le calcul de l' | Voici la structure que doit avoir un programme pour lequel le calcul de l' | ||
| - | <sxh python; title : fibonacci05_fonction.py> | + | <code python fibonacci05_fonction.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| Ligne 19: | Ligne 19: | ||
| # le programme " | # le programme " | ||
| | | ||
| - | </sxh> | + | </code> |
| - | Le rôle de la structure conditionnelle **< | + | Le rôle de la structure conditionnelle **< |
| + | |||
| + | Voici une proposition complète : | ||
| + | <code python fibonacci05_fonction.py> | ||
| + | #! / | ||
| + | # -*- coding: utf-8 -*- | ||
| + | """ | ||
| + | Calculs des premiers éléments de la suite de Fibonacci. | ||
| + | Référence : http:// | ||
| + | """ | ||
| + | def fibonacci_item(n): | ||
| + | """ | ||
| + | Renvoie l' | ||
| + | """ | ||
| + | a, b = 0, 1 | ||
| + | if n == 0: | ||
| + | return a | ||
| + | elif n == 1: | ||
| + | return b | ||
| + | for i in range(1, | ||
| + | a, b = b, a + b | ||
| + | return b | ||
| + | |||
| + | if __name__ == ' | ||
| + | i = int(input(" | ||
| + | print(" | ||
| + | print(fibonacci_item(i)) | ||
| + | print(' | ||
| + | for j in range(10): | ||
| + | print(j, | ||
| + | </code> | ||
| + | |||
| + | On peut compléter les fonctionnalités par une fonction **fibonacci_list(n)** | ||
| + | |||
| + | Voici ce que cela donne : | ||
| + | <code python fibonacci06_fonctions.py> | ||
| + | #! / | ||
| + | # -*- coding: utf-8 -*- | ||
| + | """ | ||
| + | Calculs des premiers éléments de la suite de Fibonacci. | ||
| + | Référence : http:// | ||
| + | """ | ||
| + | def fibonacci_item(n): | ||
| + | """ | ||
| + | Renvoie l' | ||
| + | """ | ||
| + | a, b = 0, 1 | ||
| + | if n == 0: | ||
| + | return a | ||
| + | elif n == 1: | ||
| + | return b | ||
| + | for i in range(1, | ||
| + | a, b = b, a + b | ||
| + | return b | ||
| + | |||
| + | def fibonacci_list(n): | ||
| + | """ | ||
| + | Renvoie la liste des éléments de la suite de Fibonacci jusqu' | ||
| + | """ | ||
| + | a, b, ans = 0, 1, [0,1] | ||
| + | if n == 0: | ||
| + | return [0] | ||
| + | for i in range(1, | ||
| + | a, b = b, a + b | ||
| + | ans.append(b) | ||
| + | return ans | ||
| + | |||
| + | def fibonacci_list_from_items(n): | ||
| + | # construit la liste à partir de la fonction donnant un élément | ||
| + | ans = [] | ||
| + | for i in range(n+1): | ||
| + | ans.append(fibonacci_item(i)) | ||
| + | return ans | ||
| + | |||
| + | def fibonacci_item_from_list(n): | ||
| + | # renvoie l' | ||
| + | return fibonacci_list(n)[n] | ||
| + | |||
| + | if __name__ == ' | ||
| + | i = int(input(" | ||
| + | print(" | ||
| + | print(fibonacci_item(i)) | ||
| + | print(' | ||
| + | for j in range(10): | ||
| + | print(j, | ||
| + | |||
| + | print(' | ||
| + | for j in range(10): | ||
| + | print(j, | ||
| + | |||
| + | |||
| + | print(" | ||
| + | print(fibonacci_list(i)) | ||
| + | print(' | ||
| + | for j in range(10): | ||
| + | print(j, | ||
| + | |||
| + | print(" | ||
| + | for j in range(10): | ||
| + | print(j, | ||
| + | </code> | ||
| + | |||
| + | Des fonctions | ||
| + | |||
| + | <code python fibonacci07_fonction_recursive.py> | ||
| + | #! / | ||
| + | # -*- coding: utf-8 -*- | ||
| + | """ | ||
| + | Calculs des premiers éléments de la suite de Fibonacci. | ||
| + | Référence : http:// | ||
| + | Application de la définition | ||
| + | """ | ||
| + | def fibonacci_item_recursive(n): | ||
| + | """ | ||
| + | Renvoie l' | ||
| + | """ | ||
| + | ... (?) | ||
| + | return fibonacci_item_recursive(n-1) + fibonacci_item_recursive(n-2) | ||
| + | |||
| + | if __name__ == ' | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | [[suite_de_fibonacci-4|Pour la suite, cliquez ici !]] | ||