Import all
This commit is contained in:
110
Formations/NSI/Bloc1/diubloc1-files-td5/Conversions.ipynb
Normal file
110
Formations/NSI/Bloc1/diubloc1-files-td5/Conversions.ipynb
Normal file
@@ -0,0 +1,110 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def binaire2decimal(chaine):\n",
|
||||
" ans = 0\n",
|
||||
" for c in chaine:\n",
|
||||
" print(c)\n",
|
||||
" ans = ans*2+int(c)\n",
|
||||
" return ans"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1\n",
|
||||
"0\n",
|
||||
"0\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"4"
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"binaire2decimal(\"100\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 47,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def decimal2binaire(nombre):\n",
|
||||
" binaire = []\n",
|
||||
" reste = nombre\n",
|
||||
" while reste:\n",
|
||||
" binaire.append(reste % 2)\n",
|
||||
" reste = reste // 2\n",
|
||||
" return binaire[::-1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0]"
|
||||
]
|
||||
},
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"decimal2binaire(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
40
Formations/NSI/Bloc1/diubloc1-files-td5/change_repr.py
Normal file
40
Formations/NSI/Bloc1/diubloc1-files-td5/change_repr.py
Normal file
@@ -0,0 +1,40 @@
|
||||
#! /usr/bin/env python3
|
||||
"""
|
||||
python3 demo_test2.py
|
||||
"""
|
||||
__author__ = "Laure Gonnord"
|
||||
__copyright__ = "Univ Lyon1, 2019"
|
||||
|
||||
## fortement inspiré de http://sdz.tdct.org/sdz/du-decimal-au-binaire.html
|
||||
|
||||
CHIFFRES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
NOMBRES = {c: n for (n, c) in enumerate(CHIFFRES)}
|
||||
|
||||
# Fonctions utilitaires
|
||||
def chiffre(n):
|
||||
return CHIFFRES[n]
|
||||
|
||||
def nombre(ch):
|
||||
"""ch= caractère qui désigne le nombre
|
||||
retourne le nombre associé"""
|
||||
return NOMBRES[ch]
|
||||
|
||||
chiffre(12) # 'C'
|
||||
nombre('C') # 12
|
||||
|
||||
# base b vers décimal
|
||||
def lire_repr(rep, b):
|
||||
nb_chiff = len(rep)
|
||||
somme = 0
|
||||
# TODO
|
||||
return somme
|
||||
|
||||
print(lire_repr('2A',16))
|
||||
|
||||
# représentation du nombre n en base b, retourne une chaîne
|
||||
# à chaque fois on ajoute à la fin de la chaîne.
|
||||
def repr_nombre(n, b):
|
||||
pass
|
||||
|
||||
print(repr_nombre(10, 2)) # 1010
|
||||
print(repr_nombre(42, 16)) # 2A
|
||||
21
Formations/NSI/Bloc1/diubloc1-files-td5/demo_repnombres.py
Normal file
21
Formations/NSI/Bloc1/diubloc1-files-td5/demo_repnombres.py
Normal file
@@ -0,0 +1,21 @@
|
||||
#! /usr/bin/env python3
|
||||
"""
|
||||
python3 demo_repnombres.py
|
||||
"""
|
||||
__author__ = "Laure Gonnord"
|
||||
__copyright__ = "Univ Lyon1, 2019"
|
||||
|
||||
# expérimentations autour de la représentation des nombres en python
|
||||
|
||||
x = bin(42)
|
||||
print(type(x)) # c'est une chaîne
|
||||
print(x)
|
||||
|
||||
y = hex(54)
|
||||
print(y)
|
||||
|
||||
z = 67
|
||||
|
||||
|
||||
# on peut utiliser le formattage python pour imprimer les rep.
|
||||
print("my num is 0x{0:02x}".format(z))
|
||||
Reference in New Issue
Block a user