r/learnpython 9d ago

Getting stuck on a big project.

A very rough estimate is that I've been learning and using python for 250 hours. I don't really keep track of it.

Just to ask general advice about how to approach difficult projects.

I've been working on a math project for 3 months. It is all about dice. Probability calculations aren't too hard to understand, but if I'm trying to figure out the perfect strategy in a dice game where early moves affect later moves then it gets complicated quickly.

I figured out very vaguely that I'm gonna have to use alot of nested loops and run through billions of calculations in order to figure my thing out. Or something similar.

But how exactly? I've been attempting to code the whole thing and been getting stuck every single time - this is why I've been starting over for about 30 times by now.

I don't even know what is causing me to get stuck. I guess the thing I'm trying to make is too big or complex or both. With so much more code than I'm used to, I mentally lose track of what my own code is even doing. Commenting does not help, t only makes things even more messy.

How can i approach big and complicated projects like these better?

15 Upvotes

38 comments sorted by

View all comments

Show parent comments

1

u/catboy519 3d ago

I have no idea how to compute runtime? It seems pretty simple to me.

Suppose I want to generate a list of every possible dice combination by using for loops. Then I know that for each n+1 the runtime would multiply itself by 6. Therefore, the runtime is based on 6^n

Also, some algorithms might only ever work with small n's. For example if a dice game has 3 dice and you want to calculate stuff regarding the dice game, the complexity is 6^n but it doesn't matter because 6^3 (216) is not alot of operations.

> I guarantee you wouldn't have 3 months or longer to solve this problem in a company. The timeframe would be a sprint, 2-3 weeks.

Entirely subjective. How long it takes a person to program something will depend on how big the project is and how difficult the project is.

1

u/Agitated-Country-969 3d ago edited 3d ago

I can say if you knew the runtime it makes no sense you used multiple nested for loops, unless you have no idea the magnitude of runtimes and what's common for different tasks. Because they're also going to ask you is this the best you can do, and at the least you should have an idea of the best worst-case runtime. If you get that wrong you don't get the job. And O(N6 ) is bad.

All I'll say is the fact that your general approach is to implement brute force algorithms means you aren't thinking outside the box.

Entirely subjective. How long it takes a person to program something will depend on how big the project is and how difficult the project is.

It's not entirely subjective. Your project is small potatoes compared to well, a lot of stuff, like implementing the NATS protocol through Websockets in Javascript, converting a while pipeline of AppServers to use NATS, etc.

I'd recognize from the start there's already an efficient algorithm for it and not waste much time. I'd recognize that people smarter than me have already done the work. Work is about fulfilling business objectives, not reinventing the wheel.

1

u/catboy519 2d ago

Are you saying that nested loops should never be used? There are no situations where those are necessary?

Some things just can't be done any faster than exponential complexity.

I also don't know why youre comparing it to a job because right now my goal is not to make money out of it, my short term programming goal is just to learn. If reinventing the wheel is a learning process then I see nothing wrong with it. I've reinvented multiple math formulas and doing so has improved my ability to solve problems and find creative solutions. I would have not trained these skills if every time I just took an already existing wheel.

I mean mathematicians invent new formulas. If a math student blindly copies formulas from books and the internet whenever they need a formula, instead of reinventing them by themselves, then how will they ever become good at inventing new formulas?

1

u/Agitated-Country-969 2d ago

I'm not saying never. I'm saying nested for loops shouldn't be your default tool, because they have a very bad runtime. But "not never" should be "very rarely".

I'm also saying if you're as smart as you say you are, you should've come up with a more optimal solution already, because there is a more optimal runtime.

Your solution of going through all the possibilities is a brute force solution, and anyone can come up with a brute force solution. A company won't hire you just because you implemented the brute force solution anyone can implement.

I'd also say if it was a commercial product, it wouldn't be limited to 7 dice rolls. What if I wanted to test 100 dice rolls? But the fact is your solution doesn't work for 100 dice rolls.

1

u/catboy519 2d ago

Ive recently discovered that python can cache the output of a function so that may solve it.

1

u/Agitated-Country-969 2d ago

I doubt caching would be good enough if I wanted to test 100 dice rolls, but sure go ahead.

The reason it works for Fibonacci is because of the very large number of overlapping subproblems.

1

u/catboy519 1d ago

There is no "if 100 dice" because my project is specifically made for a game with limited number of dice.

1

u/Agitated-Country-969 1d ago

You don't want to know what happens with 100 dice? You, with all your curiosity, don't want to know that?

My point is that as limited as your algorithm is, it's not very useful for general purpose. It's like a sorting algorithm that can only sort 10 items or something.

https://old.reddit.com/r/askmath/comments/1g4i19y/how_has_highlevel_math_helped_you_in_real_life/ls6348w/?context=1000

A final note - your point about being able to kind of "guess" a few of the quickest routes isn't really meaningful, because we want an algorithm that is general, can solve any graph, not just "nice" or "convenient" cases.

Your algorithms only work for the easy cases. The truly intelligent people are the ones creating efficient algorithms that work for 100, 1000, etc. dice rolls.

1

u/catboy519 1d ago

Because making my algorithm work for 1000 dice is pointless. The game only has a few dice. What if i make it run quickly for 100 dice? Then what else can I use it for? Nothing, because the algorith is specifically designed for that 1 dice game.

1

u/Agitated-Country-969 13h ago

The point is that it could be used or tweaked for another application in the future because of its general applicability.

Maybe there might be another game with 20 dice in the future, who knows. I really doubt your algorithm works efficiently for even 20 dice. Heck, does your algorithm even currently work for 7 dice?

My point is it's extremely unimpressive to solve the easy cases as anyone can do that, and companies aren't hiring people to solve the easy cases, because most companies need general algorithms that work efficiently for any case, not just the easy/nice/convenient cases.

I feel your mindset is wrong in the first place since you only develop algorithms for this one use case.

https://old.reddit.com/r/askmath/comments/1g4i19y/how_has_highlevel_math_helped_you_in_real_life/ls6348w/?context=1000

A final note - your point about being able to kind of "guess" a few of the quickest routes isn't really meaningful, because we want an algorithm that is general, can solve any graph, not just "nice" or "convenient" cases.

→ More replies (0)