2019-2020/Formations/NSI/Bloc1/diubloc1-files-td5/Conversions.ipynb
2020-05-05 09:53:14 +02:00

111 lines
1.9 KiB
Plaintext

{
"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
}