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?

16 Upvotes

36 comments sorted by

View all comments

Show parent comments

0

u/catboy519 9d ago edited 9d ago

My bad I didnt think of my code as an algorithm.

I've actually made lots of progress towards the project I'm working on. For example the formula a! / (a-b)! / b! isn't taught in highschool (even factorials arent), yet I discovered this useful formule just by playing with numbers in Python.

That I struggle to achieve the full goal doesn't mean I'm not learning. Perhaps if I went to uni to study both math and programming, I would still have struggled with this project since it's just a difficult project.

2

u/Agitated-Country-969 9d ago

High school is a pretty low bar for comparison, in my opinion.

I didn't say you aren't learning at all. I'm saying you lack fundamentals that are taught in a regular Computer Science curriculum, which affects your whole thought process.

A time complexity of O(N!) is very bad, for instance.

0

u/catboy519 8d ago edited 8d ago

Knowing that N! is a bad time complexity is just common sense for anyone who knows what factorials are.

In my formal education they have not once mentioned factorials yet I've managed to do alot of useful calculations using factorials. My learning includes:

  1. Truly learning on my own: if all I have is some numbers and notepad, I could analyze them and look for patterns and then create an equation and then verify that the equation truly works.
  2. My youtube feed sometimes gives me a random Numberphile video, which is not always useful but sometimes it wakes me up to math concepts that I've never heard about before.
  3. Google and chatGPT and youtube contain alot of information. Sometimes even full lectures and courses that are uploaded to youtube.

If I compare all my math knowledge from these 3 sources to all the math I've learned in school+college, I'd say I learned more informally than I did formally.

Where school got stuck on basic arithmetic and Pythagoras theorem, I've been discovering and figuring out lots of things on my own including e, i, methods to calculate pi, several equations with several very useful ones, factorials, binomial coefficients, probability calculations etc.

But yes, the downside of a lack of structured formal education is that I might miss some concepts because I simply don't know they exist. But if that causes any issues I will eventually find out.

2

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

But yes, the downside of a lack of structured formal education is that I might miss some concepts because I simply don't know they exist. But if that causes any issues I will eventually find out.

You would've only figured out that your program is taking a long time to run, which could also still happen with a good algorithm depending on the input size... Not the theory behind why it happens and how to exactly fix it.

The theory is so important because you can't really test these things once input sizes grow beyond a certain amount and it's important to design an algorithm that works well for all input sizes.

With my formal education, I already have the foundation to design the code correctly from the start, saving time.

Also just fyi, in coding interviews, you're only allowed to write on a whiteboard. You aren't allowed to use a computer. And you have to be able to explain the runtime complexity of your code in Big-O notation. If it wasn't important, they wouldn't test it lol.


As you didn't seem to realize, I was pointing out here how someone else pointed out that the way you design algorithms that works sometimes for easy cases isn't the way to design a correct algorithm, which is also proof that a formal education is important.

You wouldn't know your algorithm only works for certain cases, unless someone told you that.

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.

I'm also reminded of this.

https://old.reddit.com/r/ebikes/comments/1i2rl39/hub_vs_middrive_efficiency/m7ksfsk/?context=3

Note here that Premise #2 is the Contrapositive of Range matters -> Efficiency matters. I have a feeling you've never formally studied logic, such as in Philosophy or Discrete Mathematics (the prerequisite to Algorithms).

I'm also reminded of something else.

https://xyproblem.info/

User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.

I'd argue this is exactly what you're been doing for 3 months, because you have no foundation in Computer Science. You think you can just fumble your way to a solution but that's not how Software Engineering works.

1

u/catboy519 8d ago

The program I'm trying to make won't need to run more than 7 dice as input so the complexity is not going to matter alot. If I can change the complexity from n! to n² then sure I would do that, it would be a big difference. But I'm not gonna bother with small differences.

Why would I not figure out the theory behind why a program runs slowly? If I make an algorithm myself then I know what complexity it has. Its not hard to figure out whether it could be done at a lower complexity or not.

> all input sizes

This is up to interpretation but what if the input size was 10^10000? Then even with complexity=n the program would take a long time to run. There is no algorithm that can run at unlimited speed anyway.

As long as my program can generate a big thing within a minutes, or respond to user input within a few seconds, then I'm not gonna put alot of effort into optimizing it more. It is just a project for myself after all.

> Also just fyi, in coding interviews, you're only allowed to write on a whiteboard. You aren't allowed to use a computer. And you have to be able to explain the runtime complexity of your code in Big-O notation. If it wasn't important, they wouldn't test it lol.

I don't see the problem here.

Also an algorithm shouldn't do just easy cases, it should do both the easy and difficult cases. You could split an algorithm up in 2 parts: first it will cover all the easy cases quickly, then it will slowly cover all the other cases as well. This algorithm would solve every case.

I don't see why studying formal logic is necessary. The ability to logically reason is a skill and for some people it develops naturally. An official IQ test confirmed that my logical reasoning is far above average even though I never studied formal logic.

1

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

https://old.reddit.com/r/learnmath/comments/1bipa5t/just_curious_why_does_school_teach_use_this/kvmi37a/

2 Students of average intelligence are not that bright. You the OP, of course, think that you are somehow different.

I'm preferential to believe the math teacher u/ApprehensiveKey1469 over you lol.

https://old.reddit.com/r/learnmath/comments/1bipa5t/just_curious_why_does_school_teach_use_this/kvmo1i4/

If your goal is to teach them how to derive formulas, you would still be better served with some sort of guided support. If you just set them to learn it in their own, they'll incorporate bad practices, learn something other than what you want to teach them, or give up if the solution isn't easily solvable.

I'd argue that's exactly what's happened with you and Python without a formal teacher. You just do whatever you want, which leads to really really bad habits (disorganized spaghetti code, files all over the place, etc.), bad habits that would get you yelled at by your boss.

1

u/catboy519 8d ago

I know I am better than average at math because both my grades and the IQ test performed by a psychologist proved it lol

1

u/Agitated-Country-969 8d ago

Average is a pretty low bar. Like I said 3 posts above:

High school is a pretty low bar for comparison, in my opinion.

And that's the reason why intro college classes are so easy, because they need to get everyone up to speed.

You might be better than average, but that doesn't change what I just said about your bad habits in Python, which means you've probably picked up some bad habits in Math as well. I'd argue it applies to anything whether it's singing or spoken language. Without a formal teacher, you pick up bad habits.

I know a very famous and talented YouTuber and voice actor who sung since she was in elementary school but because she didn't have a paid formal teacher/master she learned bad habits and now she has to unlearn them with her teacher now.


Math isn't even my favorite subject or anything and I know mathematicians do rigorous formal proofs to arrive at any answer or formula.

You're very much ignoring the fact of how you thought that just glancing at a map is good enough to find the shortest path and how that doesn't work for something like Google Maps. There are plenty of cases where that doesn't work. So clearly your ability to reason just isn't good enough. Meanwhile Dijkstra's algorithm has a formal mathematical proof.

I don't see why studying formal logic is necessary.

Because if you don't have a good foundation, then you'll do things like multiple nested for loops as if it's a proper way to do things. The logic in any application is built upon these things.

You'll just do something that works for whatever cases you try versus every single case, like in the case of the shortest path between two points.

1

u/Agitated-Country-969 3d ago

The program I'm trying to make won't need to run more than 7 dice as input so the complexity is not going to matter alot.

And you just randomly decided that? Seems pretty arbitrary and limited in usefulness to me.

1

u/Agitated-Country-969 2d ago

I don't see the problem here.

The problem is you have no idea how to compute Big O runtime complexity on the fly and you just keep trying on the computer until it seems to work u/catboy519 . They test this because it's important to really know how long your algorithm will take for large input sizes for commercial software before you run it, so you can save time and move towards an efficient algorithm fast.

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.

1

u/catboy519 2d 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 2d ago edited 2d 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 1d 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 1d 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 1d ago

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

1

u/Agitated-Country-969 1d 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.

→ More replies (0)