Last active
March 30, 2018 16:30
-
-
Save notsoluckycharm/3c5e4a3c6c07f0db6ae0edace8023ea5 to your computer and use it in GitHub Desktop.
Beeswax code challenge
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
// Timed response to problem: | |
// https://drive.google.com/file/d/0B4I3zDNqwzpCZ1BmbTN6TEJxTGVYZWV5WEtTR3BmZ08zdlF3/view | |
// time taken: 8 minutes. | |
let strings = [ | |
'hello world', | |
'{} ', | |
'{{{foo();}}}{} ', | |
'{{}{}} ', | |
'{{{}', | |
'} ', | |
'{}{foo{}', | |
]; | |
function customStack(){ | |
let ourStack = [] | |
return { | |
push: function(item){ | |
ourStack.push(item) | |
}, | |
pop: function(){ | |
return ourStack.length > 0 ? ourStack.splice(ourStack.length-1) : -1 | |
}, | |
peek: function(){ | |
return ourStack.length > 0 ? ourStack[ourStack.length-1] : -1 | |
}, | |
toArray: function(){ | |
return ourStack | |
}, | |
length: function(){ | |
return ourStack.length | |
} | |
} | |
} | |
for( x in strings ){ | |
let string = strings[x] | |
let ourStack = customStack() | |
let balanced = true | |
for( let y in string ){ | |
let char = string[y] | |
if( char === '{' ) { | |
ourStack.push(y) | |
} else if( char === '}' ){ | |
let z = ourStack.pop() | |
// unmatched | |
if( z === -1 ){ | |
balanced = false | |
console.log( | |
'string: %s unmatched ending bracket at %s', | |
string, | |
y | |
) | |
} | |
} | |
} | |
if(ourStack.length() > 0){ | |
console.log( | |
'string: %s unmatched starting bracket at location(s): %s', | |
string, | |
ourStack.toArray().join(',') | |
) | |
} else if( balanced ){ | |
console.log('string: %s balanced', string); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment