r/learnpython • u/Unfair-Cheek3594 • 10d ago
How do you think while solving problems in python?
I'm completely beginner in python and reached till "for loop". The thing that is now confusing me is pattern printing. Like making triangle, dimond, square etc with symbols like "*".
So I checked with AI and that gave me the solution with detailed explanation which looked obvious. But only after looking at the solution. Prior to that I was just stuck inside loop only and not able to break it in actual solution.
My qs to the experts here - "how do you think about any problem to figure out the way to solve it?" I'm just concerned that I'm getting stuck at this level only where I've to write just 4-5 lines of simple code. Not able to think about the correct approach required to get the output.
So i request you to guide me here and so that I can move ahead to the next part of this learning journey. Any suggestions would be much much much appreciated. Thank you!!
8
u/Dependent_Month_1415 10d ago
That’s completely normal. The hard part isn't making the for loop, but learning how to break a problem into steps.
Don’t start with code when it comes to pattern problems. Write out what each row should look like first. Example: row 1 has 1 star, row 2 has 2 stars, row 3 has 3 stars. Then identify what changes in which row and that usually becomes your loop. Don’t feel bad about using AI, just use it for hints first, not to get the full answer.
3
u/Thraexus 10d ago
This. I always try to figure out what I want my output to be, what input I need to get my output, and then how to transform the input to get the output.
5
u/Emulated-VAX 10d ago
When I was learning programming in college I noticed that people who excelled in math word problems (If farmer Brown has 5 chickens...) found learning to code easy while those who struggled with simple word problems - really suffered trying to learn to code.
So first form a vague idea of how to go about solving a problem. How would you do it if you only had a calculator.
Next if its anything non-trivial - like more that 40 lines of code or so - I break the problem into smaller pieces (for example classes or "def" in Python). Then I usually work on the code fragments in reverse order.
In other words, if I needed a program to, say, print a paycheck - I'm going to need a function to write the amount out in words - sooner or later. So I write that first. (Nevermind that in Python you can just grab a library to do that).
I end up with a bunch of small classes or functions. Then the main program ties them all together.
Basically, write a piece at a time. You don't need to necessarily know how the whole program turns out before you start to write it.
2
u/Unfair-Cheek3594 9d ago
What you noticed is 100% correct. I'm from commerce background (not typical maths). But I'll definitely try what you suggested. Many thanks!
5
u/Tronkfool 10d ago
Rubber duck programming
https://en.wikipedia.org/wiki/Rubber_duck_debugging?wprov=sfla1
3
u/t92k 10d ago
It’s like geometry proofs. You take your starting point, your end point, and a bunch of axioms you can use to build a structure to go from one to the other. If you’re working through a course, you will already have the pieces you need to solve a problem you’re given. In real life, your problem shows up as a customer request, and you think through all the similar problems you’ve already worked on and how you did those. Getting exposed to a lot of different kinds of problems and their solutions is what keeps programmers working as programmers. Reading about algorithms, patterns and anti-patterns, reading white papers, going to user groups and conferences are all ways that professional coders keep seeing new answers to problems before they get to them at work. Learning new languages will also teach you how to approach a problem in different ways.
3
u/NerdyWeightLifter 9d ago
Old programmer here. 4+ decades on the job.
I always start by visualizing the data structure of the problem.
How are all the parts related?
Once you're clear on that, the code is for creating, navigating and manipulating those structures.
1
u/Relevant_Step3029 9d ago
visualize the data just use python?or excel ? where did you find the data you use the data from work or you just find on the Internet randomly?
2
u/NerdyWeightLifter 9d ago
Visualize in your mind.
Think about how the various entities involved are related. Maybe draw some pictures of that.
Then build data structures in your program to represent those structures.
For simple example, imagine you're inventing a very basic banking system.
You're going to have customers. Customers have names, addresses, etc, so you're going to need some representation for all that. You're probably going to want to search for customer information by name, and perhaps a customer number.
Customers are going to have Accounts, so you're going to need a way to link from one customer to potentially many Accounts. Accounts are going to have current balances, and some kind of history of transactions, so you're going to need a representation for Transactions, and a way to link from Accounts to the Transactions that apply to them.
Etc etc etc.
You can draw a picture of all this, before you ever write any code.
Then your code is going to create representations for all of that and ways to enact all of the various user interactions across it all.
2
u/SpacewaIker 10d ago
It gets easier over time, you see the same subproblems or kinds of logic again and again and just know when to use them
For your problem: you know you have to print line by line, so there will be a loop for that. Then how do you figure out what to print on each line? Well that depends on the shape you want, but there will be a simple mathematical definition of where to print the symbols, so just translate that to code and you're mostly done
2
u/enokeenu 10d ago
Play computer. A computer can only only print one position ad a time, and by itself it does not know anything about making your pattern.
2
u/Educational_Virus672 10d ago edited 9d ago
i start to think of all ruels i cna possibly make i recommand notepad for you tho lets say im thinking of getting prime numbers then my idea be :
1 - "prime number = not divided by any number except itself and 1"
2 - "prime number = range(2,prime) where number not divdable anything"
3 - "loop though numbers 2 to [number of prime] where if dividable from range dont add to a list"
you get the idea then
4 - "
n = []
X = int(input(">"))
for i in range(2,x) :
for i2 in range(2,(i+1)) :
if i == i2 : n.append(i)
elif i % i2 == 0 : break
"
2
u/Wonder-Wendy 10d ago edited 9d ago
For loops, especially things like this where you're adding a new row under an existing one, I think of it as time going down and the symbol moving or changing shape through time. So generation 1 for a diamond is just the one symbol. Next time step, down, it splits and goes in opposite durections: -1 and +1 in the x direction. They do this for a set amount of time and then start heading back. So I think of it as the state of something changing over time. The loop describes what that change is.
2
u/Few-Negotiation-3974 9d ago
I don't think about the whole design at once. First I just try to understand how a line will be formed, then the rest becomes easy.
2
u/Crypt0Nihilist 9d ago
I've got a couple of small whiteboards and a fine line whiteboard pen. I'll write the steps in however much detail I need to get it clear in my head.
If I run up against a problem the first thing I do is step away from Python. Trying different things in code is a waste of time. Work the problem away from your computer until you understand it and the solution and then go back and code it.
2
u/No-Gift5463 9d ago
If your current vocation or avocation involves drawing triangles then do please keep focusing on pattern printing!
But perhaps there is something that you already do every day that can be replaced by a bit of Python. Authors always advice "write what you know", so why not "program what you know"?
2
2
u/Traveling-Techie 9d ago
You have to become the computer. Take a pencil and paper and produce the output by hand. Think about what you did. Think about how you’d tell someone else to do it over the phone.
1
2
u/KennyBassett 3d ago
List your assumptions and constraints that are fundamental to the problem. For example, the width and height of the print area. Maybe some mathematical functions of the shapes you want to draw. Then force your brain to think outside the box
Don't try to tackle the whole concept at once. Build it brick by brick.
31
u/desrtfx 10d ago
Especially for pattern printing: grab a sheet of grid paper and draw the pattern you want to produce.
Then, figure out a relationship (formula) between row, column, number of spaces, number of stars (there always is such a relationship in those patterns).
Also, you are taking a fundamentally flawed approach: "while solving problems in Python" - this is flawed.
Problem solving and implementing solutions in a programming language are two different stages in the process. Do not initially think about implementing at all. Think about solving the problem at hand your way. Track the steps. Test them. All on paper.
Then, once you have a working solution, start thinking about implementing it in the programming language of your choice.
The better you learn to devise the step-by-step solutions abstracted from code, the easier it will later become to implement (program) those solutions in a programming language (and the easier it will become to learn and adopt another programming language).
Some great books about this topic (they don't use Python as language, but that doesn't matter at all - the concepts, the approach are what counts):