permutation

algorithm that lists all possible combinations of given elements in gr

Page 1 of 1

1 Replies - 896 Views - Last Post: 23 November 2008 - 10:02 PM Rate Topic: -----

#1 immila  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-November 08

permutation

Post icon  Posted 23 November 2008 - 02:04 PM

Hello, i am trying to learn Python and here is my problem:
In my program, i am trying to get the following result:
let say i have list of charachters E ['a','c',] and i have a N=3
As a result I should get (9 strings in total) -
aaa
aac
aca
caa
acc
cac
cca
ccc

My function bellow works, but it does not repeat the same char
it will print
ac
ca
need help, thanks

def comb(L1, n, L2=[]):
if not n: return pr(L2)
for i in range(len(L1)):
L2.append(L1.pop(i))
comb(L1, n-1, L2)
L1.insert(i, L2.pop())

Is This A Good Question/Topic? 0
  • +

Replies To: permutation

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: permutation

Posted 23 November 2008 - 10:02 PM

Here's a string permutation in python that could reversed to solve your problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1