From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Permutation generator in ada library Date: Fri, 03 Oct 2014 09:28:35 +0300 Organization: Tidorum Ltd Message-ID: References: <5b7788b4-323f-42db-99e9-66c989992abb@googlegroups.com> <1d9e9cd4-3087-4357-9353-db4424a16e9b@googlegroups.com> <197bb172-1356-45dc-ac0a-9dcc25c1525d@googlegroups.com> <983aef70-7b6a-45dc-a7ac-a92874ff66de@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: individual.net dndeqkrouUh0yj/ZgsyKwgzXBBMrnmlvqvtIdo6SZ+8igTjSWG Cancel-Lock: sha1:pgxj13BCJQxQVtT07hKb9o+T4KI= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <983aef70-7b6a-45dc-a7ac-a92874ff66de@googlegroups.com> Xref: news.eternal-september.org comp.lang.ada:22022 Date: 2014-10-03T09:28:35+03:00 List-Id: On 14-10-03 07:51 , Stribor40 wrote: > Is there Ada library for this ..... > > Code from that link gives wrong output...lets say for 3 gives.. > > 1 2 3 > 1 3 2 > 2 1 3 > 2 3 1 > 3 1 2 > 3 2 1 > 3 2 1 <- this shouldnt be there?? Right. The author of this program has confused herself with the "Is_Last" output parameters of the procedures, and has not documented them properly. They work differently for the two procedures: - Generic_Perm.Set_First returns Is_Last = True when the permutation it PRODUCES is the last one (that is, when N = 1). - Generic_Perm.Go_To_Next returns Is_Last = True when the permutation it is GIVEN is the last one, and it does not produce a new permutation in that case. The loop in Print_Perms consequently should have a more complex form, for example like this: Perms.Set_To_First(P, Done); Print (P); if not Done then -- There is more than one permutation, print them: loop Perms.Go_To_Next(P, Done); exit when Done; -- There was a next permutation, print it: Print(P); end loop; end if; -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .