-
-
Notifications
You must be signed in to change notification settings - Fork 44.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An alternate solution for pairs_with_given_sum #11415
base: master
Are you sure you want to change the base?
An alternate solution for pairs_with_given_sum #11415
Conversation
anagrams.txt
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this file for?
# Without using the itertools Package we can do it too | ||
def no_of_pairs(arr: list, req_sum: int) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is unnecessary. Instead, rename the function to clarify that it doesn't use itertools.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok sir
# this is a non optimised code and easy to understand as i am a beginner in coding | ||
# the code above is the best one to use as this function uses nested loops which is | ||
# very much slower in execution |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't add comments about your own thoughts about the code, just document what it does. If you want to clarify to the reader that your implementation is deliberately suboptimal, then please do so in the documentation. You should also be using docstrings for this rather than multiple single-line comments. Also make sure you add doctests in your documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok sir
def no_of_pairs(arr: list, req_sum: int) -> int: | ||
# this is a non optimised code and easy to understand as i am a beginner in coding | ||
# the code above is the best one to use as this function uses nested loops which is | ||
# very much slower in execution | ||
count_total = 0 | ||
for i in range(len(arr)): | ||
for j in range(i + 1, len(arr)): | ||
if (arr[i] + arr[j]) == req_sum: | ||
count_total += 1 | ||
return count_total |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imSanko This entirely rewrites OP's implementation. OP said that the implementation was deliberately suboptimal to demonstrate a simple but naive solution.
count_total = 0 | ||
for i in range(len(arr)): | ||
for j in range(i + 1, len(arr)): | ||
if (arr[i] + arr[j]) == req_sum: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (arr[i] + arr[j]) == req_sum: | |
if arr[i] + arr[j] == req_sum: |
The parentheses are unnecessary
@@ -23,6 +23,19 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: | |||
return len([1 for a, b in combinations(arr, 2) if a + b == req_sum]) | |||
|
|||
|
|||
# Without using the itertools Package we can do it too | |||
def no_of_pairs(arr: list, req_sum: int) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def no_of_pairs(arr: list, req_sum: int) -> int: | |
def no_of_pairs(arr: list[int], req_sum: int) -> int: |
Please specify the type of the list's contents
for more information, see https://pre-commit.ci
Describe your change:
implemented a very simple alternative solution instead of using the itertools package
Checklist: