From: Brian Drummond <brian3@shapes.demon.co.uk>
Subject: Re: permutation of strings
Date: Tue, 07 Oct 2014 11:22:49 GMT
Date: 2014-10-07T11:22:49+00:00 [thread overview]
Message-ID: <d2QYv.373310$GP4.354113@fx15.fr7> (raw)
In-Reply-To: 460f8eef-a6b8-4774-ac30-edba9f366676@googlegroups.com
On Mon, 06 Oct 2014 22:38:25 -0700, mockturtle wrote:
> On Tuesday, October 7, 2014 7:23:53 AM UTC+2, Stribor40 wrote:
>> I have created this code using some online tutorials and some examples
>> that i tried to learn... i havent been able to find whats wrong with my
>> code....i am guessing what my variable j is somehow getting reset
>> during the recursive calls...
>>
>> i have been trying to retrace recursive calls by writing each call on
>> the on the paper but i get lost trying to track it down......
>>
>> my code spits out numbers...123 123 123 123 123 123 instead of 123 132
>> 213 231 321 312 so i was hoping if someone can take a look and suggest
>> how to fix this....
>>
>> i pasted the code here it is ready to be compiled and there are no
>> errors when it is compiled....http://pastebin.com/rDF46RbR
>
> At the moment I do not have a compiler at hand (I'm having breakfast
> :-), but I see at least one strange thing in your code: in the loop "for
> Index ...." I would expect calls to "change(s, i, Index)" rather than
> "change(s, i, j)". With the latter there is no difference between
> different iterations and this could give rise to your error.
Nailed it by inspection!
> By the way, there is no need to have j declared "in out" in Change,
> since j is not changed. Moreover, I suspect that with the "in out"
> specification the compiler will complain, since you cannot change a loop
> index.
Having corrected the real bug, it did complain...
If I might add a couple more style changes:
1) Eliminate global variable "tmp" which should be local to procedure
"change"
2) Eliminate global variable "Start" and potential for off-by-1 bugs, by
using the array attributes in the main call to Perms
Works as I would expect anyway.
- Brian
---------------------------------------------
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure permutation is
str : String := "123";
procedure perms (s : in out String; i : in Integer; n : in Integer);
procedure change (s : in out String; i : in Integer; j : in Integer);
procedure perms (s : in out String; i : in Integer; n : in Integer) is
j : Integer := i;
begin
if i = n then
put(s);
New_Line;
else
for Index in j..n loop
change(s,i,index);
perms(s,i+1,n);
change(s,i,index);
end loop;
end if;
end perms;
procedure change (s : in out String; i : in Integer; j : in Integer)
is
tmp : Character;
begin
tmp := s(i);
s(i) := s(j);
s(j) := tmp;
end change;
begin
perms(str,str'first,str'last);
end permutation;
prev parent reply other threads:[~2014-10-07 11:22 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-07 5:23 permutation of strings Stribor40
2014-10-07 5:38 ` mockturtle
2014-10-07 11:22 ` Brian Drummond [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox