473,289 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 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 3274
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 ): if len( lst ) == 1: yield lst else: head =...
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. It just seems a waste to iterate through all those...
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 would like to display the more recent one and say...
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 split a number in several pieces in a random way...
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 NEED TO DO: Professor Snoop wants a program...
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 refer to many examples from the internet, but...
0
by: Vedavathi B | last post by:
I have Created a webapplication which displays the combination of inputs supplied. Permutation<string> permutations = new Permutation<string>(inputSet); ...
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), tetra, penta etc (one at a time) and then check in...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.