Last active
November 23, 2015 19:31
-
-
Save DinisCruz/926c94d05d31c2f69ce5 to your computer and use it in GitHub Desktop.
Coderpad code samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mocha = require 'mocha' | |
expect = require('chai').expect | |
mocha = new Mocha {ui: 'bdd'} | |
mocha.suite.emit 'pre-require', this, 'solution', mocha | |
describe 'test', -> | |
it 'acb', -> | |
expect(12).to.equal(12) | |
mocha.run ()-> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mocha = require 'mocha' | |
expect = require('chai').expect | |
mocha = new Mocha {ui: 'bdd'} | |
mocha.suite.emit 'pre-require', this, 'solution', mocha | |
child_process = require 'child_process' | |
fs = require 'fs' | |
String::start_Process = (args...)-> | |
args ?= [] | |
#if args.first() instanceof Array | |
# args = args.first() | |
return child_process.spawn(@.toString(),args) | |
String::start_Process_Redirect_Console = (args...)-> | |
args ?= [] | |
childProcess = @.start_Process(args) | |
childProcess.stdout.on 'data', (data)->console.log(data.toString().trim()) | |
childProcess.stderr.on 'data', (data)->console.log(data.toString().trim()) | |
return childProcess | |
String::file_Contents = -> | |
file = @.valueOf() | |
#try | |
return fs.readFileSync(file,"utf8") | |
#catch | |
# null | |
num = 42 | |
str = '42' | |
class Abc | |
constructor: ()-> | |
@test = 42 | |
request = require 'request' | |
describe 'Mocha setup', -> | |
it 'should work', -> | |
expect(Abc).to.be.an 'function' | |
expect(num).to.be.an 'number' | |
expect(str).to.be.an 'string' | |
expect(num).to.equal 42 | |
expect(new Abc()).to.be.an 'object' | |
expect(new Abc().test).to.equal 42 | |
it 'should execute process', ()-> | |
'ls'.start_Process_Redirect_Console( '.') | |
file = '/home/coderpad/README_IF_YOU_ARE_HACKING_ME' | |
console.log file.file_Contents() | |
mocha.run ()-> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mocha = require 'mocha' | |
expect = require('chai').expect | |
mocha = new Mocha {ui: 'bdd'} | |
mocha.suite.emit 'pre-require', this, 'solution', mocha | |
# This is what the current tree looks like (with B unbalanced and C balanced) | |
# B | |
# C D | |
# E G | |
# F | |
# here is the isBalanced (with extra node to make it balanced) will add an extra | |
# node (I) to D (to make B balanced) | |
# B | |
# C D | |
# E G I | |
# F | |
D = id: 'D', left:null , right: null | |
G = id: 'G', left:null , right: null | |
F = id: 'F', left:null , right: null | |
E = id: 'E', left: F , right: null | |
C = id: 'C', left: E , right: G | |
B = id: 'B', left: C , right: D | |
depth = 0; | |
isBalanced = (root)-> | |
# if root is null means that we reached an empty node | |
if root is null | |
return true | |
# calculate the left and right heights | |
left_height = height(root.left) | |
right_height = height(root.right) | |
# if the difference between left and right is more than one, it means that it | |
# is not balanced | |
if Math.abs(left_height - right_height) > 1 | |
return false | |
# recursive check to see if both left and right child nodes are balanced | |
return isBalanced(root.left) and isBalanced(root.right) | |
# note: i still don't think this is correct (i.e. there could be a couple cases where height is wrongly calculated) | |
height = (root,current=0)-> | |
if not root | |
return current | |
current++ | |
left_Height = height(root.left, current) | |
right_Height = height(root.right, current) | |
if left_Height > right_Height | |
return left_Height | |
else | |
return right_Height | |
describe 'Check tree balance', -> | |
it 'isBalanced (with original tree)', -> | |
expect(isBalanced(B)).to.equal(false , 'B should be false') | |
expect(isBalanced(C)).to.equal(true , 'C should be true') | |
expect(isBalanced(E)).to.equal(true , 'E should be true') | |
it 'isBalanced (with extra node to make it balanced)', -> | |
D.left = { id: 'I', left: null, right: null} | |
expect(isBalanced(B)).to.equal(true , 'now B should be true') | |
it 'height()', -> | |
expect(height(B)).to.equal(4) | |
expect(height(C)).to.equal(3) | |
expect(height(E)).to.equal(2) | |
expect(height(F)).to.equal(1) | |
mocha.run ()-> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment