-2

I am trying to make a python program that produces all combinations of an n-1 number with a length L:

enter image description here

I've tried using the itertools but to not avail. I am trying to make it so that it produces the output above when I give it the arguments 5 3 in my terminal. I am using python3. An explanation would be great. Here is my code.

import sys
import itertools

def comb(A,n,k,p,lo):
    stuff = [A, n, k,p,lo]
    return itertools.combinations(stuff, len(stuff)

if __name__ == "__main__":
    d = len(sys.argv)>3
    n = int(sys.argv[1])
    k = int(sys.argv[2])
    A = []
    for i in range(k):
         A.append(0)
    if d: print("n:",n,"k:",k)
    comb(A,n,k,0,0)
martineau
  • 119,623
  • 25
  • 170
  • 301
J.Doe
  • 31
  • 2
  • 8
  • It looks like your indentation is off a little and that makes it hard to see what's going on. What is the output you get now? – munk May 02 '18 at 01:33
  • where do you print the `comb(A,n,k,0,0)` Why do you have 2 zeroes in the list for `stuff`? Why can't you just to this? `combinations(range(n), L)` where n is your number – Abhishek Dujari May 02 '18 at 01:34
  • My bad it looks like it got messed up in the copy over. I'll correct it. – J.Doe May 02 '18 at 01:34
  • What is "an n-1 number"? What is a "number with a length L?". – Paul Cornelius May 02 '18 at 01:34
  • An n-1 is how it sounds. For example 5 would produce all combos 0-4. L is the length of the combination list. – J.Doe May 02 '18 at 01:36
  • "An n-1 number" sounds to me like nonsense. I think you mean that n is the first command line argument to your program, and L is the second command line argument. Then your task is to produce all L-length combinations of integers between 0 and n-1 (inclusive). Is that right? – Paul Cornelius May 02 '18 at 01:45
  • @PaulCornelius Yes – J.Doe May 02 '18 at 01:47
  • It doesn't appear that you've even bothered to read the [`itertools.combination()` documentation](https://docs.python.org/3/library/itertools.html#itertools.combinations). – martineau May 02 '18 at 02:10

3 Answers3

0

print(itertools.combinations(range(int(sys.argv[1])),int(sys.argv[2])))

edited: to intify the input as pointed out

Abhishek Dujari
  • 2,343
  • 33
  • 43
  • It's giving me a compile error. This is how I am trying to implement it. It says str object cannot be used as an integer. def comb(A,n,k,p,lo): print(itertools.combinations(range(sys.argv[1]),sys.argv[2])) – J.Doe May 02 '18 at 01:43
  • 1
    You need to convert the arguments to ints - replace with `sys.argv[1]` with `int(sys.argv[1])` and likewise for argv[2]. – Paul Cornelius May 02 '18 at 01:46
  • Yes sorry I was typing on the phone and get lazy. @Paul Cornelius is correct – Abhishek Dujari May 02 '18 at 07:22
0
import sys
from itertools import combinations

if __name__ == "__main__":
    d = len(sys.argv)>3
    n = int(sys.argv[1])
    k = int(sys.argv[2])
    A = []
    for i in range(n):
        A.append(i)
    if d: 
        print("n:",n," k:",k)
    val = combinations(A, k)
    for i in val:
        print(i)

You don't need a helper function I don't think

Erik Iverson
  • 309
  • 1
  • 11
0

This should run with python 3:

test.py

import sys
import itertools

def printArray(inputArray):
  print('[', str(inputArray)[1:-1], ']')

def comb(arr, r):
  output = itertools.combinations(arr, r)
  for x in output:
    printArray(x)

if __name__ == "__main__":
  arr = range(int(sys.argv[1]))
  r = int(sys.argv[2])
  comb(arr, r)

Just use: python3 test.py 5 3

output is:

[ 0, 1, 2 ]
[ 0, 1, 3 ]
[ 0, 1, 4 ]
[ 0, 2, 3 ]
[ 0, 2, 4 ]
[ 0, 3, 4 ]
[ 1, 2, 3 ]
[ 1, 2, 4 ]
[ 1, 3, 4 ]
[ 2, 3, 4 ]
Community
  • 1
  • 1
BenD
  • 118
  • 1
  • 10
  • @ J.Doe Since the result of combinations is an iterable we have to loop through it after in order to print. Additionally, for the first argument we can't just send int(sys.argv[1]) because itertools.combinations expects an iterable to be sent – BenD May 02 '18 at 01:56
  • my last question is how would you print this out as an array instead of strings. Mainly how do you print [] instead of ()? – J.Doe May 02 '18 at 02:05
  • I think you could create a little helper function and then for each small array print out a specific format. I'll play around with it and update – BenD May 02 '18 at 02:12
  • @ J.Doe for python3 you could do the printArray function I've added – BenD May 02 '18 at 02:24
  • thank you. How would I eliminate the white space on sides? – J.Doe May 02 '18 at 02:27
  • No problem. For the white spaces you'll probably have to try some things out. You could start at these resources: [print](http://thomas-cokelaer.info/tutorials/python/print.html) and [Similar question](https://stackoverflow.com/questions/11178061/print-list-without-brackets-in-a-single-row) – BenD May 02 '18 at 02:31