I wanted to know what are the good ways to understand how algorithms work, how to create your own algorithms, and how to think like a programmer.
understand how algorithms work
I think a better approach is study a specific algorithm using books, papers, blog posts, courses, etc. . A better approach if you definition of an algorithm is “a finite sequence of well-defined instructions”. You always have the option of try to recreate the “classic algorithms” from scratch … but I think that is a really difficult task.
how to create your own algorithms
I think that there is a misconception that make learn how to program really difficult:
the idea that the code the programmer writes is an algorithm.
The code is not an algorithm, the code is an implementation of an algorithm.
Identify the code with the algorithm is problematic, an example: most people doesn’t have problems understanding a recursive algorithm, but they have problems implementing the algorithm in JS. Why? because JS uses a
call stack and
nested (function) calls, things that the algorithm doesn’t have.
You can create an algorithm without write a single line of code. Most of the time is better to use pen and paper and not use a programming language (because programming languages are difficult to use).
how to think like a programmer
I think that most of the time programmers use a
heuristic approach to problem solving.
An example of heuristics problem solving using the exercise “DNA Pairing”:
Problem Explanation, (exercise “DNA Pairing”):
There are four potential characters that exist in DNA: “A”, “T”, “G”, and “C”.
“A” and “T” are always paired together, and “G” and “C” are always paired together.
Heuristic/rule of thumb:
Forms of data descriptions:
- collection with a finite number of element
- lists every single piece of data that belongs to it
- collections of elements that satisfy a specific property
- collection with infinitely many elements
- specifies a range of data
- specifies ranges in one clause of its definition
- and specific pieces of data in another clause
If we read the
problem explanation we can try to solve the exercise using an
The main idea of an enumeration is:
- it defines a collection of data as a finite number of pieces of data
Each item explicitly:
spells out which piece of data belongs to the class of data that we are defining. A data representation in which every possibility is listed
When a function’s input is a class of data whose description spells out its elements on a case-by-case basis:
The function should distinguish just those cases
and compute the result on a per-case basis
To distinguish in code requires:
- conditional functions: functions that choose different ways of computing results depending on the value of some argument.
Example of a conditional function and enumeration:
; TrafficLight -> TrafficLight
; yields the next state given current state s
(define (traffic-light-next s)
[(string=? "red" s) "green"]
[(string=? "green" s) "yellow"]
[(string=? "yellow" s) "red"]))
In JS you can use
Why is a rule of thumb:
Because “The function should distinguish just those cases and compute the result on a per-case basis” is not an algorithm, is just a “recipe”/recomendation that you can use (the exercise can be solved in multiple ways).
Cheers and happy coding
 Algorithm - Wikipedia
 Heuristic - Wikipedia
A heuristic … (from Ancient Greek εὑρίσκω (heurískō) ‘I find, discover’), or heuristic technique, is any approach to problem solving or self-discovery that employs a practical method that is not guaranteed to be optimal, perfect, or rational, but is nevertheless sufficient for reaching an immediate, short-term goal or approximation…
… Examples that employ heuristics include using trial and error, a rule of thumb or an educated guess.
 freeCodeCamp Challenge Guide: DNA Pairing
 I Fixed-Size Data
 I Fixed-Size Data