Learning Math for Programming

When I went to college I had to take math. My degree is actually in Math and Computer Science. Why the math part, you ask? Well, the folks at the school figured that the math will help with a lot of the logic that comes with programming.

Of course, there's also that age old question when you first start learning programming...is there a lot of math involved? The answer is it depends. If you're planning to get a doctorate and do a lot of research, then you'll most likely use a lot of it. However, if you're just working for a company, then chances are you won't need hardly any math*. If you're learning data science then some math is essential in order to gain insights from data or to understand machine learning algorithms.

In this post, we'll go over the most common types of math you may want to be familiar with to get the most out of your programming, and where you can start learning these concepts.

Discrete Math

Discrete math is used quite a bit in programming. Whether it's understanding the theory of how integers work in programming languages or using boolean logic, you've probably come across it before.

Where to Learn

Coursera comes to the rescue here as there may not be too much around discrete math.

Algebra

Yep, even plain old algebra you probably didn't like in high school can be helpful. I actually have an example of this: back when I was doing tax software I needed to use some basic algebra to create a function. Of course, tax software relies heavier on math than most other software, so it's not unusual. Knowing that bit of algebra that I needed, though, really helped when creating the function and I may have had to spend a good bit of time Googling for some help.

Where to Learn

Kahn Academy is well known for their math courses and they definitely have a good algebra one.

For a book, Practical Algebra looks to be a good one to brush back up on your algebra.

Statistics

Statistics is needed for data scientists, not only to help get insights from your data but to also make sure variables that seem correlated to each other actually are statistically significat rather than not. That's not the easiest to do even if you plot your data. Statistics will give you a big advantage in understanding your data and how it can answer any questions you can throw at your data.

Need to see if an A/B test has a significant difference? Then statistical hypothesis testing can be of a great help.

Where to Learn

Coursera has a great intro to data course that has really helped me out in learning some of the basics of statistics. The book they recommend, too, is actually really good.

As for books, I recommend a couple to start with. Practical Statistics is a great introduction. If you want to focus more on the data science side of statistics then Practical Statistics for Data Scientists is a great one to get.

If you want to get a bit advanced, then I definitely recommend Introduction to Statistical Learning. This is mainly for getting deep in the machine learning algorithms, but still is an interesting read. You can also read it for free online instead of getting a physical copy. There's an even more advanced version of this too with The Elements of Statistical Learning, which is also available for free online.

Linear Algebra

Linear algebra is a bit of a niche in programming. The only place I've really seen it used is for machine learning algorithms. Linear algebra, is mainly matrix manipulation.

Where to Learn

Khan Academy has some linear algebra courses that you can take. This is probably the most complete of them I've seen around.

For a book, mostly what you'll see are text books. That's not all bad, but sometimes a more general book is helpful. In that case there's Linear Algebra for Dummies.


While math isn't necessary for programming, I believe it can certainly help with the logic like my university thought. Learning the extra math also made me appreciate it more for what all math can teach us about the world. Also, don't think you're not good at math. This old post from Steve Yegge explains more about the way math is taught in schools and what we can do about it as programmers. You're not bad at math, you just need a better way to learn it.

* Though, depending on the company you work for you may need it. For example, working for a financial institution may involve some knowledge of math.

10 Sites Where You Can Get Programming Practice

Watching and reading programming tutorials are great! They give a curated view of a concept or new technology that may take hours longer to understand than going at it alone. However, much like math, programming is not a spectator sport. You need to practice in order to better understand the programming concept or technology in a real program.

There are a lot of ways one can do this, though, without having to fully set up an environment for each language or framework you want to learn.

Here are 10 sites that you can use to start learning new languages and other programming concepts.

Code Wars

Code Wars profile view.

Code Wars profile view.

This is one of my favorite sites to go to, especially when I want some practice with a new language. They have most of the common languages already supported with a few more in beta so most likely you can get some practice in here.

All of the "katas", as they call the challenges, have always been challenging for me. Plus seeing other people's solutions I'm sure to learn something new.

Hacker Rank

hacker-rank.JPG

Hacker Rank is another code challenge site, but a tad bit different. They also have competitions throughout the year where you can participate and see how you rank among other competitors. I've also seen some companies use this site as a first pass for interviewing candidates.

The nice thing about this site is that it pretty much emphasizes algorithms and data structures, which I consider two things that you could get the most out of if you practice it.

Top Coder

TopCoder compete login page.

TopCoder compete login page.

TopCoder is similar to HackerRank, but you can actually get paid for the challenges you compete in. This is a good way to see how you compare with other developers in the challenges and to challenge yourself to get better at certain types of programming, such as data structures, math, and string manipulations.

Kaggle

Kaggle is more suited for data scientists, but it's a place where you can find data to play around with. They also host their own competitions that can pay out.

You don't need to use this for data science, though. Since they offer a lot of data sets you can use them in for other applications as well. Want to create a web app with some of the data? Go for it!

Project Euler

Project Euler is probably one of the first sites with programming challenges and puzzles. This site is composed of mathematical problems that you can solve in code and the further you go the harder the math problems.

Rosalind

Very similar to Project Euler, Rosalind gives challenges in regards to problems in bioinformatics. These challenges may be considsered a bit better since they solve more real world problems than just arbitrary math problems in Project Euler.

Screeps

This is a fun one, especially if you are really interested in game programming. This site gives you the opportunity to code exercises as part of a Real Time Strategy game with JavaScript. Game programming is a totally different beast than doing web sites or any other business applications as it's a different way of thinking and experience that goes with it. This is especially great to get started with some game programming when you don't necessarily have a project of your own to work on.

CodeChef

CodeChef is similar to TopCoder where they host several competitions and you can get paid for completing them. CodeChef holds those competitions monthly so you'll always have a chance to see how you stand against other programmers. There are definitely plenty of problems to practice before going into a competition and they are all ranked from easy to hard.

CodingGame

CodinGame is fairly similar to how Code Wars works, but the challenges are a bit more of a game. They are more turn based so with each "turn" you get new input and you must give new output based off of it. Similar to Screeps above, the challenges are like mini games you help code. When you run your test cases you can see your code in action as the game is being played, so you get some instant, and visual, feedback to how your code performs.

Up For Grabs

Ok, so this isn't much of an exercise site, but I feel that it's important enough to mention in this post. Why? Working on open source projects is one of the best things you can do as a developer, and this site helps make it easy. A lot of projects have been creating an "up-for-grabs" label on GitHub to let newer contributors get into their projects a lot easier by fixing easier bugs. Doing these will help you become a regular contributor to the project which will have more real world experience than doing other algorithmic or mathematical exercises.

Consuming the Jamendo API with AngularJS: Services

It took a little while to get back to this little project. After watching some nice stuff from NgConf the other week, it helped get me excited again for AngularJs. Plus, I was stuck on a problem for quite a while that I'll explain in more detail below that really slowed me down. After a bit of a break I got back to it and was able to figure it out.

Services

Before we looked at how to use the $http get method from AngularJS, but all of that was within the Angular controller itself. To separate things out and to help us test easier I moved the $http calls into an Angular service.

To recap, let's take a look at our controller currently:

As mentioned earlier, we have our $http call right inside of our controller. While that definitely works, it's better to separate that out into it's own service. Let's refactor this down a bit...

Now this looks a bit cleaner. We're first passing in the albumsService as a dependency into the controller. Of course, if Angular can't find that the albumsService is defined, it'll throw an error.

Next, we have the function definition of our getAlbums method. In here we extract out the artist from the scope and then call the getAlbums method on our service. In our call to the service we give it a function as a callback if the $http get was a success that we'll see later when we look at our service. This basically just gets the results and puts them into the scope.

Now let's take a look at our service.

Pretty straight forward as it's basically the same as what we had in our controller earlier. We inject in the $http Angular object and we create our getAlbums method while prepending it with this

When we call our success promise from the get method, I just pass in the callback method that we defined in our controller earlier. This makes it a bit easier on our service since the callback method was updating our $scope object and we don't have to worry about that here in our service.

I mentioned earlier that I had a bit of an issue that kept me from finishing this up. I kept getting an $inject error on my service that indicated that it wasn't defined, but I couldn't figure out why that was. After some fiddling around with it, it turns out that I was trying to inject the $scope object into the service and Angular doesn't like that. That's also a reason to use the callback.

Conclusion

Moving the $http get call to an Angular service is mostly just a small refactoring, but it's a good one to do. We will see later when we start to do testing how much easier it is to have that separated out.

Of course, this was just an introduction to Angular services so there's tons more to learn about them. I'll recommend Dan Wahlin's AngularJS in 60ish Minutes to anyone. In fact, I've referenced it a few times to remind myself about services. There's also the WintellectNow video from Jeremy Likness on AngularJS Services that goes into much greater detail of services.

You can play with the demo and, as always, you can play around with the code itself.

Beginning Ruby for C# Developers

Ruby has become quite popular as a programming language over the past few years. Probably mainly due to the popularity of the Ruby on Rails framework. Because of its popularity, it makes sense to learn at least the basics of it and what all it can do for you as a developer.

Until I have a better solution for embedding Ruby code and output, I'm just embedding the code and results as Github Markdown for now. Even though it doesn't seem to display properly in the embed, it does when viewing the actual gist on GitHub.

Getting Started

One of the easiest ways to get started using Ruby is to just use your browser. Repl.it is a site that supports many dynamic languages to be run in a browser and it's very handy to just play around in.

I've also seen people use Sublime Text to edit their Ruby code (I admit, I enjoy using it as well as a light weight IDE. I've had no problems using the beta of version 3, either). For Windows, you'd just need to install Ruby. If you're on a Mac, you already have it installed. In fact, you can just type "ruby -v" in the terminal to see what version you have.

Since Ruby is a dynamic language, there is no compiling. However, everything in Ruby is considered an object, and because of that everything will include some built in functions with it. With everything in Ruby being an object isn't necessarily a new concept to C# developers since every object in C# derives from System.Object and you do get a few predefined functions, Ruby just seems to have gone that extra step. 

For example, say you want to run a statement three times in a row. How would you do that in Ruby? It's actually easier than just using a for-loop similar to what you would do in C# or even creating a way to use a lambda expression.

Pretty easy and very straight forward, especially for someone who may not even have a programming background.

Let's go on with a few other basics...

Output

Ruby has a couple of ways to display output back to the console. The main way is to use the "print" statement, though you may also see the "puts" (put string) command as well. The only difference in these is the "puts" command will append a new line. You'll probably see these statements quite a bit, especially if you go through online tutorials.

Methods

As mentioned earlier everything in Ruby is an object, and since that's true it can have methods on those objects. Of course, depending on the type Ruby recognizes it during runtime is what methods are available. For example, the "length" method can be used on strings but not on numbers.

Methods, like in C#, can also be chained together to give some added syntactic sugar and to reduce the overall code needed to accomplish what you need.

One final note about methods that got me confused early on but I thought was really nice is that there is a bit of a naming convention associated with certain methods. Mainly methods that change the object itself and methods that return a boolean.

Take the "upcase" method we used earlier. There are actually two versions of this method, "upcase" and "upcase!". The first returns a copy of the string being used, the second (indicated with an exclamation point) changes the instance of the string being used. In other words, the first version causes the string to be immutable.

For methods that return a boolean value a question mark is appended to the end of the method name to indicate to the developer that that method can be used for branching. Take the string's "empty?" method, for example. As you can probably tell, it returns if the string is empty or not.

Program Flow

Like most programming languages Ruby supports the if/else block. However, one noticeable difference is that Ruby also has an "unless"' keyword. It will only execute if the given conditional is false. This was included to help make certain branching more readable and a bit more intuitive.

So we can change the structure of an if/else to an unless block like so...

Further Learning

One of the best resources I can recommend to help get you on the path of learning Ruby much easier is Code Wars. This site has quite a few code katas that will help in understanding the syntax and included methods that come in with Ruby.

Codecademy is recommended a lot, as well, by people and for very good reason...they have a lot of good interactive tutorials for learning Ruby.

Conclusion

Of course, I definitely don't know everything about the language or all the tools the community uses. I still haven't gotten my head totally around gem files or rake. I'm not even totally sure of the correct structure of a Ruby application! But, hopefully this will at least spark some interest in you C# people to maybe try something different outside of the .NET world. There is plenty of fun to be had with it and I throughly enjoy learning the language and all it has to offer.

Code Katas Can Be Helpful

After reading a few posts in the development community lately about code katas, I felt I should put out my own opinion about the subject. 

At work, a few of us developers decided to take a couple of lunches each week and get together to work on a code kata that we all decide on prior to the meetings. This was mainly so we can increase our skills in development, but mainly it was a way for us to use a new or not so familiar language for a fairly small coding project.

For our first shot at a kata we decided to try Poker Hands (you're always welcome to view my progress), which we had to rank two player's poker hand and determine who won and display how they won (high card or higher set of cards). Not exactly the easiest kata to do, especially when doing it in a language you're definitely not familiar with, but we thought it was one that could give us a good challenge.

Another aspect to doing katas is to do solve very small problems. One of my favorite ways to do this lately is by playing around at Code Wars. I've been messing with the JavaScript and Ruby katas there and have been learning quite a lot about the languages as each new kata I do gets progressively harder. They have a pretty slick interface, as well.

I feel this site does a lot of things right. Most importantly, once you've successfully finished a kata it will also display how others have successfully implemented it. Looking though those can help see how other people use the language to their advantage.

Sure, you can also be doing other things in order to learn, such as messing around GitHub for a project to contribute to or volunteering. However, a big benefit to doing these small katas is because they are small compared to these other types of projects. You won't feel as overwhelmed by the size of the project or try to figure out where to start. Here you just create the appropriate class or function and make sure all the tests pass. 

Of course, you want to do code katas because you believe they help and because you find them enjoyable. I feel like I'm learning new languages little by little the more I do these. I seem to have rekindled my love of programming just by doing these katas. I hope I'm not the only one.