The Cheap Coder

I’m pretty frugal, so it caused me a great deal of psychic distress paying for my coding bootcamp, and following that, technical interviewing course. Despite my satisfaction with the curriculum, teachers, community and outcome, the entire time I kept saying to myself “I could be learning this FOR FREE. I could have just bought a thousand Chalupa Supremes from Taco Bell!”. I’m not sure why that was my metric, but it definitely stuck in my head.

Well… as I’ve gotten older, I’ve learned that sometimes you just gotta shut up and pony up the cash. You’re paying extra for quality, accept it and move on. For everything else, there’s probably a cheaper way in. You just have to look for discounts.

SUBSCRIPTION SITES

Okay, why do you need memberships to learn-to-code subscription sites? I know that you can teach yourself how to code for free. It’s all online. But the right content can make all the difference in your understanding, and sometimes the good stuff is behind a paywall! Here are some discounts I’ve seen over the past few months:

CONFERENCES

Conferences are a great learning opportunity, but with ticket fees, airfare, food and lodging it gets pricey real fast. The financial hurdle can be a lot to justify when you’re the one covering the costs. You could just watch the conference’s livestream, but really it’s not the same at all. Luckily there’s quite a few ways to weasel your way into conferences.

  • Follow the conference’s Twitter account for discount codes. They may even post ticket giveaway contests here.
  • Do you subscribe to web dev mailing lists? You can get reduced tickets there as well. I got my $19 ForwardJS ticket the day before the event thanks to Girl Develop It’s mailing list. Something like a 90% discount! The Women Who Code mailing list offers regular ticket giveaways so often they have an evergreen Google form they use for processing applications.
  • You can also try to win a scholarship, lots of conferences seem to be offering them now. However you will probably have to write more than a few essays explaining your situation -> why you are broke as a joke. It can feel very vulnerable putting that information out into the void, and then being rejected.
  • I applied for RubyConf’s scholarship, and didn’t make the cut, however they offered free passes for volunteers. So I decided to volunteer. ~10 – 15 hours of work for a $400 3 day conference pass sounded like a good idea to me.

Printing a Multiplication Table of the First 10 Prime Numbers

Ok, for a change of pace let’s break away from sorting algorithms and do this problem:

Printing a Multiplication Table of the First 10 Prime Numbers

Code Here

I got this as a coding challenge a few days ago and finally got a chance to tackle it this weekend. The prompt asks for the user to create a program that runs on the command line, and it should print out a multiplication table to STDOUT. I feel like I’ve seen similar problems elsewhere, but this one had the twist that it wanted the multiplier numbers to be primes.

Other rules: Make it flexible for N # primes, write some tests, don’t use Ruby’s Prime class, package yer code.

I started out this challenge by trying to understand clearly what the problem was. Print out a multiplication table? Like this?

039827b

Oooooooooooooh. Haven’t seen one of those in a long time.

So the top row and the first column would be populated with primes. Hmm…

Since I couldn’t use Ruby’s Prime class from the standard library, writing a method for finding primes seemed like the first thing to do.

I wanted to make my program flexible so you could create multiplication tables of different sizes. So I created a method to pull the first n primes into an array.

And finally actually printing out the actual table! Formatting was the biggest pain here.

After I was done with these methods, I thought I was good to go. HA! Nope, still got to package it up and write some tests. I was going back and forth, but I’ll talk about the tests first.

I decided to go with RSpec, so I created a gemfile and added in the requirement. Then I created some spec docs and started going to town. I really only had 3 methods to test, and I decided to do 2 tests for each. It’s been a while since I watched ThoughtBot’s TDD lecture, and my subscription to their site has lapsed, so I decided to keep it as simple as possible. An RSpec tutorial I was reading was referencing a “Calculator” class in its tests, so I thought “Oh, I might as well bundle up the 3 methods into a MultiplicationTable class.” So I went back and changed that, and my tests.

All of the tests were pretty simple except for the print_tables one. Again, it all came down to formatting. I wasn’t sure how to check what was being printed to STDOUT. I came across stringio and some helper methods on StackOverflow, threw them into my spec_helper, and then it was on. I could capture and check what was being printed to the console. Still, it was tricky to figure out what string I was looking for here. RSpec failure messages came to the rescue as I just copied the extremely ugly failure string  (making sure #’s were correct) and pasted into my tests.

Hooray! My tests were looking for the right things now. Nevermind that when I ran the tests I got this little deprecation warning:

screen-shot-2016-10-16-at-2-16-45-am

Um… ok. Sure I can go back and change that easily. Nope, getting errors after I tried to fix it. I’ll go back and deal with it later.

I started out thinking about how to package my program. My mind wandered to gems, which of course made me think about my NPR Stories Gem. I looked at my gem to check out the basic structure, see what I was still missing in my multiplication table project. Then I got sidetracked for about 30 minutes trying to push my NPR gem to RubyGems. I hadn’t yet actually published my NPR gem for a variety of different failures, but this time I realized one major reason- I had to actually build the gem first with:

gem build npr_stories.gemspec

then I could push it to RubyGems. D’oh. That was embarrassing. Now that it was published, and robots were already scraping it, I wanted to make sure that it worked, so I spent some more time trying to edit and test my NPR gem, then I remembered that I had to finish this coding challenge. Right. And of course there was a typo in my gemspec file. Not worth doing a whole gem update for a single letter typo.

Ok, back to multiplication tables! So I could package this multiplication table as a gem for easy installation…But is it even worth it? If so, I should have set this up as a gem to begin with so I could have used this bundler command that pulls together all those files and folder structure for you:

$ bundle gem my_gem

I decided to do it anyways and created a new repo to play with. I just copied over my tiny amount of code thus far.

15 minutes later I abandoned ship and returned to my original repo. Creating a gem just didn’t make sense to me for something so small. Hmmmm… how did I want this to run on the command line? I created a bin folder & executable, and also a CLI class with a simple interface.

And after a few hiccups it’s working!

screen-shot-2016-10-16-at-2-15-21-am

The last step I had was to write a README doc. Whew! That was a lot of work for such a simple little ask. It would have gone faster if I hadn’t indulged my little diversions to other projects and down multiple rabbit holes.

Algorithms: Insertion Sort

I wanted to make Insertion Sort part of a two-fer with Selection Sort, but it honestly took me a minute to figure out how to implement.

Continuing the card game analogy of earlier, if Selection Sort is scanning your hand and moving the smallest items down to the left, eventually ending up with a fully sorted hand…

Insertion Sort is like when a dealer gives you a new card. You look over what you’ve got so far, and add the card to its rightful spot. Similar to Selection Sort, Insertion Sort also operates on the idea of a “sorted” section and  an “unsorted” section.

When an item (let’s call it A) is larger than the element to its immediate left (B), the two aren’t swapped like in Bubble Sort. Instead, you hold on to A’s value, and then reassign B’s value to the spot array[A]. So it’s like you are sliding B’s value down the line. Then you compare A with the item in the index to the left again (C). If A > C, you can then put A down in array[B]’s original spot. Otherwise, you need to keep working to the left and shifting over elements until you find the right spot for A. When you finally assign A to its spot, you’re done with inserting A. Then you repeat the process with the next item in the “unsorted” section.

I had some issues wrapping my head around how to move items down the line, making room for the array, initially thinking about a recursive solution, but really all I needed was a while loop.

 

Algorithms: Selection Sort

I’m still working my way through implementing the basic sorting algorithms in Ruby, so maybe I should just make this a special “Intro Sorting Week.” There, sounds like a fun holiday, right? Today we’re talking selection sort.

Selection Sort

Selection sort is where you scan the array, pick the smallest element, and then swap it with the first element. Then you scan the rest of the array again, look for the next smallest element, and then swap it with the second element… Y’all see where I’m going with this?

It creates a sorted subarray on the left, and an unsorted subarray on the right. You work your way through smaller and smaller subarrays until the entire array is sorted. The algorithm is called selection sort because it repeatedly SELECTs the next smallest element and swaps it into place.

A common example of real life selection sort is when you’re playing cards and you are organizing your hand. You look over your hand, and SELECT a card and move it to its rightful place on the left (creating a sorted section), and continue working through until your hand is sorted. To me this isn’t the greatest example, because you’re not necessarily swapping the items when you select and sort a minimum card. It’s more like the cards are individually going both left and right, and often you can move multiple cards at a time. But that’s just me and how I organize my cards… back to Selection Sort.

The good: Selection sort is easy to understand. It’s also faster than bubble sort, but given how awful BS is (ever notice that bubble sort is BS?) that’s not much of an accomplishment. Selection sort is also in place, so it saves you space.

Downsides: This sorting algorithm takes a long long time. O(n^2) even in its best case. Ouch! Also it’s important to note that it’s not stable.

Here’s my selection sort done with two for loops:

Algorithms: Bubble Sort

Bubble sort! I stumbled across this clip while looking for video algorithm explanations.

Wow, even Obama knows what’s up. Bubble sort ain’t where it’s at.

The idea behind bubble sort is that given an array, you can go through the array, item by item, and compare each item with the item that is next in line. If the first item is larger (or comes after) the second item, then you should swap the pair. Continue these pair comparisons until you get to the end and then repeat. After enough times running through the array, you will come to the point where the array is sorted, and you’ll find that you’ve made no swaps.

There are some advantages to bubble sort. It’s easy to understand how it works. It uses up a constant amount of auxiliary space, it’s stable, and in the best case scenario, when the array is already sorted, it takes linear time.

Alas, bubble sort is mostly impractical. In application, bubble sort can take O(n^2). Not great.

Here’s my implementation of bubble sort in ruby. It’s really all about that swap flag.

** EDIT : After some thinking, I realized that during each iteration there is no need to check the items at the end since the largest item will “bubble up” to the top. The subarray that you are looping through will get smaller & smaller. This can be accomplished by iterating backwards over the array and decreasing the area searched, but I haven’t added this yet. Surprisingly, Bubble Sort could be a little less terrible.

Scroll To Top