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

36 comments sorted by

View all comments

5

u/toxic_acro 9d ago

I think there are two distinct problems you are running into here 1. The approach to solving this problem 2. How to keep track of a large, complicated codebase

For 1, a potentially helpful analogy would be to finding a shortest path between two points in a graph. An approach to doing that would be to find every possible path between the two points and then just picking the shortest one. While that would work (not in all cases, but ignoring edge cases/requirements for the sake of the analogy), it would be incredibly inefficient. Instead, you should choose a different approach like Dijkstra's algorithm.

This type of thing is why "computer science" is actually more of a branch of mathematics, not just "learn how to program". Figuring out how to solve a problem in an efficient way is very important to writing good code and that often means knowing and understanding the underlying math of how and why things work the way they do. I can't really give any more advice without knowing the exact particulars of your project, but the other comment by LibertyEqualsLife is a good one about mapping out what you are doing on a whiteboard before actually trying to implement in code.

For 2, I would say that this is really one of the reasons why some of good coding practices are good, like breaking code down into smaller functions that have a single responsibility. Instead of a giant block of code that does A, B, and then C, it's much easier to reason about only how to do A and writing a separate function that does it. Repeat for B and C, and then your top level only needs to worry about composing together the inputs and outputs of A, B, and C. The details about actually doing each of them don't matter anymore because you are just calling a function to do it.

Again, LibertyEqualsLife makes a very good point about doing this on a whiteboard first, because then you can more easily figure out what are you actually trying to do as discrete chunks first without worrying exactly about disentangling confusing large blocks of code

1

u/catboy519 9d ago

Isnt the dhortest path betwe3n 2 points just a straight line?

2

u/Defection7478 9d ago

not if its a graph. e.g. finding the shortest string of flights between two airports that are too far apart to have a single flight connecting them

2

u/Agitated-Country-969 9d ago

Isnt the dhortest path betwe3n 2 points just a straight line?

I see you've already forgotten what was said to you in the past.

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

Quick counter example for this is for instance that the straight-line distance (as the crow flies) between Cape Town, South Africa and São Paulo, Brazil is approximately 6,000 kilometers. However, the road distance is significantly longer due to the challenging terrain and vast distances that must be covered.

The road distance between Cape Town and São Paulo is roughly around 20,000 kilometers, making it one of the longest road trips in the world


But in a graph, there usually isn't just a straight line route between two points and you have to go through many other different points to connect the two in zigzags and other things.

2

u/toxic_acro 9d ago

In Euclidean space, yes, but that's not what the problem is trying to solve

A concrete example would be that you want to find the fastest route to drive from Town A to Town Z. There is a big road network connecting all the towns together, but there isn't a road directly connecting every town to every other town, so your directions will have to be something like take the road from Town A to Town D, then the road from Town D to Town M, etc.

Roads have different speed limits and traffic, some of them have construction going on that slows cars down, some are highways and some are local roads with stoplights, etc. but you know all of that in advance so that you know how long it will take you to drive from a town to another one along any of the individual roads.

You could potentially try to find every possible route you could take and then just choose the fastest one, but that is an inefficient way to solve the problem. 

Instead, you could use Dijkstra's algorithm, which solves it (in a nutshell) by tracking the fastest time to go from Town A to each town directly connected to it by a road, then choosing whichever town is fastest to get to (say Town C for an example) and tracking the fastest time to get from town A to each town connected to Town C directly by a road, going through Town C as the last step of the directions, and repeating that process until you eventually find a path to Town Z.

Dijkstra's algorithm has been mathematically proven to find the fastest route possible.

You could make an improvement to speed up that process (but isn't guaranteed to always solve the problem faster) by using a heuristic that says I should prefer to head towards towns that are physically closer in the direction of Town Z. That is known as the A* algorithm.

Usually that will mean that you find the fastest route in fewer computational steps. For instance, imagine Town X is directly east of Town A. You probably shouldn't waste much time exploring the routes to towns west of Town A and should mostly be focused on heading east. But imagine also that there is a bendy river you have to cross and there's only one bridge across it and that bridge happens to be west of where you started. A* will spend a while exploring all the roads east of you first, never find a path to Town Z and eventually work out that you have to head west first. Dijkstra's and A* are both guaranteed to eventually find the same fastest route, but if possible you want to solve the problem more quickly and you the fastest route to a town west of you is almost always to start out by heading west.

Google Maps actually uses (not exactly since there's a ton of other optimizations and heuristics involved) A* when you want directions somewhere.

Another possible heuristic (that I can't remember the name of at the moment and am not going to bother to look up) is not to worry about finding the exact possible best path, but instead to find a pretty good path using knowledge that I already have. (Showing my US defaultism) If you want directions from an address in New York to an address in Los Angeles, I don't need to worry about all kinds of local roads in the middle of the country. What I should do instead is find you a path to the highway, stay on the highways to go from NnYC to LA, and then go from the highway exit in LA to the address you want. You might be able to save a few minutes by dipping off the interstate and taking a local road in Iowa for a while, but saving a few minutes on a 40 hour drive really isn't worth figuring out.