{"id":20931313,"url":"https://github.com/ssteele/sum-pairs","last_synced_at":"2025-09-03T12:47:06.377Z","repository":{"id":32996837,"uuid":"36626930","full_name":"ssteele/sum-pairs","owner":"ssteele","description":"Code kata","archived":false,"fork":false,"pushed_at":"2017-03-15T05:47:44.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T02:16:58.180Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ssteele.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-31T23:30:26.000Z","updated_at":"2017-03-15T05:47:45.000Z","dependencies_parsed_at":"2022-09-04T02:51:13.064Z","dependency_job_id":null,"html_url":"https://github.com/ssteele/sum-pairs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ssteele/sum-pairs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssteele%2Fsum-pairs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssteele%2Fsum-pairs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssteele%2Fsum-pairs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssteele%2Fsum-pairs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ssteele","download_url":"https://codeload.github.com/ssteele/sum-pairs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssteele%2Fsum-pairs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273445964,"owners_count":25107150,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T21:41:07.701Z","updated_at":"2025-09-03T12:47:06.337Z","avatar_url":"https://github.com/ssteele.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Sum Pairs\n\n### Requirements\n\nWrite a function f(x) that returns all distinct pairs of integers between -50 and 50 (inclusive) whose sum is `x`. Thus:\n\n* f(100) returns []\n* f(99) returns (49,50)\n* f(0) returns (-50,50)(-49,49)(-48,48)(-47,47)(-46,46)(-45,45)(-44,44)(-43,43)(-42,42)(-41,41)(-40,40)(-39,39)(-38,38)(-37,37)(-36,36)(-35,35)(-34,34)(-33,33)(-32,32)(-31,31)(-30,30)(-29,29)(-28,28)(-27,27)(-26,26)(-25,25)(-24,24)(-23,23)(-22,22)(-21,21)(-20,20)(-19,19)(-18,18)(-17,17)(-16,16)(-15,15)(-14,14)(-13,13)(-12,12)(-11,11)(-10,10)(-9,9)(-8,8)(-7,7)(-6,6)(-5,5)(-4,4)(-3,3)(-2,2)(-1,1)\n\n### Discussion\n\nWhile the solution is a bit overkill for the problem at hand, the code shows a number of concepts that are important in software development, namely:\n\n* Object-oriented programming principles\n    * Single responsibility\n    * Dependency inversion\n    * Don't repeat yourself\n* Test driven development (or at the very least, having functional tests)\n* Exception handling\n* Lazy class loading\n* Namespacing\n\n#### Big-O\n\nThe functionality that solves the problem posed basically boils down to the following:\n\n    // input\n    $sum = 0;\n    $range = [-50, 50];\n    $pairs = [];\n\n    // loop range\n    for ( $i=$range[0] ; $i\u003c=$range[1] ; $i++ ) {\n\n        for ( $j=$i+1 ; $j\u003c=$range[1] ; $j++ ) {\n\n            if ( $sum === ( $i + $j ) ) {\n                $pairs[] = [$i, $j];\n            }\n\n        }\n\n    }\n\n* $sum, $range, and $pairs assignment: `O(1)`\n* outer loop: `O(n)`\n* inner loop: `O(.5n)` as it will average half as many iterations as the outer loop\n* is equal conditional: `O(1)`\n* append to $pairs array: `O(1)`\n\nWritten out and simplifying yields:\n\n    O(1) + O(1) + O(1) + O(n){ O(.5n)[ O(1) + O(1) ] }\n    O(3) + O(n^2)\n    O(n^2)\n\nA more efficient approach for a large dataset might be, for a single outer iteration:\n\n1. split the inner range in half [1sthalf, 2ndhalf]\n2. take the first value of the 2ndhalf\n3. add this value to the outer index\n4. compare this calculation ($calc) to the input $sum\n5. if $calc \u003c $sum, split 2ndhalf into new [1sthalf, 2ndhalf]; else split 1sthalf into new [1sthalf, 2ndhalf]\n6. repeat steps 2-5 until the proper inner value is found\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssteele%2Fsum-pairs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fssteele%2Fsum-pairs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssteele%2Fsum-pairs/lists"}