Skip to content

Add combinations - #1015

Merged
cclauss merged 5 commits into
TheAlgorithms:masterfrom
obelisk0114:patch
Jul 14, 2019
Merged

Add combinations#1015
cclauss merged 5 commits into
TheAlgorithms:masterfrom
obelisk0114:patch

Conversation

@obelisk0114

Copy link
Copy Markdown
Contributor

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

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

@cclausscclauss left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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:

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

Comment threadbacktracking/all_combinations.py Outdated
n = 4
k = 2
total_list = generate_all_combinations(n, k)
print_all_state(total_list)

@cclausscclaussJul 14, 2019

Copy link
Copy Markdown
Member

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=4k=2total_list=generate_all_combinations(n, k)
print_all_state(total_list)

Or just replace the four lines with:

if__name__=="__main__":
importdoctestdoctest.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
Copy Markdown
Member

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

@cclauss

Copy link
Copy Markdown
Member

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

@cclauss

Copy link
Copy Markdown
Member

@cclauss

Copy link
Copy Markdown
Member

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

@obelisk0114

Copy link
Copy Markdown
ContributorAuthor

It's ready to be merged.

@cclauss
cclauss merged commit 628794d into TheAlgorithms:masterJul 14, 2019
@cclauss

Copy link
Copy Markdown
Member

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

@obelisk0114@cclauss