Skip to content

Instantly share code, notes, and snippets.

@spcheema
Created July 2, 2016 13:59
Show Gist options
  • Save spcheema/9b0e395e81091e56ebe15e51f12986ed to your computer and use it in GitHub Desktop.
Save spcheema/9b0e395e81091e56ebe15e51f12986ed to your computer and use it in GitHub Desktop.
10 Word Count Puzzle Solution
through2 = require 'through2'
module.exports = ->
words = 0
lines = 1
transform = (chunk, encoding, cb) ->
# Count lines
if chunk.match(/\n/mg)
lines = chunk.match(/\n/mg).length
parsedInput = filterQuotedWords (chunk)
chunk = parsedInput.remainingStirng
tokens = []
if chunk.length > 0
# Split camel case word into two and rempve trainling space or new line
chunk = chunk.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/(\n|\s)$/, '')
# Replace multiple space and new lines with white space and split string into words
tokens = chunk.replace(/((\s)+|(\n)+)/g,' ').replace( /\n/g, " ").split(' ')
words = tokens.length
if(parsedInput.quotedWords)
totalWords = parsedInput.quotedWords.concat tokens
words = totalWords.length
return cb()
flush = (cb) ->
this.push {words, lines}
this.push null
return cb()
###*
* Function to separate quoted string as single word
* @param {string} Sting
* @return {array} Array of double quoted words and remaining string (withpout doubled quoted words)
###
filterQuotedWords = (input) ->
output = false
quotedStings = input.match(/".*?"/g)
if(quotedStings && quotedStings.length > 0)
output = quotedStings.map((v, i) ->
input = input.replace(v,'')
v.replace (/\"/mg), ''
)
quotedWords : output, remainingStirng : input
return through2.obj transform, flush
The
"Quick Brown Fox"
jumps over the lazy dog
fdcf "cdsa" vwds "casz"
cdsdf
fdcf "cdsa" vwds "casz"
fdcf "cdsa" vwds "casz"
"csaxz" "cwdsa" "adcsxz"
csaxz asdx sasz "casxz"
"ascxz" "fedsacxz"
"ascxz" "fedsacxz"
"ascxz" "fedsacxz"
"ascxz" "fedsacxz"
"csaxz/casxz" "dsacxz"
"Hello" Sp "How are you" Manne
"HelloSP"
ManneCheema
assert = require 'assert'
WordCount = require '../lib'
fs = require('fs')
helper = (input, expected, done) ->
pass = false
counter = new WordCount()
counter.on 'readable', ->
return unless result = this.read()
assert.deepEqual result, expected
assert !pass, 'Are you sure everything works as expected?'
pass = true
counter.on 'end', ->
if pass then return done()
done new Error 'Looks like transform fn does not work'
counter.write input
counter.end()
describe '10-word-count', ->
it 'should count a single word', (done) ->
input = 'test'
expected = words: 1, lines: 1
helper input, expected, done
it 'should count words in a phrase', (done) ->
input = 'this is a basic test'
expected = words: 5, lines: 1
helper input, expected, done
it 'should count quoted characters as a single word', (done) ->
input = '"this is one word!"'
expected = words: 1, lines: 1
helper input, expected, done
it 'should count camel cased word as two word', (done) ->
input = 'TheQuick
BrownFox
jumps
OverTheLazy
dog'
expected = words: 9, lines: 1
helper input, expected, done
it 'should count 1 line and 9 words ', (done) ->
fs.readFile "#{__dirname}/fixtures/1,9,44.txt", 'utf8', (err, data) ->
if err then return throw err
expected = words: 9, lines: 1
helper data, expected, done
it 'shoud count lines in an input string', (done) ->
fs.readFile "#{__dirname}/fixtures/5,9,40.txt", 'utf8', (err, data) ->
if err then return throw err
expected = words: 9, lines: 5
helper data, expected, done
it 'shoud count quotes string as one word ', (done) ->
fs.readFile "#{__dirname}/fixtures/18,44.txt", 'utf8', (err, data) ->
if err then return throw err
expected = words: 44, lines: 18
helper data, expected, done
# !!!!!
# Make the above tests pass and add more tests!
# !!!!!
@spcheema
Copy link
Author

spcheema commented Jul 2, 2016

#10 Word count puzzle solution

Replace lib/index.coffee with lib-index.coffee file and rename file to index.coffee
Replace test/index.coffee with test-index.coffee file and rename file to index.coffee
Add test-fixtures-18,44.txtfile in fixtures folder and rename file to18,44.txt`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment