Category: coding

National Novel Generation Month, 2017 Edition

November is traditionally “National Novel Writing Month.” The goal is to write a short novel that is 50,000 words in length. I always have grand plans to attempt it and have started a number of times over the years but have never actually finished. (One day, I swear!)

Recently, I stumbled across a geekier take on it, called National Novel Generation Month. The goal of this particular project is to write code that can generate a 50,000 word novel instead. Hey, why not?

I published my code at the beginning of November for my project: The Complete Encyclopedia on 1,449.5 Random Ways to Make a Sandwich.

This book of 1,449.5 random sandwich recipes was created for NaNoGenMo (National Novel Generation Month) 2017. You can view the source code for this project on GitHub.

It uses data parsed from a 1909 book, entitled “The Up-To-Date Sandwich Book: 400 Ways to Make a Sandwich”, written by Eva Greene Fuller and now available for free in the public domain.

For this particular book, new sandwich recipes were generated using Markov chains created from the above text.

Please don’t try to actually make any of the sandwich recipes created with this process. However, if you do, please contact me and show me pictures.

Disclaimer: I cannot be held responsible for any health issues that may arise from eating any of these sandwiches.

There are definitely some interesting ones…

BUTTERED CHEESE AND OLIVE SANDWICH NO. 3

Use three slices of Swiss cheese, spread fresh butter and two tablespoonfuls of olive oil, the juice of two oranges and knead the mixture.

Want to learn to code?

A friend of mine recently asked for some suggestions on which language she should use to learn to code.

There are so many different routes to go! Python and JavaScript might be easiest because they’re dynamically typed languages (basically they’re more forgiving with how you use variables and pass them around — it’s one less thing to worry about as you start out).

I find Python to be super fun and easy to pick up. Plus there’s tons of neat libraries available for manipulating data. One bonus: down the road, you can start playing with some of the many machine learning libraries that are available. Then you can build a model that will predict names of Android phones.

I’m partial to JavaScript. At this point, it’s probably one of the most popular languages right now. People are building web apps, desktop apps, native mobile apps, and backend servers with it due to the abundance of tools available. Plus, there’s a really addictive feedback loop with it: when you’re first starting out, you code something, refresh your browser and boom, there it is!

I’ve played only a little bit with Swift. I like it and I think Apple is doing some good work trying to provide tools to help people learn. For now, you’re mostly going to be limited to building mobile apps, though there are more tools being built that expand its uses (e.g., servers).

There are tons of great resources. Codecademy, Code School, Udemy, free tutorials, etc. When I started out, I started learning by trying to build a JavaScript app that could solve Sudoku puzzles. Somehow, I eventually did it! I was pretty hooked!

Emoji Say What?

Here’s a random little side project that I’ve been working on: Emoji Say What?

It’s like a game of telephone, but using the latest in human communication technologies, hieroglyphics, emoji!

Basically, you visit the site and get a completely out of context sentence or set of emoji and it’s your job to decipher it. And so on and so on. It evolves over time and eventually you get something like this.

Using neural networks to generate names for craft beer.

I’ve been on a machine learning kick lately. Given a large enough dataset to train with, it’s really interesting to see what a neural network can come up with.

This week, it’s names for craft beer.

If you’re a fan of IPA beer, you’ve got names like Dang River, Yamquak, Yall in Wool, Wicked Geee, Yampy, and Oarahe Momnila Day Revenge Bass Cornationn Yerve Of Aterid Ale. Like strong pale ales? Trippel Lock, Third Maus, Third Danger, Spore of Gold and Drammnt. Stouts more your thing? Look for Sir Coffee, Shock Slate, Take Bean, Black Sink Stout, Shrump, Avidberry, or Cherry Trout Stout.

Naturally, I tried to create my own model using a Python library called Keras and a dataset of 7,500 craft beer names.

…I should leave this stuff to the professionals.

Update: Kaggle has a new tutorial teaching you how to do this exact same thing. Neat!

Setting up tests using Tape

Test driven development has become an important process in the software engineering world. It allows coders to develop functions by first creating a series of tests that the new function must solve. The benefit of this is that once your app grows more complex and you add new functionality, you can see if any existing tests have failed, meaning that something broke (and now you know where to find it). Look no further than any popular project on Github and you’ll often see a “tests” folder.

Today, we’re going to talk about setting up tests using Tape.

Tape is an alternative to popular testing suites such as Jasmine and Mocha. Like any tool related to software engineering, there are some developers that strongly prefer Tape over other options. It’s fairly easy to setup and can easily be run in automated task runner tools such as Grunt and Gulp.

To use it as part of your project, you can install it through npm:

  npm install tape --save-dev

Once it’s been added as part of your project, you can create a new tests.js file and require the module.

For our demonstration, we’re going to write a simple test that checks if my name is Dave, plus a few additional parameters.

Start off by setting up your test.js file like so (you can name it whatever you prefer). I’ve commented the code for some additional clarity on what’s happening here.

// Require the Tape module imported from npm
var test = require('tape');

// Write your tests in the code block
test('All about Dave', function (t) {
    // The number of tests that you plan to run.
    // NOTE: If this number doesn't match up with the number
    // of tests that are run, your test will fail.
    t.plan(2);
    
    // Let's setup some variables to test
    var name = "Seymore";
    var city = "Oakland";
    var favBaseballTeam = "Athletics";

    // This test will check for my favorite baseball team.
    // The first parameter is the result, the second is
    // the value you're expecting, and the third is the message
    t.equal(favBaseballTeam, "Athletics", "Favorite baseball team should be Athletics");

    // This test will check for my name.
    // As you can probably assume, it will fail.
    t.equal(name, "Dave", "Name should be Dave");

    // This test will check if city has been set:
    if (city) {
      t.pass("City set");
    } else {
      t.fail("City not set");
    }
});

That’s it! You can run Tape from your terminal and point it to your newly created test.js file in order to run it.

Screenshot 2015-09-01 14.00.18

Using the Mongo CLI to find your data.

We’ve been working on many projects lately that have utilized MongoDB as the primary means of database storage. I have previous experience building and using MySQL databases, so the idea of these NoSQL databases is a new concept for me.

I’m not one to shy away from new technologies, so I’ve been trying to embrace MongoDB and learn how to use it.

One of the most important things I’ve been learning is how to view the databases, collections, and records that I’ve saved in my various applications through the MongoDB command line interface.

Let’s do a quick walk through and pretend I have a database dedicated to baseball.

Once you have Mongo installed on your machine, you run the interface by typing mongo in your terminal. Now, you can bring up a list of databases by typing show databases.

Screenshot 2015-08-20 10.24.51

How do we use a particular database? Easy! Just type use [database_name]


use baseball

Awesome! Of course, you’ll want to do more than just “use” the database. We want to see what’s inside it. This is accomplished by telling mongo to show us all collections (e.g., think of these as “tables” in a traditional SQL database).


show collections

Screenshot 2015-08-20 10.27.33

Awesome! Now we have a collection of teams and collection of players. Well, let’s display everything within a particular collection. In this case, let’s print out all teams that we have stored in our database.


db.teams.find()

Screenshot 2015-08-20 10.28.32

Great!

Now, let’s say you’re looking for a particular record. How do you limit your search to just one thing? Like this:


db.teams.find({team: “dodgers”})

Screenshot 2015-08-20 10.30.04

Now, you can imagine that if we had more data, there are a lot more things that we could search for and find. It’s pretty powerful!

Anyway, this was a quick tutorial on how to use the Mongo DB CLI. I hope you found it helpful!

Getting started with Firebase, by building a persistent chat client

Screenshot 2015-08-07 14.05.09

I recently utilized Firebase to power the database and back end of an AngularJS web application I’ve been working on.

Firebase was proved to be amazingly easy to setup. Their “5 minute quick start” sold me on the utility of this service.

How easy is it? Let’s go ahead and create ourselves a persistent chat client in about 5 minutes. I’ll assume you’ve had some previous experience with Angular, but it’s not required.

In an empty HTML page, let’s add the Firebase Javascript library in the head section of our page.

<script src='https://cdn.firebase.com/js/client/2.2.1/firebase.js'></script>

Alright, easy enough, right?

Now, you need to create a reference to your Firebase database inside your script tag. When you sign up (for a free, even!), Firebase will give you a random URL that links to your new database.

So, a reference in your script tag, like so:

  <script>
    var myData = new Firebase('https://d______.firebaseio.com/');
  </script>

Okay, okay. Not too bad.

How do you start sending data to your new Firebase DB? You just apply some Firebase methods to the reference we created about. In this case, we’ll use the set() method.

  // Store values from our input boxes here.
  var name, text; 

  // jQuery or other magic here to set input boxes
  // to the values above.

  // Save to our Firebase DB using the .set() method.
  myDataRef.set('User ' + name + ' says ' + text);

You’d likely use jQuery to set the values of a particular input box to the name and text. You can also pass in objects to the set() method like so:

  myDataRef.set({name: name, text: text});

Another option that you can utilize to store data inside Firebase is the push() method. You can utilize this method to easily create lists of objects.

  myDataRef.push({name: name, text: text});

Okay, we talked a lot about how to send messages to our database. But how do we retrieve them? We can create an event listener using the on() method, like so:

  myDataRef.on('child_added', function(snapshot) {
    var message = snapshot.val();
    displayChatMessage(message.name, message.text);
  });

And just like that, you now have a working chat client supporting many users and can share data to all clients using it. Amazing!

Hack Reactor: Day 15 – Strategic Partner Picking

First off, getting home early last night was amazing. The sun was still up when I left class, Kerry was still awake and in the living room, and even Benson was finally happy to see me. After unwinding (and meditating) I went to bed at around 9:30PM and only ended up waking up once (from around 3:30AM to 4:15AM).

But I still managed around 7 and a half hours of sleep! And even got up for a 3 mile run with Benson.

Basically, I did everything possible to try and ensure that this would be a good day. And you know what? It was!

So, knowing that our instructors said that “this week is going to suck,” I decided to be a bit more strategic about picking my partner. I sent a message to the young kid who gave a presentation last week on building a ray tracer, and asked if he wanted to pair up. He said sure!

I’ve wanted to work with him for awhile. He’s just absolutely brilliant and probably the smartest person in class. After hitting so many walls last week, I thought it’d be interesting to work with someone who is able to pick up these new concepts easily. I suppose it sounds like I’m trying to get someone to “do my homework” but that’s not it. I ensured I got as much out of it as I could. Anyway, more on that later.

We started with our usual toy problem. This time, navigate a tree and return all elements using a “depth first search”. I don’t entirely understand what that means, but we were basically traversing a tree and returning all values that matched some criteria as a flat array.

I didn’t haven any trouble building a recursive function that could traverse a tree (amazing!), but I had a hell of a time trying to apply a provided filter function to my array. I just couldn’t get it working and only got 4 out of 7 possible points.

Ah well. After this, it was our sprint reflection / feedback session. It turns out, this is one of the last times Hack Reactor is doing this because it was happening so frequently and students weren’t finding much value in it. I thought there was a good compromise, like maybe once a week or so, but ah well. I guess our lecture schedule is getting bumped around and this will give us more time to code! Yay!

We went to lecture and had a brief overview of the Model-View-Controller architecture in general. I think most of us understand it from a high level. It’s getting down and dirty and figuring out where things get plugged in that get crazy.

After lecture, we had some time to explore the problem on our own. It took me 45 minutes of looking at documents and source code to even get an idea of what we were going to work on. I was getting a bit worried. This was going to be intense.

Lunch involved people asking me where I was going (cheap Bahn Mi sandwiches nearby) and 5 others coming along. Hah!

We came back from lunch and had another 45 minute lecture on “The Secrets of Backbone JS” which gave us some hints to get started. And then it was time to pair up! We claimed a workstation and chatted about our expectations for this sprint. The goal was to build a playlist / queuing system for a music web application.

I told him that I didn’t feel completely comfortable with the material so far, but I wanted to do everything I could to get a better understanding of it. He was completely onboard.

One of the first things we had to do was draw out a “system architecture diagram”, which kind of explained how our app was supposed to work. By the time I was done drawing it out, it looked like a drunk person tried to make a circuit board. I have some work to do there.

We started off with me navigating (so I was telling him what to do) and basically kept those roles for the rest of the day. When I would get stuck on a problem, he would gently prompt. “so, where have you seen that function used before? What do you expect to happen here? Can you explain to me exactly what is happening here at line 57?”

It was awesome. He was patient, enthusiastic when I figured something out, and knew how to ask questions that didn’t give away the answer. We made some pretty significant progress!

Things started to slow down right before dinner — I guess I was hitting a mental wall. For dinner, we split up, I went to the mall to get a mean bowl of soup. (Man, that sounds so sad that we often resort to going to a food court for meals — though other people in our cohort often go to a nearby Subway. Uggggggh).

The after-dinner lecture was given by a fellow student who recently graduated college (so many young kids around here — feeling old over here in my 30s!) and shared her college thesis project, which was studying the rise of Bitcoin in Argentina and whether it could be used as an alternative currency compared to the Argentinian peso. It was an interesting premise, though I have to admit that I have less than flattering thoughts about Bitcoin.

We went back to our workstations and made some significant progress again. It’s nice to step back from a problem for a bit and come back to it later with fresh eyes and a fresh mind. By the time 8PM rolled around, we had finished all the basic requirements. It was amazing!

I feel like I really learned a lot about how this particular MVC library works, and it’s going to be very helpful going forward. I’m not sure I could build an application from scratch right now that utilizes this architecture, but I’m sure that will come in due time.

After hours, we spent some time helping out our neighbors who were trying to get their own web applications to work. It was fun to see my partner in action, as well as get to test my own chops and explain how this stuff was supposed to work to others.

I spent another 45 minutes or so working on the day’s toy problem, trying to get my filter function to work, and I’m not sure what’s going on. I ended up leaving the building at about 9:30PM. Tomorrow, we’re going to work on some of the “extra credit” portion of this sprint and try to build in some new functionality. Also, I’m going to go climbing with a few people from class during our extended lunch. I haven’t done that in ages, so I’m looking forward to it!

Hack Reactor: Day 14 – ZZzzzZzzzZzzzzzz

After getting home and going to bed last night, I woke up about 4 or 5 times throughout the night after dreaming of various whiteboard problems. It felt like my mind had an actual electric current going through it and it was just buzzing with nonstop activity. Needless to say, I’ve been absolutely wiped out today. This explains why it’s 8:15pm and I’m heading home on BART. The sun is still literally above the horizon right now. (Interestingly, I didn’t meditate last night, so it might be a good experiment for tonight.)

After taking BART to school this morning, there was a crazy bum on the sidewalk yelling. I accidentally made eye contact with him, so he helpfully walked me to school.

And by helpfully, I mean yelling things right into my face while waking next to me. “DO YOU KNOW WHY I DON’T LIKE YOU. DO YOU KNOW? DO YOU?! I’VE HATED YOU EVER SINCE I MET YOU.

He left me alone once I got in the building. The security guard in the lobby shook his head and said “welcome to my life every morning.” Anyway, it was an interesting way to start the morning. I never really felt threatened, just felt sorry for him.

Anyway, school was good.

We did our usual assortment of toy problems. This time, we had to find common characters between two different strings and return them. For example, you’re given ‘aeiou’ and ‘abcde’, the test would expect to get back a string containing ‘ae’.

I didn’t do it in the most efficient way but I did it! I finished with some extra time so I tried to refactor my code to use a more efficient method and ran out of time.

I feel like I’m getting to the point where I have the tools at my disposal to solve any of these problems, I just don’t know the most efficient way to do it yet. But I’m really trying to think about it more!

My partner and I continued to work on our current sprint and finished up adding a few “nice to have” features to our chat client before lunch.

One thing we encountered whenever we look things up online (this happens a lot to all of us and Hack Reactor actively encourages us to solve problems on our own this way) is that there is a universally hated site that pops up to the top of the search results for nearly any web development problem. It’s called W3Schools and has amazing Google ranking for some reason, even though their answers leave a lot to be desired. It’s really just an ad network.

Anyway, they are so derided that people at school generally call others out when they see them on this site. So, we made a little bet.

Each time one of us clicked a link to W3Schools, that person would owe the other partner a beer. So, we started keeping track on a whiteboard. I ended us losing the day and owe him a single beer.

Myself and another classmate (who was my coding partner a week ago), walked down to the Ferry Building for lunch. On Tuesday, Thursday and Saturday we have a two hour long lunch which is provided so that people can exercise or work out. It’s a nice way to get out and enjoy the day.

After lunch, we decided to split up for a bit so we could do some solo studying on the Backbone.js library. Our instructors strongly suggested that we try and complete the extra credit for our current sprint by converting it to use Backbone.

Holy crap, what a beast of a process. It’s probably because I don’t really understand much about something called the “Model-View-Controller” architecture right now. But it was some crazy hard shit!

We watched the solution video to this sprint (which included converting the project to Backbone.js) and we still didn’t understand it. We tried our best anyway but didn’t finish.

That said, even when things weren’t going well or we weren’t understanding some concept, we were never frustrated with each other and always having a great time. It was awesome!

Dinner was pretty uneventful and the student presentation was given by a friendly fellow who is visiting from Florida. His talk was about planning for retirement and “how to manage the piles of dollars we’re all going to get once we graduate.”

The rest of the evening was spent messing around and trying to get stuff to work with Backbone. We didn’t get very far (nor did many other people). Tomorrow is going to be interesting!

Looking forward to eating some sleep tonight though. I’m ready.