Last active
February 26, 2017 07:46
-
-
Save hzhu/4087a121d39b8b3ed76548671c7016f2 to your computer and use it in GitHub Desktop.
Revisions
-
hzhu revised this gist
Feb 25, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ reduce_dups('aba') => "aba" ``` ### Analysis of Problem We can solve this problem by `for` looping through the string starting at index 1. Then compare the current character `str[i]`, to the previous character `prev`. When we find a set of duplicate touching characters `(str[i] === prev)`, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string. -
hzhu revised this gist
Feb 25, 2017 . 1 changed file with 10 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -16,27 +16,21 @@ Finally, we reset our variables: `str`, `i`, `prev`. This allows our program to ### Solution Code ``` function remove_dups(str) { let prev = str[0]; for (let i = 1; i < str.length; i++) { if (str[i] === prev) { str = str.slice(0, i - 1) + str.slice(i + 1, str.length); prev = str[0]; i = 0; } else { prev = str[i]; } } return str; } remove_dups('abba') // '' remove_dups('aab') // 'b' remove_dups('aba') // 'aba' ``` -
hzhu revised this gist
Feb 17, 2017 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,11 @@ reduce_dups('aba') => "aba" ``` ### Analysis of Problem We can solve this problem by `for` looping through the given string starting at index 1. We then compare the current character `str[i]`, to the previous character `prev`. When we find a set of duplicate touching characters `(str[i] === prev)`, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string. Finally, we reset our variables: `str`, `i`, `prev`. This allows our program to run a new loop, on our newly created string, repeating the steps above. ### Solution Code ``` -
hzhu revised this gist
Feb 17, 2017 . 1 changed file with 5 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,15 +15,13 @@ Then we reset our variables to run a new loop, on our newly created string, repe ### Solution Code ``` function reduce_dups(str) { let prev = str[0] for (let i = 1; i < str.length; i++) { if (str[i] === prev) { let before_dups = str.slice(0, i - 1) let after_dups = str.slice(i + 1, str.length) str = before_dups + after_dups i = -1 prev = undefined -
hzhu revised this gist
Feb 17, 2017 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,12 @@ ### Problem Write a function that accepts a string argument and removes duplicate characters that are neighbors. If the new string also has duplicate neighbors, those should be removed also. ``` reduce_dups('abb') => "a" reduce_dups('abba') => "" reduce_dups('aba') => "aba" ``` ### Analysis of Problem We can solve this problem by using a loop to find duplicate touching characters. When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string. -
hzhu revised this gist
Feb 17, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ ### Analysis of Problem We can solve this problem by using a loop to find duplicate touching characters. When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string. Then we reset our variables to run a new loop, on our newly created string, repeating the steps above. ### Solution Code -
hzhu created this gist
Feb 17, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ ### Analysis of Problem We can solve this problem by using a loop to find duplicate touching characters. When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters after the *dups*, creating a new string. Then we reset our variables to run a new loop, on our newly created string, repeating the steps above. ### Solution Code ``` function reduce_dups(str) { let prev = undefined for (let i = 0; i < str.length; i++) { if (!prev) { prev = str[i] } else if (str[i] === prev) { let before_dups = str.slice(0, i - 1) let after_dups = str.slice(i + 1, str.length) str = before_dups + after_dups i = -1 prev = undefined } else { prev = str[i] } } return str } reduce_dups('abba') // '' reduce_dups('aab') // 'b' reduce_dups('aba') // 'aba' ```