Skip to content

Conversation

@obelisk0114
Copy link
Contributor

Return all possible combinations of k numbers out of 1 ... n.

@obelisk0114 obelisk0114 deleted the patch branch July 14, 2019 05:04
Copy link
Member

@cclauss cclauss left a 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 = []
Copy link
Member

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
    """

n = 4
k = 2
total_list = generate_all_combinations(n, k)
print_all_state(total_list)
Copy link
Member

@cclauss cclauss Jul 14, 2019

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

@obelisk0114 obelisk0114 restored the patch branch July 14, 2019 05:05
@cclauss
Copy link
Member

cclauss commented Jul 14, 2019

It would be cool to think up more helpful variable names than n and k.

@obelisk0114 obelisk0114 reopened this Jul 14, 2019
@cclauss
Copy link
Member

cclauss commented Jul 14, 2019

Is this ready to be merged or are you still working on it?

@cclauss
Copy link
Member

cclauss commented Jul 14, 2019

@cclauss
Copy link
Member

cclauss commented Jul 14, 2019

Is this ready to be merged or are you still working on it?

@obelisk0114
Copy link
Contributor Author

It's ready to be merged.

@cclauss cclauss merged commit 628794d into TheAlgorithms:master Jul 14, 2019
@cclauss
Copy link
Member

cclauss commented Jul 14, 2019

import itertools ; print(list(itertools.combinations(range(1, 5), 2)))

@obelisk0114 obelisk0114 deleted the patch branch July 14, 2019 23:15
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Update Bucket Sort time complexity analysis

* Add combinations

* Adding doctest

* Fix doctest problem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants