Good ways of learning algorithms? Your advices?

Hello @ahmedev,

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”[0]. 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[1] approach to problem solving.

An example of heuristics problem solving using the exercise “DNA Pairing”[2]:
Problem Explanation, (exercise “DNA Pairing”)[3]:

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[4]:

a. enumerations
- collection with a finite number of element
- lists every single piece of data that belongs to it

b. intervals
- collections of elements that satisfy a specific property
- collection with infinitely many elements
- specifies a range of data

c. itemizations
- 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 enumeration:

4.3 Enumerations[5]

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)
  (cond
   [(string=? "red"    s) "green"]
   [(string=? "green"  s) "yellow"]
   [(string=? "yellow" s) "red"]))

In JS you can use if...else[6] or switch[7] statements.

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 :slight_smile:


Notes:
[0] Algorithm - Wikipedia

[1] 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.

[2] https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing

[3] freeCodeCamp Challenge Guide: DNA Pairing

[4] I Fixed-Size Data

[5] I Fixed-Size Data

[6] if...else - JavaScript | MDN

[7] switch - JavaScript | MDN

1 Like