ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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?
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? :)
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.