0

Good day. I was searching for how to list all possible combinations of numbers 1 to n taken k numbers. I cam across some algorithms and recursive methods. But I am fairly new to coding so I have a hard time understanding complex codes. Can someone tell me a way I can think of this problem so that I can put it into code? I am not allowed to use intertools so I may have to do an iterative or recursive version.

all_combination(3 , 2)

should return

[[1,2],[1,3],[2,3]]
1

1 Answer 1

1

You can use:

from itertools import combinations 
list(combinations(range(1,n+1), k))

putting n=3, k=2 produces:

[(1, 2), (1, 3), (2, 3)]
Sign up to request clarification or add additional context in comments.

3 Comments

Standard library for the win :-)
@TonySuffolk66 Yeah, fortunately only "intertools" is forbidden.
Why - it is part of the standard language ? Banning it's use seems perverse to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.