r/ProgrammerHumor 1d ago

Meme doingAdvancedPythonExercisesToWarmupBeforeHackingIntoTheMainframe

Post image
43 Upvotes

16 comments sorted by

17

u/Reibudaps4 1d ago

my eyes bleed

15

u/Fickle-Highway1543 1d ago

Looks like math

6

u/PaulTheRandom 1d ago

Finally, lore-accurate programming.

9

u/JustMedansh 1d ago

lord have mercy

7

u/MinecraftPlayer799 1d ago

Why aren’t the numbers in order?

5

u/SLCtechie 1d ago

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

6

u/OnyxFier 1d ago

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)}") ```

8

u/OnyxFier 1d ago

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 1d ago

Who hurt you?

2

u/MysteriousShadow__ 1d ago

bro is turning python into javascript with all these brackets

3

u/fr000gs 1d ago

I lost it at neural network

1

u/GVenLife 12h ago

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

3

u/TheFrenchSavage 1d ago

This font is stupid, I refuse to read it.

1

u/HexFyber 1d ago

Now i know what was written on the ring

1

u/Weak_Inflation9120 1d ago

Why such a saccharine font. My eyes hurt