Created
October 4, 2017 22:48
-
-
Save theoomoregbee/450e4bb352eae0931c54b10ca6de5338 to your computer and use it in GitHub Desktop.
function to return true if the string can be broken down to incrementing sets of integers
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 characters
| /** | |
| * Write a function to return true if the string can be broken down to incrementing sets of integers | |
| * no negative >= 1 | |
| * "" => false | |
| * "1" > false | |
| * "1315" => ? => false | |
| * "54" => false | |
| * "1234" => "1" + "2" + "3" + "4" => true | |
| * "91011" => "9" + "10" + "11" => true | |
| * "5556" => "55" + "56" => true | |
| * "9991000" => "999" + "1000" => true | |
| * | |
| * cc Problem by Simon Lim | |
| */ | |
| /** | |
| * created by Theophilus Omoregbee <theo4u@ymail.com> | |
| * | |
| * Hurray finally gotten it | |
| */ | |
| /////// test cases /// | |
| // let input = "91011"; | |
| // let input = "5556"; | |
| // let input = "1234"; | |
| // let input = "1315"; | |
| // let input = "1314"; | |
| // let input = "99999999100000000"; | |
| // let input = "998999"; | |
| let input = "99100101"; | |
| let c = input.split('');// this is used and can be altered when our programming is running | |
| let original = input.split(''); // just to keep track of the original array | |
| // parse it to be number here | |
| let realMultiple = 1; | |
| for (let i = 0; i < c.length; i++) { | |
| c[i] = restructureFirstElement(realMultiple); | |
| let d = [parseInt(c[i])]; // add to our array to push too when any creteria is correct | |
| let s = ""; | |
| let check = true; | |
| for (let j = i + 1; j < c.length; j++) { | |
| s += c[j]; | |
| if ((d[d.length - 1] - parseInt(s)) === -1) { | |
| d.push(parseInt(s)); | |
| i = j + 1; | |
| s = ''; | |
| // console.log("C", c[i], j); | |
| check = true; | |
| } else { | |
| // console.log("not same", d[d.length - 1], parseInt(s), i, j); | |
| check = false; | |
| } | |
| console.log("d", d); | |
| } | |
| realMultiple++; // increase multiple here to next tens | |
| if (check && d.length > 1) { | |
| console.log("Yippee correct", check); | |
| } else { | |
| check = false | |
| } | |
| } | |
| /** | |
| * increase the first element if its tens or 100s or units | |
| * @param multiple | |
| * @returns {string} | |
| */ | |
| function restructureFirstElement(multiple) { | |
| let value = ""; | |
| for (let i = 0; i < multiple; i++) { | |
| value += original[i]; | |
| } | |
| return value; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment