Posts

Showing posts from December, 2020

Big O Time

Image
Here's a great way to approximate the Big O time of your algorithm: just run it with differently sized inputs. That's right, that's all you have to do* (*there's more to it, we'll get to it in a sec).  You don't need to follow your inputs around, count any loops, look for best case and worse case scenarios.  You can get a pretty good approximation by following the steps below. 1.  Speed test your algorithm .  Make sure you have at least 3 inputs - if you're working with an array, make sure there's 3 elements, if you have a string make sure there's at least 3 letters, etc.  Larger inputs will make this test more accurate.  Note down how fast your algorithm was. 2. Double the number of inputs. 3. Speed test your algorithm again.  Got a number? Good. 4.  Now, divide the second speed test result by the first one. Once you've got that ratio, you're good to go!  Simply compare your ratio to the following chart: Ratio Big O time approximately 1 O(1)...

Djikstra's Algorithm

Image
We can't all be graph theory geniuses like my friend Nathan Klein , but...we can learn how to make graphs in JavaScript. Source:  Wikipedia This is what mathematicians refer to as a graph.  In a graph, nodes (the circles above, numbered so we can distinguish them) are linked together by vertices (the lines).  Graphs can be used to store data and relationships, and they can also be used to simulate problems, such as in the  Traveling Salesman Problem , which asks you to find the most efficient route through a bunch of different cities. Quite often, we need to find the shortest path from one node to another (such as in the Traveling Salesman problem. It's a great problem).  What's the best way to do that? Djikstra's algorithm. Dijkstra’s algorithm  has a Big O complexity of log n, which is super efficient.  How does one do Djikstra's algorithm?  It's simple: you start at one node, and then move to the next node that has the shortest distance.  ...

Type

One of the first thing a programmer learns is the various "types" that data can have.  Commonly-used types include things like strings, integers, and arrays.  Underneath the hood, all software data is, of course, composed of bits commonly represented by sequences of ones and zeros.  But typing gives structure to those bits, and not only allows programmers to understand and reason about the code they write (it makes more sense, for instance, to think of a string as letters or words rather than bits), but also prevents errors through creating restrictions on what certain types can and cannot do (for instance, you can add floating point numbers but you can't add arrays). There are two major axes along which typing varies: a language can be either strongly or weakly typed; and a language can be statically or dynamically typed.  You can loosely think of these, respectively, as a language's ability to prevent type errors, and when a language catches type errors. Static vs....