Kata: Prime Numbers
In CoffeeScript.
Did my first Vim session with it
- Framework: coffeescript.jasmine-node
- Author: Andreas Simon
- Twitter:
Final Solution
========== AsyncSpec.coffee ==========
#=============================================================================
# Async spec, that will be time outed
#=============================================================================
describe 'async', ->
it 'should be timed out', ->
waitsFor (-> false), 'MIRACLE', 500
========== CoffeeSpec.coffee ==========
describe 'jasmine-node', ->
it 'should pass', ->
expect(1+2).toEqual(3)
========== GrammarHelper.coffee ==========
global.testClass = (description, specDefinitions) ->
suite = jasmine.getEnv().describe('Class: ' + description, specDefinitions)
suite.tags = ['class']
suite.isIntermediate = true;
suite
global.feature = (description, specDefinitions) ->
suite = jasmine.getEnv().describe('Feature: ' + description, specDefinitions)
suite.tags = ['feature']
suite.isIntermediate = true;
suite
global.scenario = (desc, func) ->
suite = jasmine.getEnv().describe('Scenario: ' + desc, func)
suite.tags = ['scenario']
suite.isIntermediate = true;
suite
global.should = (description, specDefinitions) ->
suite = jasmine.getEnv().it('It should ' + description, specDefinitions)
suite.tags = ['should']
suite
========== HelperSpec.coffee ==========
testClass 'HelperLoader', ->
feature 'Loading order', ->
should 'load the helpers before the specs.', ->
expect(true).toBeTruthy()
# will fail to parse the spec if the helper was not loaded first
========== dojoSpec.coffee ==========
# This is a workaround to prevent jasmine-node from executing spec files in the .codersdojo directory
require '../kataSpec'
========== kataSpec.coffee ==========
primenumbers = require './prime-numbers'
describe 'The Prime-numbers', ->
for n in [0..3]
do (n) ->
describe "of #{n}", ->
it "should be [#{n}]", ->
expect(primenumbers.prime(n)).toEqual([n])
for factors in [[2, 2], [2, 3], [3, 3, 3], [5, 7, 11]]
do (factors) ->
number = factors.reduce (a, b) -> a * b
describe "of #{number}", ->
it "should be #{factors}", ->
expect(primenumbers.prime(number)).toEqual factors
========== prime-numbers.coffee ==========
prime = (number) ->
if number < 4
[number]
else
collectPrimeFactors(number, 2, [])
collectPrimeFactors = (number, factor, foundPrimes) ->
if number == 1
return foundPrimes
else if isPrimeFactorOf(factor, number)
foundPrimes.push factor
collectPrimeFactors(number / factor, factor, foundPrimes)
else
collectPrimeFactors(number, factor+1, foundPrimes)
isPrimeFactorOf = (factor, number)->
number % factor == 0
exports.prime = prime
========== specs.coffee ==========
describe "prime factors", ->
describe "of 0", ->
it "is 0", ->
expect(primeFactors(0)).toEqual([0])
describe "of 1", ->
it "is 1", ->
expect(primeFactors(1)).toEqual([1])
describe "of 2", ->
it "is 2", ->
expect(primeFactors(2)).toEqual([2])
describe "of 4", ->
it "are [2, 2]", ->
expect(primeFactors(4)).toEqual([2, 2])
describe "of 6", ->
it "are [2, 3]", ->
expect(primeFactors(6)).toEqual([2, 3])
describe "of 8", ->
it "are [2, 2, 2]", ->
expect(primeFactors(8)).toEqual([2, 2, 2])
describe "of 10", ->
it "are [2, 5]", ->
expect(primeFactors(10)).toEqual([2, 5])
describe "of 37", ->
it "are [37]", ->
expect(primeFactors(37)).toEqual([37])
primeFactors = (number) ->
if number < 4
[number]
else
findFactors(number, 2)
findFactors = (number, nextFactor) ->
if number == 1
return []
if number % nextFactor == 0
[nextFactor].concat findFactors(number / nextFactor, nextFactor)
else
findFactors(number, nextFactor + 1)
Statistics
| Framework |
Started |
Number of Moves |
Duration |
Number of modifications |
| kata |
per move |
kata |
per move |
| coffeescript.jasmine-node |
22-Apr-2012, 07:09:14 PM |
22 |
18m 26s |
50 seconds |
59 |
2.7 |
Sharing
Link to Kata: http://codersdojo.org/statistics/558745aa1f2086a07521c76febe4208a030f1231
Short link to Kata: http://bit.ly/ImnfKO
@