472,352 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

Permutation String

2 2Bits
Hi everyone... Can someone help me?

Expand|Select|Wrap|Line Numbers
  1. def lexico_permute_string(s):
  2.     ''' Generate all permutations in lexicographic order of string `s`
  3.  
  4.         This algorithm, due to Narayana Pandita, is from
  5.         https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
  6.  
  7.         To produce the next permutation in lexicographic order of sequence `a`
  8.  
  9.         1. Find the largest index j such that a[j] < a[j + 1]. If no such index exists, 
  10.         the permutation is the last permutation.
  11.         2. Find the largest index k greater than j such that a[j] < a[k].
  12.         3. Swap the value of a[j] with that of a[k].
  13.         4. Reverse the sequence from a[j + 1] up to and including the final element a[n].
  14.     '''
  15.  
  16.     a = sorted(s)
  17.     n = len(a) - 1
  18.     while True:
  19.         yield ''.join(a)
  20.  
  21.         #1. Find the largest index j such that a[j] < a[j + 1]
  22.         for j in range(n-1, -1, -1):
  23.             if a[j] < a[j + 1]:
  24.                 break
  25.         else:
  26.             return
  27.  
  28.         #2. Find the largest index k greater than j such that a[j] < a[k]
  29.         v = a[j]
  30.         for k in range(n, j, -1):
  31.             if v < a[k]:
  32.                 break
  33.  
  34.         #3. Swap the value of a[j] with that of a[k].
  35.         a[j], a[k] = a[k], a[j]
  36.  
  37.         #4. Reverse the tail of the sequence
  38.         a[j+1:] = a[j+1:][::-1]
  39.  
  40. for s in lexico_permute_string('data):
OUTPUT :
aadt
aatd
adat
adta
atad
atda
daat
data
dtaa
taad
tada
tdaa

CAN OUTPUT TO :
data
data
daat
dtaa
dtaa
daat
data
adta
adat
atda
atad
aadt
aatd
tdaa
tdaa
tada
taad
tada
taad
adat
adta
aadt
aatd
atda
atad
Jun 15 '21 #1
3 3142
SioSio
272 256MB
I don't know what to help, but the syntax is wrong.
Expand|Select|Wrap|Line Numbers
  1. for s in lexico_permute_string('data’):
Jun 15 '21 #2
gieforce
2 2Bits
Expand|Select|Wrap|Line Numbers
  1. def lexico_permute_string(s):
  2.     ''' Generate all permutations in lexicographic order of string `s`
  3.  
  4.         This algorithm, due to Narayana Pandita, is from
  5.         https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
  6.  
  7.         To produce the next permutation in lexicographic order of sequence `a`
  8.  
  9.         1. Find the largest index j such that a[j] < a[j + 1]. If no such index exists, 
  10.         the permutation is the last permutation.
  11.         2. Find the largest index k greater than j such that a[j] < a[k].
  12.         3. Swap the value of a[j] with that of a[k].
  13.         4. Reverse the sequence from a[j + 1] up to and including the final element a[n].
  14.     '''
  15.  
  16.     a = sorted(s)
  17.     n = len(a) - 1
  18.     while True:
  19.         yield ''.join(a)
  20.  
  21.         #1. Find the largest index j such that a[j] < a[j + 1]
  22.         for j in range(n-1, -1, -1):
  23.             if a[j] < a[j + 1]:
  24.                 break
  25.         else:
  26.             return
  27.  
  28.         #2. Find the largest index k greater than j such that a[j] < a[k]
  29.         v = a[j]
  30.         for k in range(n, j, -1):
  31.             if v < a[k]:
  32.                 break
  33.  
  34.         #3. Swap the value of a[j] with that of a[k].
  35.         a[j], a[k] = a[k], a[j]
  36.  
  37.         #4. Reverse the tail of the sequence
  38.         a[j+1:] = a[j+1:][::-1]
  39.  
  40. for s in lexico_permute_string('data'):
Sorry...
Jun 15 '21 #3
SioSio
272 256MB
Does that mean including duplicate characters?
Expand|Select|Wrap|Line Numbers
  1. import itertools
  2. all = itertools.permutations('data', 4)
  3. for x in all:
  4.     print(''.join(x))
Jun 16 '21 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix....
27
by: onkar | last post by:
How to generate different permutations of n char array? ex : for n= 3, and basic string = abc bca cab bac cab ..... ... ..
3
by: Csaba Gabor | last post by:
I'm comparing the text of (snippets of) web pages which I expect to be quite different or quite similar. In the case where they are similar, I...
20
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I...
7
by: xirowei | last post by:
Let's say i create a String array that store 4 Alphabets {"A","B","C","D"} How can i get the result if i need permutation of 4P3 and 4P2? I had...
0
by: Vedavathi B | last post by:
I have Created a webapplication which displays the combination of inputs supplied. Permutation<string> permutations = new...
1
by: dar102 | last post by:
I need to generate permutation of some words (A T G C ) actually nucleotides for di-composition (eg AA AT AG AC), tri-composition (AAA AAT AAC AAG),...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.