-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add combinations #1015
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
Add combinations #1015
Conversation
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.
Adding this doctest would be a nice addition but I approve this PR in its current state.
|
||
|
||
def generate_all_combinations(n: int, k: int) -> [[int]]: | ||
result = [] |
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.
Please insert this function docstring here:
def generate_all_combinations(n: int, k: int) -> [[int]]:
"""
>>> generate_all_combinations(n=4, k=2)
1 2
1 3
1 4
2 3
2 4
3 4
"""
backtracking/all_combinations.py
Outdated
n = 4 | ||
k = 2 | ||
total_list = generate_all_combinations(n, k) | ||
print_all_state(total_list) |
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.
Please put these four lines under a if __name__ == '__main__'
:
if __name__ == '__main__':
n = 4
k = 2
total_list = generate_all_combinations(n, k)
print_all_state(total_list)
Or just replace the four lines with:
if __name__ == "__main__":
import doctest
doctest.testmod()
This should not change the functionality of your script but it should give it a new superpower:
- python3 -m doctest -v backtracking/all_combinations.py
It would be cool to think up more helpful variable names than n and k. |
Is this ready to be merged or are you still working on it? |
Is this ready to be merged or are you still working on it? |
It's ready to be merged. |
import itertools ; print(list(itertools.combinations(range(1, 5), 2))) |
* Update Bucket Sort time complexity analysis * Add combinations * Adding doctest * Fix doctest problem
Return all possible combinations of k numbers out of 1 ... n.