Répétition et Binaire

.

Exercice N°1 :

Enoncé Exemple :

Écrire une fonction python appelée nb_repetitions qui prend en paramètres un
élément elt et une liste tab et renvoie le nombre de fois où l’élément apparaît dans la
liste.

Exemples :
>>> nb_repetitions(5,[2,5,3,5,6,9,5])
3
>>> nb_repetitions('A',[ 'B', 'A', 'B', 'A', 'R'])
2
>>> nb_repetitions(12,[1, '! ',7,21,36,44])
0

REPONSE

L=[2,5,3,5,6,9,5]

J=[ 'B', 'A', 'B', 'A', 'R']

K=[1, '! ',7,21,36,44]

def nb_repetitions(a,L):

     k=0

     for elt in L:

        if elt==a:

            k=k+1

     return k,L

print(nb_repetitions(5,L))

(3, [2, 5, 3, 5, 6, 9, 5])

print(nb_repetitions('A',J))

(2, ['B', 'A', 'B', 'A', 'R'])

print(nb_repetitions(12,K))

(0, [1, '! ', 7, 21, 36, 44])

.

Exercice N°2 :

Chercher :

Pour rappel, la conversion d’un nombre entier positif en binaire peut s’effectuer à l’aide
des divisions successives comme illustré ici :

Voici une fonction python basée sur la méthode des divisions successives permettant de
convertir un nombre entier positif en binaire :

def binaire(a):

    bin_a = str(...)

    a = a // 2

    while a ... :

        bin_a = ...(a%2) + ...

        a = ...

    return bin_a

 

Compléter la fonction binaire.
Exemples :

>>> binaire(0)

'0'

>>> binaire(77)

'1001101

image/21.png

.

REPONSE

.

Programme 

def binaire(a):

  

    bin_a =str(a%2)

    a = a // 2

    while a !=0:

        bin_a =str(a%2) +bin_a

        a = a//2

    return bin_a

 

for i in range(100):

    print(i,' = ',binaire(i))

RESULTATS 100 convertions

0  =  0

1  =  1

2  =  10

3  =  11

4  =  100

5  =  101

6  =  110

7  =  111

8  =  1000

9  =  1001

10  =  1010

11  =  1011

12  =  1100

13  =  1101

14  =  1110

15  =  1111

16  =  10000

17  =  10001

18  =  10010

19  =  10011

20  =  10100

21  =  10101

22  =  10110

23  =  10111

24  =  11000

25  =  11001

26  =  11010

27  =  11011

28  =  11100

29  =  11101

30  =  11110

31  =  11111

32  =  100000

33  =  100001

34  =  100010

35  =  100011

36  =  100100

37  =  100101

38  =  100110

39  =  100111

40  =  101000

41  =  101001

42  =  101010

43  =  101011

44  =  101100

45  =  101101

46  =  101110

47  =  101111

48  =  110000

49  =  110001

50  =  110010

51  =  110011

52  =  110100

53  =  110101

54  =  110110

55  =  110111

56  =  111000

57  =  111001

58  =  111010

59  =  111011

60  =  111100

61  =  111101

62  =  111110

63  =  111111

64  =  1000000

65  =  1000001

66  =  1000010

67  =  1000011

68  =  1000100

69  =  1000101

70  =  1000110

71  =  1000111

72  =  1001000

73  =  1001001

74  =  1001010

75  =  1001011

76  =  1001100

77  =  1001101

78  =  1001110

79  =  1001111

80  =  1010000

81  =  1010001

82  =  1010010

83  =  1010011

84  =  1010100

85  =  1010101

86  =  1010110

87  =  1010111

88  =  1011000

89  =  1011001

90  =  1011010

91  =  1011011

92  =  1011100

93  =  1011101

94  =  1011110

95  =  1011111

96  =  1100000

97  =  1100001

98  =  1100010

99  =  1100011