C: Ejercicios extra resueltos (B y C)

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.

B.13:

[1]:
def is_stable(c, num_iterations):
    z = 0

    for _ in range(num_iterations):
        z = z*z + c

    return abs(z) <= 2

y = -.9
while y <= .9:
    x = -2.0
    while x <= .5:
        if is_stable(complex(x, y) , 100):
            print("*", end="")
        else:
            print(" ", end="")
        x += .05
    print("")
    y += .05


                                      *
                                    ****
                                    ****
                                    ****
                                  **   *
                              *  **********
                             ******************
                              *****************
                           * *****************
                            *******************
                          ***********************
                           *********************
                 * ***    **********************
                 *******  **********************
                ********* **********************
                ********* **********************
             ** ********* *********************
     *    ************************************
             ** ********* *********************
                ********* **********************
                ********* **********************
                 *******  **********************
                 * ***    **********************
                           *********************
                          ***********************
                            *******************
                           * *****************
                              *****************
                             ******************
                              *  **********
                                  **   *
                                    ****
                                    ****
                                    ****
                                      *

C.3:

[2]:
def is_stable(c, num_iterations):
    z = 0

    for _ in range(num_iterations):
        z = z * z + c

    return abs(z) <= 2

filas = []

y = -.9
while y <= .9:
    fila = []

    x = -2.0
    while x <= .5:
        if is_stable(complex(x, y) , 100):
            fila.append(255)
        else:
            fila.append(0)
        x += .005
    y += .005
    filas.append(fila)


fichero = open("salidas/fractal.pgm", "wb")   # El modificador ```b``` especifica que el fichero es binario

informacion = f"P5 {len(filas[0])} {len(filas)} 255 "

b = bytes(informacion, encoding="ASCII")
fichero.write(b)

for f in filas:
    b = bytes(f)
    fichero.write(b)

fichero.close()

C.4:

[3]:
def test_stable(c, num_iterations):
    z = 0

    i = 0

    while abs(z) <= 2 and i < num_iterations:
        z = z * z + c
        i += 1

    if abs(z) <= 2:
        return 0
    else:
        return i

#print(test_stable(complex(0.1,.5), 254))

maxIter = 50

filas = []

y = -.9001
while y <= .9:
    fila = []

    x = -2.001
    while x <= .5:
        fila.append(test_stable(complex(x, y), maxIter))
        x += .0025
    y += .0025
    filas.append(fila)


fichero = open("salidas/fractal2.pgm", "wb")   # El modificador ```b``` especifica que el fichero es binario

informacion = f"P5 {len(filas[0])} {len(filas)} {maxIter} "

b = bytes(informacion, encoding="ASCII")
fichero.write(b)

for f in filas:
    b = bytes(f)
    fichero.write(b)

fichero.close()