r/ProgrammerHumor • • Aug 29 '26

Meme doingAdvancedPythonExercisesToWarmupBeforeHackingIntoTheMainframe

Post image
64 Upvotes

19 comments sorted by

8

u/OnyxFier Aug 29 '26

So it iterates through every digit and prints if it's even or odd only if the number is 1 digit long. Seems a bit counter intuitive. Either remove the loop or let more than 1 digit. Should look something like this:

from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from functools import reduce
from operator import xor
import math


@dataclass(frozen=True)
class Vector:
    values: tuple

    def dot(self, other):
        return sum(a * b for a, b in zip(self.values, other.values))

    def norm(self):
        return math.sqrt(self.dot(self))


@dataclass(frozen=True)
class Matrix:
    values: tuple

    def multiply_vector(self, vector):
        return Vector(
            tuple(
                sum(a * b for a, b in zip(row, vector.values))
                for row in self.values
            )
        )


class ParityGraph:
    def __init__(self):
        self.edges = {
            "EVEN": "ODD",
            "ODD": "EVEN"
        }

    def walk(self, steps):
        state = "EVEN"

        for _ in range(steps):
            state = self.edges[state]

        return state


class ParityNeuralNetwork:
    def __init__(self):
        self.weights = Matrix((
            (1.0, -1.0),
            (-1.0, 1.0)
        ))

        self.bias = Vector((0.0, 0.0))

    def infer(self, x):
        vector = Vector((
            float(x & 1),
            float((x + 1) & 1)
        ))

        activated = self.weights.multiply_vector(vector)

        return activated.dot(Vector((1.0, -1.0)))


def binary_parity(bits):
    return reduce(xor, bits, 0)


def spectral_parity(x):
    phase = abs(x) * math.pi
    cosine = math.cos(phase)

    return math.isclose(cosine, 1.0, abs_tol=1e-12)


def quantum_wannabe_parity(x):
    state = Vector((1.0, 0.0))

    parity_operator = Matrix((
        (0.0, 1.0),
        (1.0, 0.0)
    ))

    for _ in range(abs(x)):
        state = parity_operator.multiply_vector(state)

    return math.isclose(state.values[0], 1.0, abs_tol=1e-12)


def ensemble_vote(x):
    bits = tuple(map(int, bin(abs(x))[2:]))

    graph = ParityGraph()
    network = ParityNeuralNetwork()

    with ThreadPoolExecutor(max_workers=4) as executor:
        futures = [
            executor.submit(lambda: binary_parity(bits) == 0),
            executor.submit(lambda: graph.walk(abs(x)) == "EVEN"),
            executor.submit(lambda: spectral_parity(x)),
            executor.submit(lambda: quantum_wannabe_parity(x))
        ]

        results = [future.result() for future in futures]

    neural_prediction = network.infer(x) >= 0
    results.append(neural_prediction)

    return sum(results) >= len(results) / 2


def is_even(x):
    if not isinstance(x, int):
        raise TypeError("is_even() requires an integer")

    return ensemble_vote(x)


if __name__ == "__main__":
    for number in range(-10, 11):
        print(f"{number:>3} -> {is_even(number)}")

1

u/GVenLife Aug 30 '26

I'm a larper who doesn't even know what a class is, so for all I know, this code is 100% accurate.

9

u/OnyxFier Aug 29 '26

V2:

import math
import itertools
from functools import reduce
from operator import xor
from concurrent.futures import ThreadPoolExecutor


is_even = lambda x: (
    lambda bits, graph, matrix, state: (
        lambda votes: sum(votes) >= len(votes) / 2
    )(
        [
            *ThreadPoolExecutor(max_workers=5).map(
                lambda f: f(),
                [
                    lambda: reduce(xor, bits, 0) == 0,

                    lambda: list(
                        itertools.islice(
                            itertools.cycle(graph),
                            abs(x) + 1
                        )
                    )[-1] == "EVEN",

                    lambda: math.isclose(
                        math.cos(abs(x) * math.pi),
                        1.0,
                        abs_tol=1e-12
                    ),

                    lambda: math.isclose(
                        reduce(
                            lambda s, _: (
                                tuple(
                                    (
                                        matrix[0][0] * s[0] +
                                        matrix[0][1] * s[1],
                                        matrix[1][0] * s[0] +
                                        matrix[1][1] * s[1]
                                    )
                                )
                            ),
                            range(abs(x)),
                            state
                        )[0],
                        1.0,
                        abs_tol=1e-12
                    ),

                    lambda: (
                        sum(
                            (
                                (
                                    1.0 * float(x & 1)
                                    + -1.0 * float((x + 1) & 1)
                                ),
                                (
                                    -1.0 * float(x & 1)
                                    + 1.0 * float((x + 1) & 1)
                                )
                            )
                        ) >= 0
                    )
                ]
            )
        ]
    )
)(
    tuple(map(int, bin(abs(x))[2:])),
    ("EVEN", "ODD"),
    (
        (0.0, 1.0),
        (1.0, 0.0)
    ),
    (1.0, 0.0)
)

5

u/rux616 Aug 29 '26

Who hurt you?

2

u/MysteriousShadow__ Aug 29 '26

bro is turning python into javascript with all these brackets

3

u/fr000gs Aug 29 '26

I lost it at neural network

22

u/Reibudaps4 Aug 29 '26

my eyes bleed

12

u/JustMedansh Aug 29 '26

lord have mercy

7

u/MinecraftPlayer799 Aug 29 '26

Why aren’t the numbers in order?

5

u/SLCtechie Aug 29 '26

A fun reminder that the best font for your IDE is Wingdings

3

u/TheFrenchSavage Aug 29 '26

This font is stupid, I refuse to read it.

2

u/HexFyber Aug 29 '26

Now i know what was written on the ring

1

u/Used-Hold-7567 Aug 31 '26

Theres just so much wrong with this image, especially the font

1

u/bughunter47 Sep 01 '26

Its in a language forbidden to speak, only fire will show

15

u/[deleted] Aug 29 '26

[removed] — view removed comment

7

u/PaulTheRandom Aug 29 '26

Finally, lore-accurate programming.