{"id":252,"date":"2020-02-15T14:58:26","date_gmt":"2020-02-15T14:58:26","guid":{"rendered":"https:\/\/danwritescode.com\/?p=252"},"modified":"2020-06-22T13:13:34","modified_gmt":"2020-06-22T13:13:34","slug":"codility-lesson-1-javascript-solution-binary-gaps","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/codility-lesson-1-javascript-solution-binary-gaps\/","title":{"rendered":"Codility Binary Gaps"},"content":{"rendered":"\n

This is a javascript implementation that renders a 100% correct solution of the Codility Lesson 1 challenge – Binary Gaps.<\/p>\n\n\n\n


I chose a recursive approach to this, and quite a simple solution. The performance is on point however.
We transform the integer into a string representation in binary of that integer.<\/p>\n\n\n\n

Then, we check for the first occurance of a 1, measure the gap to the next 1 and so on. There are tests to see if we reached the end, or if there’s only one “1” in the representation. And that’s all, folks.<\/p>\n\n\n\n

let gap;\nfunction solution(N) {\n    \/\/ write your code in JavaScript (Node.js 8.9.4)\n    if (N === parseInt(N, 10) && N>=1 && N<=2147483647){\n        gap = 0;\n\n        \/\/turn it to binary\n        const binary = N.toString(2)\n        \/\/turn it into an array\n        let NArray = binary.split('')\n        findGap(NArray)\n    }\n    return gap;\n    \n}\n\n\n\nfunction evalGap(distance){\n    if (distance > gap){\n        gap = distance;\n    }\n}\n\nfunction findGap(binaryArray) {\n    if (binaryArray.indexOf('1') == -1){\n        return gap\n    } \n    else {\n        index1 = binaryArray.indexOf('1');\n        evalGap(index1)\n\n        binaryArray = binaryArray.slice(index1+1);\n        index2 = binaryArray.indexOf('1')\n\n        if (index2 !== -1) {\n\n            evalGap(index2)\n            binaryArray = binaryArray.slice(index2+1);\n            findGap(binaryArray)\n        } else {\n            if (gap === 0) {\n                gap = 0\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n

See it in action here: https:\/\/app.codility.com\/demo\/results\/trainingJQ5UZ9-3G4\/<\/a><\/p>\n\n\n\n

Did you solve it in another way? Or perhaps you know a way to improve this solution? Drop it below in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"

This is a javascript implementation that renders a 100% correct solution of the Codility Lesson 1 challenge – Binary Gaps. I chose a recursive approach to this, and quite a simple solution. The performance is on point however.We transform the integer into a string representation in binary of that integer. Then, we check for the first occurance of a 1, measure the gap to the next 1 and so on. There are tests to see if we reached the end, or if there’s only one “1” in the representation. And that’s all, folks. See it in action here: https:\/\/app.codility.com\/demo\/results\/trainingJQ5UZ9-3G4\/ Did <\/p>\n","protected":false},"author":1,"featured_media":331,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[],"_links":{"self":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/252"}],"collection":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":0,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media\/331"}],"wp:attachment":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}