Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: MRide on July 21, 2010, 11:18:01 am

Title: String Manipulation
Post by: MRide on July 21, 2010, 11:18:01 am
I've written the following code in BASIC:

Code: [Select]
randInt(1,9->X
prgmNUM2STR
Str3->Str1
randInt(0,9->X
prgmNUM2STR
While Str3=Str1
randInt(0,9->X
prgmNUM2STR
End
Str3->Str2
randInt(0,9->X
prgmNUM2STR
While Str3=Str2 or Str3=Str1
randInt(0,9->X
prgmNUM2STR
End
Str1+Str2+Str3->Str1

I would like to convert this code into Axe.  I know the randInt(s can be converted to rand^10->X, and I've replaced prgmNUM2STR with sub(NS).
If I'm using it correctly, I believe I can change Str3->Str1 into Copy(Str3,Str1,1) (I'm not entirely sure though.)
I am having the most trouble with Str1+Str2+Str3->Str1.

Also, If you know of a better way to generate a random three digit number where no two digits are the same, and store it as a string, that would be most appreciated.
Title: Re: String Manipulation
Post by: calcdude84se on July 21, 2010, 11:30:07 am
A better way is like this: (I'll explain it in a moment, and note I'm storing the data at L1 rather than Str1)
Code: [Select]
rand^10+48->T->{L1
rand^9+48->S+(S>=T)->R->{L1+1
rand^8+48->S+(S>=T)->S+(S>=R)->{L1+2
0->{L1+3
It uses three variables for temporary purposes (R, S, and T) and the result is stored at L1
If you must use Str1, (you shouldn't have to) replace every L1 with Str1, add Zeros(4)->Str1 somewhere in your code, and remove the last line.
Title: Re: String Manipulation
Post by: jnesselr on July 21, 2010, 12:02:01 pm
That code will not work, calcdude.  The first routine will return a random number 0-9, the second, 0-8, the third ,0-7.  In other words, they could still all end up being 7 or something.

[EDIT] Ignore this post.
Title: Re: String Manipulation
Post by: calcdude84se on July 21, 2010, 12:03:04 pm
that's what +(S>=T) and related are for. They pretty much skip over already-generated numbers.
Title: Re: String Manipulation
Post by: jnesselr on July 21, 2010, 12:07:30 pm
Okay, I see it.  I see how it works.  Wow, that is really amazing.  Wow, my bad.
Title: Re: String Manipulation
Post by: MRide on July 21, 2010, 12:12:52 pm
Yes, this is great. Thanks, calcdude.
Title: Re: String Manipulation
Post by: calcdude84se on July 21, 2010, 12:18:05 pm
You're welcome. Glad to help! :)
Title: Re: String Manipulation
Post by: ztrumpet on July 21, 2010, 12:21:54 pm
That's some neat code Calcdude!  Nice job! ;D