Breaking Down Problems Into Ever Smaller Parts, Smaller and Smaller
Breaking Down Problems Into Ever Smaller Parts, Smaller and Smaller
CodeWars
When approaching a new task: "start by writing the "codewars problem" that you are trying to solve.
So what the requirements are; and then, think about the inputs, and the outputs.
So every time I am breaking down a feature problem I want to think: what is the codewars problem here? And if that is too big I want to think, what are the aspects of this feature? What are the components? And then once I have the feature broken down into small components, I can start to think about: what are the codewars problems too? What are the inputs and the outputs?
"Remember that comments can represent toasters with inputs and outputs"
"Knowing the types of the inputs and outputs really helps."
I sought further feedback and guidance about this
And a comment can also include something like a requirement.
Adding Comments
Even if we want to break down the steps, it might be helpful to add some extra comments, or even more, then, about how each step is implemented and what we think that we might be able to do/want to do/should do.
"Comments describe overall parts of the algorithm and each part can be thought of as a toaster with inputs and outputs. The types of inputs and outputs are very useful for understanding how to implement a toaster. Knowing that the input type is X, and the output type is Y, helps to remove any ambiguity."
One thing that you can do is that you can add in the requirements into the comments
def send_favorite_color_email_to_users() -> None:
# get users from the database
for user in users:
# send email
# - email must be sent to the user's email address
# - email must contain the user's favorite color
Can you see the two requirements that we have listed there? The email must be sent to the user's email address; and the email must contain the first one.
- Things that sound obvious are not so obvious in software
- Yes of course the email has to be sent to the users email address - but what if you were the one who is literally building the software that sends the email? Well that is us! Not so simple anymore...
"Codewars problems range in complexity. A 6 kyu codewars problem may contain multiple Level 7 and 8 kyu codewars problems to solve."
"These are toasters of varying levels of complexity."
"The way you are approaching 7 kyu problems by writing the algorithm in comments and gradually turning them into pseudo-code and real Python applies to every complexity of problem."
A big problem will contain any number of smaller problems. If a problem is very complex, drawing a diagram of the high-level and smaller problems is a good starting point. "Keep breaking it down" until they are much smaller problems.
"Then each one can be solved with the comments -> pseudo-code-> Python code approach."
This is the important part
The approach should be more or less like:
- Write comments
- Write pseudo-code
- Write Python code
- pseudocode can also start to add things like:
- defining variable names
- adding control structures
- like if statements
- or for loops
- which is not quite valid Python yet
- but this is before thinking about whether or not the code is valid Python
"The comments are a translation of how you solve the problem mentally - they're your mental algorithm that you come up with for solving the problem."
"Then the comments get translate into the end result which is a working implementation."
What about drawing diagrams, how about those, should I be drawing them?
"Drawing diagrams is a great idea and I'd start with a diagram if the problem is too big."
"Then you can take parts of the diagram which are small enough to start writing comments for, and then turn that into code."
"Breaking it down in that way results in solving complex problems in small steps."
A big problem will contain any number of smaller problems. If a problem is very complex, drawing a diagram of the high-level and smaller problems is a good starting point. "Keep breaking it down" until they are much smaller problems.
"Then each one can be solved with the comments -> pseudo-code-> Python code approach."
I love the wind farms and I love the pylons
I love the wind farms and I love the pylons
Everything I do I do it for the pylons
Everything I do I do it for the deep deep love of the forest.
I find love in the forest
A few bangin' quotes for you today:
- "When everything that can be let go of is let go of, what remains is what we desire above all else."
- "In ignorance, I am something; in understanding, I am nothing; in love, I am everything."
- Both are from The Ashes of Love, by Rupert Spira
Codecademy is my happy place
- Intermediate Python
- Django
- Advanced Python
Comments
Post a Comment