Posts

Showing posts from November, 2020

The Singleton Pattern

Image
Welcome to the second in a series of posts about anti-patterns in Ruby ! What is a singleton?  A singleton is a single instance of a class, particularly  a single instance of a class - to be a singleton, a class must have the property of only being able to be created once (note that a singleton could not be created yet, so a singleton doesn't have the property of necessarily existing). Why use a Singleton pattern? * You have a single resource that you need to model - for instance, a log file. * You need quick access to a particular resource throughout your system and don't want to have to remake it every time. * Having more than one of a resource might be dangerous in your system - for example, if you're designing hardware related to a physical system, it may cause serious problems if you have multiple instances of an object floating around and different parts of the software are making calculations from different representations of a real-world machine part. * ...other rea...

The Sorting Hat

Image
Here's a lesson about the difference between theory and practical applications. Everyone knows about  bubble Sort  and  merge Sort  - they're probably the first two sorting algorithms you learned.  Everyone also knows that (1) bubble sort sucks and (2) this is due to the fact that its average time complexity is O(n^2), aka the worst time complexity.  It even generally performs worse than things like insertion sort and  selection sort , the "a D- is still a passing grade" of sorting algorithms. While all these things are true, there's a more important truth: always test your code. While reading  The Imposter's Handbook by Rob Conery  the other day and flipping through the comparison sort algorithms he provides, I decided, Why not actually test out this code?   And, the  speed test demon  that I am, my test involved sorting out his [23,4,42,15,16,8] array one million times with bubble sort and with merge sort, and I got this res...

Do Not Pass Go

When I was in middle school, I found a bunch of books at my local library that explained how to create games for the Atari console in  BASIC .  This was my first introduction to coding. BASIC is a great first language to learn to the extent that, as its name implies, it is an extremely basic programming language and is easy to understand (as you'll be able to tell after reading this post, it is not a good way to learn how to program in general, at least if your goal is to use modern programming languages). BASIC is also based off of  FORTRAN II , which is of course based off of FORTRAN, which is the language that invented the GOTO statement. The GOTO statement takes its inspiration from the most fundamental of computers, the Turing machine.  Turing machines are an abstraction in which a head reads and writes ones and zeros from an infinite tape of individual cells. The head moves back and forth along the tape based off of a finite list of instructions and what those ...

Making Logic Gates in JavaScript

Image
Everyone knows that computer programs are made of bits - the ones and zeros that hold all the information that keeps our modern life running. But how do you get from numbers to programs - how simple ones and zeros do  anything? The answer is logic gates (as well as a whole bunch of stuff about how electronics are built - you need to actually make these logic gates on out of circuits in order for our logic structures to be enacted; but I'm a programmer, not an electrical engineer, so let's not go there).  A logic gate is what's known as a  Boolean function , a mathematical operation that takes in bits and spits out more bits. Each logic gate takes in two bits of information (as mentioned before, generally denoted as a 1 (also sometimes represented as true ) or a zero (also sometimes represented as false )), so there are four possible inputs to a logic gate: {0, 0}, {0, 1}, {1, 0}, and {1, 1}.  For each of these inputs, a logic gate can spit out either a 1 or a 0, so t...

Why binary search is the fastest search

 dadf

The Giving Trie

Image
 Alternative Titles: A Trie Grows in Palo Alto The Trie of Life The Best Time to Make a Trie is Twenty Years Ago, the Second Best Time is Now The Trie of the Knowledge of 1 and 0 If a Trie Fails and No One Sees the Error, Does The Code Compile? What is a Trie? Source: Wikipedia A trie (generally pronounced like "tree") is a searchable tree that has individual letters for nodes.  So in the example above, we have the following keys associated with their values: "to": 7 "tea": 3 "ted": 4 "ten": 12 "A": 15 "i": 11 "in": 16 "inn": 25 Keys are only complete when you run into a value - so just "t" or "te" by themselves do not constitute keys for this trie. Tries are sort of a niche data structure, but they do have some advantages: the lookup time for a trie will only ever be at most the length of the longest key.  In comparison to a binary search tree - which can at most have only 2 s...