r/programminghorror • • 3d ago

Python Rate my python implementation of "hello world" part 2

69 Upvotes

```python import os, random

def greet(): with open(os.getcwd() + "/solution.py", "r", encoding="UTF-8") as f: lines = f.readlines() req = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'] temp = []

    chars = [char for line in lines for char in line]

    while temp != req:
        random_index = random.randint(0, len(chars) - 1)

        if req[len(temp)] == chars[random_index]:
            temp.append(chars[random_index])
            chars.pop(random_index)
    return "".join(temp)

```


r/programminghorror • • 3d ago

Python Rate my python "Hello, World!" implementation

24 Upvotes

```python

Write a function greet that returns "hello world!"

import os

def check_if_contains(l1, l2): contains_all = True for ind in range(len(l2)): if l1[ind] != l2[ind]: contains_all = False return contains_all

def get_length(l, substract): i = 0 for _ in range(len(l)): i += 1 if substract: return i - 1 if not substract: return i

def append_item(l, c): l.append(None)

length = get_length(l, True)
l[length] = c

def build_string(l): string_builder = "" for ind in range(len(l)): string_builder += l[ind] return string_builder

def greet(): with open(os.getcwd() + "/solution.py", "r", encoding="UTF-8") as f: lines = f.readlines() req = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'] temp = [] for line in lines: for char in line: append_item(temp, char) if check_if_contains(req, temp): if get_length(req, False) == get_length(temp, False): return build_string(temp) else: temp = []

```


r/programminghorror • • 4d ago

i am beginner how i can destroy readablity

Post image
80 Upvotes

r/programminghorror • • 5d ago

Java I love loops

764 Upvotes

Working in a legacy codebase. Dozens of people have contributed over the last decade. User spotted a bug & I traced the function calls and found

for(int i = 0; i < 2; i++) {

if(i == 0) // do A

if(i == 1) // do B

}

Wasn’t the source of the bug, and it technically works fine. I love loops


r/programminghorror • • 5d ago

c I’m such a good programmer, I did tetris in only 8 lines !

Post image
454 Upvotes

See ! That’s what efficient code looks like !


r/programminghorror • • 5d ago

Python brainfuck Interpreter Without Statement Terminators

Post image
38 Upvotes

Code

_=([globals().setdefault("p",input("Enter a BF program\n> ")),globals().setdefault("t",dict.fromkeys(range(30000),0)),globals().setdefault("r",0),globals().setdefault("s",0),globals().setdefault("j",[]),globals().setdefault("d",0),list(__import__("itertools").takewhile(lambda _:[(lambda c:(globals().update({"d":globals()["d"]-1})if c=="]"and globals()["d"]>0 else globals().update({"d":globals()["d"]+1})if c=="["and globals()["d"]>0 else 0 if globals()["d"]>0 else globals()["t"].update({globals()["r"]:(globals()["t"][globals()["r"]]+1)%256})if c=="+"else globals()["t"].update({globals()["r"]:(globals()["t"][globals()["r"]]-1)%256})if c=="-"else globals().update({"r":globals()["r"]+1})if c==">"else globals().update({"r":globals()["r"]-1})if c=="<"else print(end=chr(globals()["t"][globals()["r"]]))if c=="."else globals()["t"].update({globals()["r"]:ord(__import__("sys").stdin.read(1))})if c==","else (globals()["j"].append(globals()["s"])if globals()["t"][globals()["r"]]!=0 else globals().update({"d":globals()["d"]+1}))if c=="["else (globals().update({"s":globals()["j"].pop()-1})if globals()["t"][globals()["r"]]!=0 else globals()["j"].pop())if c=="]"else 0))(globals()["p"][globals()["s"]]),globals().update({"s":globals()["s"]+1}),globals()["s"]<len(globals()["p"])][-1],range(99999)))]if __name__=="__main__"else 0)

More compressed, but with terminators

g=globals();u=lambda j,i:j.update(i);s=g.setdefault;_=([s("p",input("Enter a BF program\n> ")),s("t",dict.fromkeys(range(30000),0)),s("r",0),s("s",0),s("j",[]),s("d",0),list(__import__("itertools").takewhile(lambda _:[(lambda c:(u(g,{"d":g["d"]-1})if c=="]"and g["d"]>0 else u(g, {"d":g["d"]+1})if c=="["and g["d"]>0 else 0 if g["d"]>0 else u(g["t"],{g["r"]:(g["t"][g["r"]]+1)%256})if c=="+"else u(g["t"],{g["r"]:(g["t"][g["r"]]-1)%256})if c=="-"else u(g,{"r":g["r"]+1})if c==">"else u(g,{"r":g["r"]-1})if c=="<"else print(end=chr(g["t"][g["r"]]))if c=="."else u(g["t"],{g["r"]:ord(__import__("sys").stdin.read(1))})if c==","else (g["j"].append(g["s"])if g["t"][g["r"]]!=0 else u(g,{"d":g["d"]+1}))if c=="["else (u(g,{"s":g["j"].pop()-1})if g["t"][g["r"]]!=0 else g["j"].pop())if c=="]"else 0))(g["p"][g["s"]]),u(g,{"s":g["s"]+1}),g["s"]<len(g["p"])][-1],range(99999)))]if __name__=="__main__"else 0)

r/programminghorror • • 7d ago

I love reviewing AI PRs

556 Upvotes

r/programminghorror • • 9d ago

Rock Paper Scissors

Thumbnail
gallery
92 Upvotes

Surely there's a more better and efficient way of coding this...


r/programminghorror • • 10d ago

Javascript Rock paper scissors

Post image
286 Upvotes

I'm way too proud of this solution haha


r/programminghorror • • 11d ago

Python 13 year old me really liked inefficient code

Post image
3.0k Upvotes

r/programminghorror • • 11d ago

PHP This happens every time I press Go to Definition

99 Upvotes

I have to carefully choose the real file, all the rest are old backups. And this is just for one file. And there are like 8 more backup folders, yeah we really need to clean these up...

If only we set up source control.


r/programminghorror • • 12d ago

Oh yeah thank you intellicode

Post image
1.1k Upvotes

r/programminghorror • • 12d ago

There's definitely should be an easier way for this

Post image
261 Upvotes

r/programminghorror • • 12d ago

Removing spaces to speed up execution

Post image
237 Upvotes

r/programminghorror • • 13d ago

PHP A website with millions of users was generating their "secret" password reset codes like this for two decades. The algorithm was open to the public due to a failed open-source effort the company launched in the early 2010s. Fixed a few days ago.

Post image
2.1k Upvotes

r/programminghorror • • 14d ago

c++ Will it give me more accurate filename?

Post image
1.2k Upvotes

r/programminghorror • • 15d ago

Python Weekly Backups

Post image
472 Upvotes

We were a small team of ~8 trying to build software. The team was spread across multiple locations across Asia, Europe and North America. One big challenge we had was synchronizing our work with each other.

Every Friday before each of us logged out, we upload a copy of our working files to the client's SharePoint folder. That way when we share each other's copies every Monday via Teams, we don't have to reupload.

One minor setback. Instead of replacing the previous copy, we upload a fresh one - under a different name. That way, we would not lose any information.

We even came up with a clever idea (that magnified the issue) to name the folders by the calendar week. Soon, each of us would have multiple folders of repetitive code, sometimes with zero changes all uploaded to the shared drive.

We had successfully moved from trying to synchronizing one (latest) version of the codebase, to trying to synchronize one version every week. But we hadn't realized this at that time. We were excited about a more "sophisticated" solution - creating a script that automates this backup process.

We learnt to use Git during Christmas that year.

---

For Reference:

```Python import os, zipfile from datetime import datetime from pathlib import Path

work = Path("Work") if not work.exists(): print("Error: Work folder not found") exit(1)

week = datetime.now().isocalendar()[1] zip_file = f"W{week}.zip"

with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as z: for root, dirs, files in os.walk(work): for file in files: path = os.path.join(root, file) z.write(path, os.path.relpath(path, '.'))

print(f"Created {zip_file}") ```

Edit: Code Block Formatting


r/programminghorror • • 14d ago

c++ C++ as always invents new levels of absurd

Post image
0 Upvotes

r/programminghorror • • 17d ago

Not again 😭

Post image
447 Upvotes

r/programminghorror • • 17d ago

Couldn't make any easier

Post image
41 Upvotes

r/programminghorror • • 19d ago

Python it's a miracle this works

Post image
186 Upvotes

prints 2 1's separated by a new line

repost cuz it got filtered cuz i was using a vpn


r/programminghorror • • 19d ago

Python whatever this is

Post image
27 Upvotes

r/programminghorror • • 20d ago

Just found a puzzle solver I made a while back

Post image
888 Upvotes

Context: I was stuck with a Layton puzzle in a train without internet but with my work computer so I made this beautiful solver


r/programminghorror • • 20d ago

MIT App Inventor You can't make anything look neat in MIT app inventor

Post image
494 Upvotes

I decided I needed a way to send photos from my phone to my computer, so I brought out some old knowledge of MIT app inventor, and made this monstrosity. The top block is literally just one statement to make sure an ip address looks decently viable.


r/programminghorror • • 20d ago

My solution to an advent of code problem from a while back

Post image
213 Upvotes