How can I improve my algorithm thinking?
I struggle trying to create simple algorithms. For instance, I would spend almost an entire day trying to figure out how to print an inverse right triangle with asterisks in my computer programming class while my fellow peers would solve it in 30 mins. #computer-science #programming #java #critical-thinking
7 answers
Daniel’s Answer
If it's truly algorithms you're struggling with, then two things come to mind:
1) Study more math. Algorithms is inherently more of math problem than a programming problem.
2) Get a good algorithms book and learn from it (e.g. CLRS is the canonical textbook).
On the other hand, if it's basic programming and not the algorithms that trips you up, then the best advice is to practice more, and learn how to break things up into tiny pieces. So for printing a triangle, my thought pattern would go something like:
1) can I print anything to console? If not, Google how to print stuff in whatever language
2) run a basic program printing one line with at least a couple of just to see it work.
3) do I know how to concatenate strings or print single character? If not, Google how to print single character or add strings together
4) figure out how many spaces and to print on a given line (should probably be some math formula, see above)
5) try a lot of for loops
mathias’s Answer
A key here is breaking the problem down into manageable pieces that you clearly know what to solve.
e.g. First step, print a single asterix on the screen -- basic, but your first victory
Second step, make a loop to print 10 asterixes
Third step ...
As you complete each step you only have to formulate a new task to take you closer to the final goal. You don't have to solve all remaning items in the next iteration.
The more you work like this, the more it will become automated so that a year you'll pack each "small" step with lot's of single steps that initially were too complex to bundle at the time.
AKSHAY’s Answer
As per my experience i divide this into 2 category:
1. Focusing on understanding the ideas very deeply(Depth Approach)
To start with this you need to focus on a few representative problems lets say 100 to 200, Solve them a couple of times and you’ll start seeing
patterns. You also start getting better at the coding part. Once you achieve this you will be able to :
> You can code the usecase quickly.
> You can apply the same code to a new problem quickly .
> You know the data structure you are using and can implement it .
Once you are done with this pls revise the core sets of algorithm i strongly recommend "Cracking the Coding Interview".
2. Focusing on solving as many problem as you can (Breadth Approach)
Here you can now look beyond your core set of problems. Because you’ve implemented so many techniques already and gone through core
sets of algorithm, you don’t even have to code all the new questions.BUT Practice the sudo code on PAPER if you do so you are forced to plan
your code before writing!.
-
Akshay
AKSHAY recommends the following next steps:
Earl A.’s Answer
Hi Paul,
Programming is often a "divide and conquer" task. You usually can't jump from the problem to finished code in one step.
Write the steps of the problem in English (or your native language) or a pseudo-language, and then translate that into code/language.
Trying to work out the algorithm and the coding in one step can be intimidating at first. Eventually, coding into the language becomes more fluid and natural, but for now, break it down into steps.
But first, make certain that the problem itself is well defined.
So, for your example, I'm assuming that an "inverse right triangle" is one with a wide, flat side up and the acute angles pointing right and down. (I would call that "inverted" triangle.) I'm assuming that "inverse" means that it should be filled with asterisks, not just an outline. (NOTE: the problem needs to be even more well defined than I have done here! Also, clear up the assumptions!)
But moving on to the next step, write out the steps in your most fluent (human) language:
--- start ---
Set the number of asterisks to the desired length of the first side
Start a loop to print the triangle
Print a string of that number of asterisks
Decrement the number of asterisks
Loop until the number of asterisks = 1
--- end ---
Now, with the problem broken down into discrete steps, you can translate into code.
I hope that gets you headed in the right direction.
Peace, Earl
Gautam’s Answer
> Then algorithms require practice and getting yourself familiarized with a wide variety of problems. So, once you start practicing, you will slowly start to come up with better solutions.
> Try to solve a problem in different ways or using different data structures and algorithms and understand the trade-offs between time (speed) and space (memory).
> When you're stuck with a problem, before looking at the solution, try to at least think of a brute-force way of solving the problem. Then think of how you can optimize it.
Juwan’s Answer
How about just try to jump on more and more algorithm problems available out there such as https://leetcode.com/.
There are also numerous of various solutions provided by other people for each problems.
Delete Comment
Flag Comment