LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2004, 06:16 AM   #1
sougata
LQ Newbie
 
Registered: Oct 2004
Location: india
Posts: 1

Rep: Reputation: 0
permutation of a string


hi everybody,
suppose i have string like "abc"-- now i want to print all the unique appearances of that string- menas- abc, bca, cba, cab, bac and acb. The factorial of the length of the string is needed that is for the string "abc" the length is 3 and factorial of 3 is 6. so 6 unique occurances of the string "abc" is needed- but how should i get them? if anyone can help me plz?
 
Old 10-28-2004, 07:38 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i find it hard to see how a problem like this could come up in a real environment. Conveserly it's absolutely perfect material for a college course in programming. if this is homework from school or wherever, please do not expect other people to do your work for you. you are unliekly to learn much by handing somethign you cut and pasted off some forum.
 
Old 10-28-2004, 05:28 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Hey, I just did it, it's not too bad, but I don't want to post it here, cuz it really does sound like your hw which really wouldn't be helping you. I'll give you a hint though. Recursion is probably best.
Think of it like this... for each letter in the string there belongs (n-1)! permutations using all other letters. So you can break it down. If we had the string "asdf", then the number of permutations starting with "a" is (n-1)! and the same goes for s,d, and f. Now if we go even further, starting with a, we know the first permutation would start with an "as". we know that for permutations starting with "as" there are (n-2)!, the first one starting with "asd". On the next level, we know that the first one will start with "asd", and there are (n-3)! permutations for the string that starts with "asd". In this case (4-3)! = 1. So if we use this as our base case, then we can print our first permutation. "asdf". Now we go up a level back to "as" again, the we know the next permutation starts with "asf" (since it was the next letter), with (n-3)! permutations. Since there is only one permutations starting with "asf", we put it out, "asfd". Now we go back up, and we no longer have any more permuations for "as", so we go to the next letter, and do the same process with "ad". We can then just keep following the same steps. Don't know if that made sense, but it works.
 
Old 10-28-2004, 05:35 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
now that's a good way to answer a homework question... why can't others do that?
 
Old 10-31-2004, 04:18 PM   #5
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
i've been working with permutations for 15 years (trying to find the holy grail of rubik's cube optimal solvers (also use perms for unscrambling the daily word jumble in the newspaper or at jumble.com)) and i was going to post one of my early permutation functions, but seeing what has been said here about homework i'm afraid i'll get my head ripped off if i do. i don't see why it should be assumed the person is doing homework, and even if they are what's the bfd about helping them out?
 
Old 10-31-2004, 04:36 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
a) It's against the rules, which in itself should be
a good enough explanation.

b) the rule actually makes sense in encouraging the
person to learn, and attempt to solve the problem. If
the guy comes back with an algorithm that doesn't
quite do what he expects you're more than welcome
to help him setting his flawed idea straight.

It's not helping him, however, if you do his work. Just
imagine you have a child that you "help" with homework
all the time, and that child grows up to be a doctor and
someone else at university did all the exams and
practical study of anatomy for him. Would you let him
do a heart-transplant on you? :)



Cheers,
Tink
 
Old 10-31-2004, 05:04 PM   #7
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Didn't the old war games dialers do this? War game dialer does''nt sound like the right name but I'm trying to remember back to the 70's.

I had one for my old 8 bit machine.
 
Old 11-01-2004, 09:13 AM   #8
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
ok, fair enough on the homework issue.

i was interested in comparing notes on perm algorithms. without getting into actual code, i'm wondering if the recursive solution described by 95se will produce the list of perms in the proper sorted order. i have a recursive solution that i wrote many years ago that produces the perm list, but not in order. i eventually moved to a different, non-recursive, solution method (converting integers to corresponding permutation strings, ie: 1->abc, 2->acb, 3->bac, 4->bca, 5->cab, 6->cba) which allows generating sorted perm lists.

i thought that i had a recursive solution that produced a sorted list, but looking back through my code archive i've only found the non-sorted solution (if i had it, i lost it). so if someone can tell me they've done it, i'll figure it out (again, probably).
 
Old 11-01-2004, 11:02 AM   #9
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
ok, i figured out the recursive & sorted method so never mind.
 
Old 11-05-2004, 12:03 AM   #10
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Sorted? Output example of my prog.
./abc
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba

What's your email, you can take a look if you want.
 
Old 11-05-2004, 03:40 PM   #11
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
95se,

got it, thanks.
 
Old 11-05-2004, 11:57 PM   #12
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
no, prob
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
[c] string help saiz66 Programming 4 10-06-2004 01:00 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
C String Help Scrag Programming 6 04-14-2004 12:47 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration