File tree Expand file tree Collapse file tree 7 files changed +82
-25
lines changed
lib/data-structures/chapter-1
data-structures/chapter-1 Expand file tree Collapse file tree 7 files changed +82
-25
lines changed Original file line number Diff line number Diff line change 1+ node_modules
2+
3+ # Logs
4+ logs
5+ * .log
6+
7+ # Mac specific
8+ DS_Store
Original file line number Diff line number Diff line change 1- ####Run the code and test
2- This project uses Mocha and Chai for testing:
1+ # Cracking the Coding Interview 6th Edition - JavaScript
32
43<br >
5- #####Install dependencies:
4+
5+ ## Install dependencies:
66``` bash
77npm install
8- npm install -g mocha
9- npm install -g chai
108```
119
1210<br >
13- #####Run tests:
11+
12+ ## Test
13+
1414``` bash
15- mocha --recursive
15+ npm test
1616```
1717
18- <br >
19- #####Add new npm modules:
18+ If you just want to lint
2019``` bash
21- npm install --save < package name >
20+ npm run lint
2221```
22+
23+ <br >
Original file line number Diff line number Diff line change 1+ module . exports = Strings1_4 = ( function ( ) {
2+ return {
3+ // Checks if a string is a permutation of a palindrome
4+ // Solution #4 from the book. Assumes ascii character set
5+ // @param {String } str - Word or phrase
6+ // @retuns {Boolean} - true if a permutation of palindrome exists, false if it does not
7+ palindromePermutation : function ( str ) {
8+ var distinct = 0 ;
9+ var s_array = Array . apply ( null , Array ( 256 ) ) . map ( Number . prototype . valueOf , 0 ) ;
10+ str = str . toLowerCase ( ) ;
11+ for ( var i = 0 ; i < str . length ; i ++ ) {
12+ if ( str [ i ] == ' ' ) {
13+ continue ;
14+ }
15+ s_array [ str [ i ] . charCodeAt ( 0 ) ] ++ ;
16+ if ( s_array [ str [ i ] . charCodeAt ( 0 ) ] % 2 ) {
17+ distinct ++ ;
18+ } else {
19+ distinct -- ;
20+ }
21+ }
22+ return ( distinct < 2 ) ;
23+ }
24+ } ;
25+ } ( ) ) ;
Original file line number Diff line number Diff line change 11module . exports = Strings_1_8 = ( function ( ) {
2- var _isSubstring = function ( str1 , str2 ) {
3- return str1 . indexOf ( str2 ) != - 1
4- }
5- var _sameLengthAndNotBlank = function ( str1 , str2 ) {
2+ var isSubstring = function ( str1 , str2 ) {
3+ return str1 . indexOf ( str2 ) != - 1 ;
4+ } ;
5+
6+ var sameLengthAndNotBlank = function ( str1 , str2 ) {
67 var len = str1 . length ;
7- return len === str2 . length && len > 0
8- }
8+ return len === str2 . length && len > 0 ;
9+ } ;
10+
911 return {
1012 isRotation : function ( str1 , str2 ) {
11- if ( ! _sameLengthAndNotBlank ( str1 , str2 ) ) return false ;
12- if ( _isSubstring ( str1 + str1 , str2 ) ) return true ;
13+ if ( ! sameLengthAndNotBlank ( str1 , str2 ) ) {
14+ return false ;
15+ }
16+ if ( isSubstring ( str1 + str1 , str2 ) ) {
17+ return true ;
18+ }
1319 }
14- }
20+ } ;
1521} ( ) ) ;
Original file line number Diff line number Diff line change 11{
22 "name" : " CrackingJS" ,
3- "version" : " 0.0.1 " ,
3+ "version" : " 0.0.2 " ,
44 "description" : " Data Structures and Algorithms in Javascript" ,
5+ "scripts" : {
6+ "lint" : " eslint lib/**/*.js test/**/*.js" ,
7+ "test" : " npm run lint && mocha --recursive"
8+ },
59 "dependencies" : {
6- "mocha" : " *" ,
710 "chai" : " *" ,
11+ "eslint" : " ^2.2.0" ,
12+ "mocha" : " *" ,
813 "require-directory" : " *" ,
914 "sinon" : " *"
1015 },
11- "devDependencies" : {
12- }
16+ "devDependencies" : {}
1317}
Original file line number Diff line number Diff line change 1+ require ( '../../test_helper' ) ;
2+ describe ( '1.4 #palindromePermutation' , function ( ) {
3+ it ( 'returns true if a string is a permutation of a palindrom' , function ( ) {
4+ expect ( Strings1_4 . palindromePermutation ( 'Tact Coa' ) ) . to . be . true ;
5+ expect ( Strings1_4 . palindromePermutation ( 'jhsabckuj ahjsbckj' ) ) . to . be . true ;
6+ expect ( Strings1_4 . palindromePermutation ( 'Able was I ere I saw Elba' ) ) . to . be . true ;
7+ } ) ;
8+ it ( 'returns false if a string is not a permutation of a palindrome' , function ( ) {
9+ expect ( Strings1_4 . palindromePermutation ( 'So patient a nurse to nurse a patient so' ) ) . to . be . false ;
10+ expect ( Strings1_4 . palindromePermutation ( 'Random Words' ) ) . to . be . false ;
11+ expect ( Strings1_4 . palindromePermutation ( 'Not a Palindrome' ) ) . to . be . false ;
12+ } ) ;
13+ } ) ;
Original file line number Diff line number Diff line change 11require ( '../lib' ) ;
2- module . exports = sinon = require ( " sinon" ) ;
3- module . exports = expect = require ( " chai" ) . expect
2+ module . exports = sinon = require ( ' sinon' ) ;
3+ module . exports = expect = require ( ' chai' ) . expect ;
You can’t perform that action at this time.
0 commit comments