I had to run these commands in Ubuntu (13.10) terminal after installing my LAMP stack, composer and mcrypt to get it to load up:
Finally got it working!
Writing HTML in Sublime 3 can be slow if you dont have the ‘View In Browser’ Plugin installed. To get this going it take a few steps:
1) Copy and paste the below code into Sublime’s console (View -> Show Console) and then hit ENTER to exectue it:
2) Once that has install you can then press CTRL + SHIFT + P on PC or CMD + SHIFT + P on Mac. This will open up the package command palette.
3) Type in the word install and the command palette should filter down to the command Package Control: Install Package. Select this command by either clicking on it, or using your cursor keys to highlight it and pressing Enter.
4) After that finishes you press CTRL + SHIFT + P again and start typing view in browser and the list will filter as you type. Select the plugin by clicking on it or using your cursor keys to highlight it and pressing Enter.
5) CTRL + ALT + V will now open your code in the browser
An algorithm is a clear, step-by-step sequence of instructions for performing some task.
We dance with algorithms at every step of our lives. In fact, stepping is one of the many primary autonomic algorithms our bodies’ perform for us everyday. Still even these algorithms can be broken down into tinier and more simplified instruction subsets.
Lift leg upward, move leg forward, angle foot downward, lean body forward, press leg down. As these motions become ingrained in us we adapt and become able to take a bigger steps, backwards steps or extend this basic functionality and do multiple tasks while stepping. This is one of the fundamental concepts of programming and I’m going to explain one that I encountered recently during a job interview.
Traditionally, there are 9 steps used when developing an algorithm:
1. Problem definition
2. Development of a model
3. Specification of Algorithm
4. Designing an Algorithm
5. Checking the correctness of Algorithm
6. Analysis of Algorithm
7. Implementation of Algorithm
8. Program testing
9. Documentation Preparation
Let’s start by defining the problem. We will begin by imagining an auditorium with many seats. The seats are numbered and visitors are picking out their own seats as they arrive. Eventually the auditorium begins to fill up and we want to place new visitors in the remaining seats starting with the most empty sections. Our algorithm should assign a new visitor with a seat number which will be the seat number with the most empty seats on either side of it.
Now that we have defined the problem we can begin developing a model to solve it. The model in this situation will be to scan the entire auditorium for empty seats. We will then rank each one based on how many seats are empty on either side of it and return the seat number with the highest ranking. We can model it by using human language with steps like these:
- For every seat in the auditorium
- If it is empty
- Increase its rank
- Look at every seat to the right of the current seat
- If any are empty then increase the right ranking of the current seat for each one that is empty
- When you find a seat that is full to the right then stop looking to the right
- Look at every seat to the left of the current seat
- If any are empty then increase the left ranking of the current seat for each one that is empty
- When you find a seat that is full to the left then stop looking to the left
- If there are an equal number of empty seats to the left and right then increase the current seats rank
- If there are no empty seats to the left or right decrease the rank
- Add up the left and right ranks to the current rank to get the total rank
- Put the current seat number and its rank on a list
- Reset the rank for the next seat
- Goto the next seat and perform the prior instructions until all the seats have been looped and included on the list
- Sort the list from highest ranking to lowest ranking
- Give the vistor the highest ranking seat number to sit in
Using this model we can further elucidate the specifics of the algorithm. We’re going to use two arrays to do this. The first array will be a boolean array representing whether each seat is occupied or not. We will want a second array to track the seat numbers in the boolean array and their rankings.
Now for designing the algorithm. First, let’s create a simplified version of the boolean array. We want to know what the correct result is before writing the algorithm so that we can test its accuracy.
With this array our goal is to return 3 because seat number 3 is the seat with the most empty seats surrounding it. Now let’s initialize the other variables we’ll need and define our function:
The rankList array will be used to store which index we are currently at in the for loop and it’s rank. The emptyLeft and emptyRight variables will store a count of how many empty seats there are around the current seat. The rank and finalAnswer variables are for storing the rank and the result. Now we can make the ranking for loop.
If we combine all of the pieces together we can test our algorithm using the browser console. In most browsers you can hit F12 to access the console. You can copy and paste the code into console and run it to verify that the result is 3.
After you’ve validated that the algorithm is returning the correct answer to the problem you will want to test it with other variables. You can do this by making a bigger booleanArray:
Running the findBestSeat function with this array instead will return a different seat. Look at the values in this array you’ll find that it is correct. You can view what these values are during runtime by inserting a console.log command before the rankList array push with:
Those are all of the initials phases of the first 5 steps in designing an algorithm. From here you can analyze the algorithm and test it with different use cases. You’d be then plan implementation and actually print out a ticket with the seat number. Then follow that up with documentation and sharing it with the world. I’ll be covering those in part 2.
Silex is a concise, testable and extensible PHP micro-framework based on Symfony 2. In a nutshell, you define controllers and map them to routes, all in one step.