{ "cells": [ { "cell_type": "markdown", "id": "83baffd8-f5d2-4e9c-8d09-9e213cc45629", "metadata": {}, "source": [ "# C: Ejercicios extra resueltos (B y C)\n", "\n", "Estos ejercicios se supone que tiene que resolverlos el alumno. Ni están todos los que deberían ni tienen por que ser muy legibles." ] }, { "cell_type": "markdown", "id": "8b1dda5b-9323-4686-b18c-c347597e4aaf", "metadata": {}, "source": [ "**B.13:**" ] }, { "cell_type": "code", "execution_count": 1, "id": "3276b8db-a230-4747-a177-450a645771ac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n", " * \n", " **** \n", " **** \n", " **** \n", " ** * \n", " * ********** \n", " ****************** \n", " ***************** \n", " * ***************** \n", " ******************* \n", " *********************** \n", " ********************* \n", " * *** ********************** \n", " ******* ********************** \n", " ********* ********************** \n", " ********* ********************** \n", " ** ********* ********************* \n", " * ************************************ \n", " ** ********* ********************* \n", " ********* ********************** \n", " ********* ********************** \n", " ******* ********************** \n", " * *** ********************** \n", " ********************* \n", " *********************** \n", " ******************* \n", " * ***************** \n", " ***************** \n", " ****************** \n", " * ********** \n", " ** * \n", " **** \n", " **** \n", " **** \n", " * \n" ] } ], "source": [ "def is_stable(c, num_iterations):\n", " z = 0\n", "\n", " for _ in range(num_iterations):\n", " z = z*z + c\n", "\n", " return abs(z) <= 2\n", "\n", "y = -.9\n", "while y <= .9:\n", " x = -2.0\n", " while x <= .5:\n", " if is_stable(complex(x, y) , 100):\n", " print(\"*\", end=\"\")\n", " else:\n", " print(\" \", end=\"\")\n", " x += .05\n", " print(\"\")\n", " y += .05\n", " \n", " \n" ] }, { "cell_type": "markdown", "id": "4782500d-5088-48f4-8fd3-cbef3ef6afec", "metadata": {}, "source": [ "**C.3:**" ] }, { "cell_type": "code", "execution_count": 2, "id": "3829b84a-d2ea-4fa2-b2d6-68dc083f3fd6", "metadata": {}, "outputs": [], "source": [ "def is_stable(c, num_iterations):\n", " z = 0\n", "\n", " for _ in range(num_iterations):\n", " z = z * z + c\n", "\n", " return abs(z) <= 2\n", "\n", "filas = []\n", "\n", "y = -.9\n", "while y <= .9:\n", " fila = []\n", " \n", " x = -2.0\n", " while x <= .5:\n", " if is_stable(complex(x, y) , 100):\n", " fila.append(255)\n", " else:\n", " fila.append(0)\n", " x += .005\n", " y += .005\n", " filas.append(fila)\n", " \n", "\n", "fichero = open(\"salidas/fractal.pgm\", \"wb\") # El modificador ```b``` especifica que el fichero es binario\n", "\n", "informacion = f\"P5 {len(filas[0])} {len(filas)} 255 \"\n", "\n", "b = bytes(informacion, encoding=\"ASCII\")\n", "fichero.write(b)\n", "\n", "for f in filas:\n", " b = bytes(f)\n", " fichero.write(b)\n", "\n", "fichero.close()" ] }, { "cell_type": "markdown", "id": "604fd891-76ff-48a2-988b-f05317464060", "metadata": {}, "source": [ "**C.4:**" ] }, { "cell_type": "code", "execution_count": 3, "id": "01e8d1af-08e3-4d56-85ea-e9f12ff0a32a", "metadata": {}, "outputs": [], "source": [ "def test_stable(c, num_iterations):\n", " z = 0\n", "\n", " i = 0\n", " \n", " while abs(z) <= 2 and i < num_iterations:\n", " z = z * z + c\n", " i += 1\n", "\n", " if abs(z) <= 2:\n", " return 0\n", " else:\n", " return i\n", "\n", "#print(test_stable(complex(0.1,.5), 254))\n", "\n", "maxIter = 50\n", "\n", "filas = []\n", "\n", "y = -.9001\n", "while y <= .9:\n", " fila = []\n", " \n", " x = -2.001\n", " while x <= .5:\n", " fila.append(test_stable(complex(x, y), maxIter))\n", " x += .0025\n", " y += .0025\n", " filas.append(fila)\n", " \n", "\n", "fichero = open(\"salidas/fractal2.pgm\", \"wb\") # El modificador ```b``` especifica que el fichero es binario\n", "\n", "informacion = f\"P5 {len(filas[0])} {len(filas)} {maxIter} \"\n", "\n", "b = bytes(informacion, encoding=\"ASCII\")\n", "fichero.write(b)\n", "\n", "for f in filas:\n", " b = bytes(f)\n", " fichero.write(b)\n", "\n", "fichero.close()\n" ] } ], "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.16" } }, "nbformat": 4, "nbformat_minor": 5 }