Daniel Sullivan

Ruby, Rails and other things.

Stacks and Queues

| Comments

The following is my take on a classic stacks and queues problem.

Set Up

A queue is like a line at the Apple store. It’s “first in, first out” (FIFO), which means that the item that was put in the queue longest ago is the first item that comes out. “First come, first served.”

Queues have two main methods:
enqueue() : adds an item to the “back” of the line.
dequeue() : removes and returns the next item, or “front” of the line.

A stack is like a stack of records. It’s “last in, first out” (LIFO), which means that the item that was put in the stack most recently is the first item that comes out.

Stacks have two main methods:
push() : adds an item to the “top” of the stack, pushing everything else down.
pop() : removes and returns the top item, moving the next item to the “top”.

Classical Collatz

| Comments

Yesterday at Flatiron I had a mock technical interview and was asked to solve a classic Project Euler Collatz problem. Just in case you are not familar with Collatz, here is the set up for the problem.

The following iterative sequence is defined for the set of positive integers:
Given a number n in the Collatz sequence, if n is even, the next number in the sequence is n/2 if n is odd, the next number in the sequence is 3n + 1
Applying the rule above with the starting number 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1. It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms.
Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?

Fibonacci, Recursion and Ruby

| Comments

What is recursion? Wikipedia says “Recursion is the process of repeating items in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other, the nested images that occur are a form of infinite recursion.”

What is a Fibonacci sequence? In mathematics, the Fibonacci sequence are the numbers in the following integer sequence: 1,1,2,3,5,8,13…and so on. If you want to get the next number in the sequence, you add the two previous numbers before it.

The Fibonacci sequence and recursion are widely discussed in computer science. The goal of this post is not to explain them in their entirety, but to hopefully add clarity to the array of blog posts and writings that already exist.

For me, the hardest thing about solving this problem recursively is it’s difficult to visualize what is happening when you call a method inside itself, over and over. So, the first thing I did was Google. I really wanted to find some type of visual representation of what is happening. I came across this post by Natasha The Robot. If you scroll down you will see a hand drawing explaining the recursive pattern, although it’s hard to read. So I figured I would try to recreate it to bring a little more clarity, which is what you see below.


Geolocating Forms With Uranium

| Comments

If you are looking for a quick and easy way to geolocate a user’s form, check out Uranium. “Uranium offers web page authors the ability to add several convenient JavaScript interactions to the HTML elements on their pages…”

Simple Apps With Sinatra

| Comments

Sight Word Generator

This project is more or less a sandbox where I can test out new skills and concepts that we are learning here at Flatiron School. I tend to absorb material better when I apply it to projects that come from a personal place, so I figured I should stop waiting for the perfect time, or perfect amount of knowledge and build something of my own. I definitely have a stronger sense of intrinsic motivation when building my own projects as opposed to following tutorials or excercises built by someone else, but they each have their own place in the learning spectrum.

Adding Disqus to Octopress

| Comments

After a few days of deliberation, I finally have my Disqus comments loading properly. There are a number of tutorials and blog posts on this topic already, not to mention the help section at Disqus, but for those still struggling, this may be your solution.