Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| teaching:progappchim:suite_de_fibonacci-3 [2013/10/24 10:11] – 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 : | Voici une proposition complète : | ||
| - | <sxh python; title : fibonacci05_fonction.py> | + | <code python fibonacci05_fonction.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| Ligne 36: | Ligne 36: | ||
| """ | """ | ||
| a, b = 0, 1 | a, b = 0, 1 | ||
| - | if n==0: | + | if n == 0: |
| return a | return a | ||
| - | elif n==1: | + | elif n == 1: |
| return b | return b | ||
| for i in range(1,n): | for i in range(1,n): | ||
| Ligne 45: | Ligne 45: | ||
| if __name__ == ' | if __name__ == ' | ||
| - | i=input(" | + | i = int(input(" |
| - | print (" | + | print(" |
| - | print fibonacci_item(i) | + | print(fibonacci_item(i)) |
| - | print (' | + | print(' |
| for j in range(10): | for j in range(10): | ||
| - | print j, | + | print(j, |
| - | </sxh> | + | </code> |
| On peut compléter les fonctionnalités par une fonction **fibonacci_list(n)** qui génère et renvoie la liste des éléments de la suite de Fibonacci jusqu' | On peut compléter les fonctionnalités par une fonction **fibonacci_list(n)** qui génère et renvoie la liste des éléments de la suite de Fibonacci jusqu' | ||
| Voici ce que cela donne : | Voici ce que cela donne : | ||
| - | <sxh python; title : fibonacci06_fonctions.py> | + | <code python fibonacci06_fonctions.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| Ligne 68: | Ligne 68: | ||
| """ | """ | ||
| a, b = 0, 1 | a, b = 0, 1 | ||
| - | if n==0: | + | if n == 0: |
| - | return | + | return |
| - | for i in range(n): | + | elif n == 1: |
| + | return b | ||
| + | for i in range(1,n): | ||
| a, b = b, a + b | a, b = b, a + b | ||
| return b | return b | ||
| def fibonacci_list(n): | def fibonacci_list(n): | ||
| - | """ | + | |
| - | Renvoie la liste des éléments de la suite de Fibonacci jusqu' | + | Renvoie la liste des éléments de la suite de Fibonacci jusqu' |
| - | """ | + | """ |
| - | a, b,ans = 0,1,[0] | + | a, b, ans = 0, 1, [0,1] |
| - | if n==0: | + | if n == 0: |
| - | return | + | return |
| - | for i in range(n): | + | for i in range(1,n): |
| a, b = b, a + b | a, b = b, a + b | ||
| ans.append(b) | ans.append(b) | ||
| Ligne 98: | Ligne 100: | ||
| if __name__ == ' | if __name__ == ' | ||
| - | i=input(" | + | i = int(input(" |
| - | print (" | + | print(" |
| - | print fibonacci_item(i) | + | print(fibonacci_item(i)) |
| - | print (' | + | print(' |
| for j in range(10): | for j in range(10): | ||
| - | print j, | + | print(j, |
| - | print ('Avec fibonacci_item_from_list : ') | + | print(' |
| for j in range(10): | for j in range(10): | ||
| - | print j, | + | print(j, |
| - | print (" | + | print(" |
| - | print fibonacci_list(i) | + | print(fibonacci_list(i)) |
| - | print (' | + | print(' |
| for j in range(10): | for j in range(10): | ||
| - | print j, | + | print(j, |
| - | print "Avec fibonacci_list_from_items" | + | print("Avec fibonacci_list_from_items" |
| for j in range(10): | for j in range(10): | ||
| - | print j, | + | print(j, |
| - | </sxh> | + | </code> |
| Des fonctions qui appellent d' | Des fonctions qui appellent d' | ||
| - | <sxh python; title : fibonacci07_fonction_recursive.py> | + | <code python fibonacci07_fonction_recursive.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| Ligne 136: | Ligne 138: | ||
| """ | """ | ||
| ... (?) | ... (?) | ||
| - | return fibonacci_item_recursive(n-1)+fibonacci_item_recursive(n-2) | + | return fibonacci_item_recursive(n-1) + fibonacci_item_recursive(n-2) |
| if __name__ == ' | if __name__ == ' | ||
| ... | ... | ||
| - | </sxh> | + | </code> |
| [[suite_de_fibonacci-4|Pour la suite, cliquez ici !]] | [[suite_de_fibonacci-4|Pour la suite, cliquez ici !]] | ||