473,507 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Permutation String

2 New Member
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 3280
SioSio
272 Contributor
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 New Member
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 Contributor
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
5582
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
7119
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
6475
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
2947
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
41265
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
3191
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
11726
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
4073
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
1026
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
1161
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
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.