Friday, December 27, 2013

Day 2 -- Simple Sorts in CoffeeScript

This is the second workout. If, like me, you are new to Node.Js and CoffeeScript, yesterday and today may be a bit of a challenge. In my warmup for today, I started to modularize my simple unit tests. This required me to learn about the Node.Js "require" function. This page - http://openmymind.net/2012/2/3/Node-Require-and-Exports/ - helped me understand how to do this.

My code from yesterday: https://github.com/HackerWorkout/HackerWOD/tree/master/WOD/Day1

Today's workout

Recommended Setup

Language: CoffeeScript 
Tools: Node.js (to make CoffeeScript work in a console window)

Warmup 

A) Improve your simple unit test so that it works as a module.


UnitTest = require "./UnitTest.Core.Js"

B) Add a timer to your unit test so you can measure how long it takes to run.

C) Write a function to reverse a string using an array


ReverseEasy = (str) -> str.split("").reverse().join ""
 
UnitTest.Test "Reverse test", () -> 
    tst = "This is a forward string."
    UnitTest.Assert.Equal(ReverseEasy(tst), Reverse(tst))

Exercise

Write a function BubbleSort (http://en.wikipedia.org/wiki/Bubble_sortand SelectionSort (http://en.wikipedia.org/wiki/Selection_sort) and see which is faster for 10,000 random numbers.

Starter code


Rnd10k = () ->
    (Math.random() for num in [1..10000])
 
CheckSorted = (arr) ->
    n = 0
    end = arr.length - 1
    while (end > n + 1)
        if (arr[n] > arr[n+1]) then throw "Array is not sorted."
        n++
 
UnitTest.Test "Unsorted is unsorted", () -> UnitTest.Assert.Exception () -> CheckSorted(Rnd10k())
 
UnitTest.Test "Bubble Sort test", () -> 
    CheckSorted BubbleSort Rnd10k()
 
UnitTest.Test "Selection Sort test", () -> 
    CheckSorted SelectionSort Rnd10k()

Happy Hacking

Thursday, December 26, 2013

Day 1 -- Setup CoffeeScript



Hacker Workout's goal is to create short daily programming exercises. All the sample code for the next several days will be written in CoffeeScript. Here is why I chose CoffeeScript:

I initially thought JavaScript would be a good starting language, since it has become more important and is so easily accessible. When Google introduced its Chrome browser, one of its distinguishing features was how fast it could interpret the language. Since then, as all browsers have raced to create the fastest JavaScript virtual machine, there has been a movement to start designing webpages using JavaScript as a base language instead of primarily HTML markup. I first noticed this trend when I read Scott Hanselman's JavaScript is Assembly Language For the Web.

JavaScript has some different design characteristics from other languages. It has almost no meta-data, so metaprogramming strategies often do not work. On the other hand, it has strong support for encapsulation and first-class functions, which opens up lots of Object Oriented and functional-style approaches.

For professional development, JavaScript has several gotchas that make it hard to program, such as comparison rules. Probably the most annoying thing about JavaScript is the absence of native namespacing -- named functions are always global, and can easily collide and introduce bugs.

These complexities have lead to several attempts to create a better JavaScript. Some languages, such as closure and C#, try to map their syntax to best-practice JavaScript. Google is working on an actual replacement for JavaScript in a language called Dart. What I really wanted for the first few workouts, though, was a language that would help me learn JavaScript.

This left me with two candidates: Microsoft's superset language TypeScript, and CoffeeScript. What I liked most about CoffeeScript is that its design goal is to make JavaScript easier to write. In just a few minutes on the webpage, I started learning the language better.

Today's workout

Recommended Setup

Language: CoffeeScript 
Tools: Node.js (to make CoffeeScript work in a console window)

Warmup 

Write a function that takes a function as a parameter, paramA, and returns a function, returnA, such that when returnA is executed, it will print a message, then execute the function paramA.

Using Node.js, you can write to your console window by issuing the following command:

console.log "hello world"

Exercise

Write a simple unit test framework in CoffeeScript that handles the following tests:

Test "Addition Test", () -> Assert.Equal(4, 2 + 2)
 
Test "ExpectedFailure", () -> Assert.Exception( () -> throw "failure" )
 
Test "FailedTest", () -> Assert.True(false)

Happy hacking,
-James