Created
August 13, 2017 22:17
-
-
Save odarbelaeze/dd6f89a0d7fbd9d8b78cb9b86fd16cbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0b110\n", | |
| "0b11\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(bin(0b100111 // 0b110))\n", | |
| "print(bin(0b100111 % 0b110))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def dividí(num, den):\n", | |
| " num = bin(num)\n", | |
| " den = bin(den)\n", | |
| " print(f'{num} / {den}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0b1010011 / 0b100\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "dividí(0x53, 4)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "341" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "0b101010101" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0d3451'" | |
| ] | |
| }, | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'0d3451'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['b', 1, 0, 1, 0, 1]" | |
| ] | |
| }, | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "['b', 1, 0, 1, 0, 1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['x', 1, 0, 1, 0, 1]" | |
| ] | |
| }, | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "['x', 1, 0, 1, 0, 1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['o', 1, 0, 1, 0, 1]" | |
| ] | |
| }, | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "['o', 1, 0, 1, 0, 1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 201, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from functools import wraps\n", | |
| "\n", | |
| "def simplify_binary(func):\n", | |
| " @wraps(func)\n", | |
| " def wrapper(*args, **kwargs):\n", | |
| " result = func(*args, **kwargs)\n", | |
| " prefix, number = result[:2], result[2:]\n", | |
| " index = number.find('1')\n", | |
| " return prefix + number[index:]\n", | |
| " return wrapper" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 202, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "@simplify_binary\n", | |
| "def octal_a_binario(octal):\n", | |
| " if not octal.lower().startswith('0o'):\n", | |
| " raise ValueError(f'{octal} no es un octal.')\n", | |
| " oct_to_bin_map = {'0': '000', '1': '001', '2': '010', '3': '011',\n", | |
| " '4': '100', '5': '101', '6': '110', '7': '111'}\n", | |
| " try:\n", | |
| " digits = [oct_to_bin_map[digit] for digit in octal[2:]]\n", | |
| " except KeyError:\n", | |
| " raise ValueError(f'{octal} no es un octal bien formado.')\n", | |
| " return '0b' + ''.join(digits)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 203, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0b1111001001'" | |
| ] | |
| }, | |
| "execution_count": 203, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "octal_a_binario('0o1711')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 204, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "@simplify_binary\n", | |
| "def hex_a_binario(hexadecimal):\n", | |
| " if not hexadecimal.lower().startswith('0x'):\n", | |
| " raise ValueError(f'{hexadecimal} no es un hexadecimal.')\n", | |
| " hex_to_bin_map = {'0': '0000', '1': '0001', '2': '0010', '3': '0011',\n", | |
| " '4': '0100', '5': '0101', '6': '0110', '7': '0111',\n", | |
| " '8': '1000', '9': '1001', 'A': '1010', 'B': '1011',\n", | |
| " 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}\n", | |
| " try:\n", | |
| " digits = [hex_to_bin_map[digit.upper()] for digit in hexadecimal[2:]]\n", | |
| " except KeyError:\n", | |
| " raise ValueError(f'{hexadecimal} no es un hexadecimal bien formado.')\n", | |
| " return '0b' + ''.join(digits)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 205, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0b100100011000100110010000101000101000100100011111100010010101010101010'" | |
| ] | |
| }, | |
| "execution_count": 205, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "hex_a_binario('0x123132145123f12aaa')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 206, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "@simplify_binary\n", | |
| "def decimal_a_binario(decimal):\n", | |
| " if not decimal.isnumeric():\n", | |
| " raise ValueError(f'{decimal} no es un decimal.')\n", | |
| " num = int(decimal)\n", | |
| " digits = [str(num % 2)]\n", | |
| " while num > 1:\n", | |
| " num //= 2\n", | |
| " digits.append(str(num % 2))\n", | |
| " return '0b' + ''.join(reversed(digits))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 207, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 207, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "all(decimal_a_binario(str(num)) == bin(num) for num in range(10000))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 221, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "@simplify_binary\n", | |
| "def a_binario(num):\n", | |
| " if num.lower().startswith('0b'):\n", | |
| " return num\n", | |
| " if num.lower().startswith('0o'):\n", | |
| " return octal_a_binario(num)\n", | |
| " if num.lower().startswith('0x'):\n", | |
| " return hex_a_binario(num)\n", | |
| " if num.isnumeric():\n", | |
| " return decimal_a_binario(num)\n", | |
| " raise ValueError('What is this {num}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 222, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0b11110110100110110101'" | |
| ] | |
| }, | |
| "execution_count": 222, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "a_binario('1010101')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 223, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def suma_un_digito(a, b, acarreo):\n", | |
| " cont = sum(item == '1' for item in [a, b, acarreo])\n", | |
| " if cont == 0:\n", | |
| " return '0', '0'\n", | |
| " if cont == 1:\n", | |
| " return '1', '0'\n", | |
| " if cont == 2:\n", | |
| " return '0', '1'\n", | |
| " if cont == 3:\n", | |
| " return '1', '1'\n", | |
| " raise ValueError('What does {a}, {b}, {acarreo} mean')\n", | |
| "\n", | |
| "\n", | |
| "def partes_de_la_suma(a, b, fill='0'):\n", | |
| " a = a_binario(a)\n", | |
| " b = a_binario(b)\n", | |
| " acarreo = '0'\n", | |
| " digitos = []\n", | |
| " for a, b in itertools.zip_longest(reversed(a[2:]), reversed(b[2:]), fillvalue=fill):\n", | |
| " digito, acarreo = suma_un_digito(a, b, acarreo)\n", | |
| " digitos.append(digito)\n", | |
| " return ''.join(reversed(digitos)), acarreo\n", | |
| "\n", | |
| "\n", | |
| "@simplify_binary\n", | |
| "def suma(a, b):\n", | |
| " digitos, acarreo = partes_de_la_suma(a, b)\n", | |
| " return '0b' + acarreo + ''.join(reversed(digitos))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 224, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0b1101110'" | |
| ] | |
| }, | |
| "execution_count": 224, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "suma('0x35', '0o50')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 225, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0b1011101'" | |
| ] | |
| }, | |
| "execution_count": 225, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "bin(0x35 + 0o50)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 226, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "@simplify_binary\n", | |
| "def complemento(binario):\n", | |
| " if not binario.startswith('0b'):\n", | |
| " raise ValueError(f'{binario} no es un binario.')\n", | |
| " return '0b' + ''.join('1' if num == '0' else '0' for num in binario[2:])\n", | |
| "\n", | |
| "def resta(minuendo, sustraendo):\n", | |
| " minuendo = a_binario(minuendo)\n", | |
| " sustraendo = a_binario(sustraendo)\n", | |
| " resta, acarreo = partes_de_la_suma(minuendo, complemento(sustraendo), fill='1')\n", | |
| " if acarreo == '0':\n", | |
| " raise ValueError(f'Underflow restando {minuendo} - {sustraendo} (complemento: {complemento(sustraendo)})')\n", | |
| " else:\n", | |
| " print(resta)\n", | |
| " return suma('0b' + resta, '0b1')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 227, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0b1'" | |
| ] | |
| }, | |
| "execution_count": 227, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "complemento('0b1110')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 228, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'0b1111111111111111111111111111'" | |
| ] | |
| }, | |
| "execution_count": 228, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "num = '0b1001110101001001010101010010'\n", | |
| "suma(num, complemento(num))" | |
| ] | |
| } | |
| ], | |
| "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.6.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment