Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: ztrumpet on January 01, 2010, 10:57:26 pm

Title: Figure out if Ans is a Number or a String
Post by: ztrumpet on January 01, 2010, 10:57:26 pm
Here's a challenge for all of you: In pure basic, figure out if Ans is a Number or a String.
It is possible, but I havn't figured out how. :P
If you figure it out, you win a cookie. :D

I have a solution for everything except the number zero. :(  I can't figure out how to compleatly do it.
For more info, http://www.unitedti.org/forum/index.php?showtopic=9191
Title: Re: Figure out if Ans is a Number or a String
Post by: cooliojazz on January 01, 2010, 11:25:30 pm
as long as ans /= B: B->A:Ans->A:If A=B:Ans is a string
Does that help?
EDIT: oops! i messed up i'll think about and redo it when i remember how it actually went, since i made this a while ago for a ramdom use of mine
Title: Re: Figure out if Ans is a Number or a String
Post by: Insorak on January 02, 2010, 04:02:49 pm
Hmm...

Code: [Select]
:DelVar AAns-->A
:(A and 1)

is very short, and will give 1 in Ans if input was a number, or 0 if input was a string. I'm just trying to make zero work...
Title: Re: Figure out if Ans is a Number or a String
Post by: ztrumpet on January 02, 2010, 04:15:38 pm
Insorak, that's exactly what I got. :D

Delvar AAns->A
not(not(A

I just wonder how to do it and have everything work.
Title: Re: Figure out if Ans is a Number or a String
Post by: Insorak on January 02, 2010, 04:32:40 pm
I want to initialize A to something other than 0 before storing Ans to A. But that's obviously impossible, because initializing A changes Ans. >_<
Title: Re: Figure out if Ans is a Number or a String
Post by: ztrumpet on January 02, 2010, 04:33:39 pm
Nope. :)

To intilize A to 500 without changing Ans:
For(A,500,0:End
;D
Title: Re: Figure out if Ans is a Number or a String
Post by: Insorak on January 02, 2010, 04:37:54 pm
Would seem to work... but still not really what I was looking for.

Assume that A = i before program is run.

:Ans->A
:1 and imag(A

would work, giving 1 for all strings and 0 for all numbers.

Unfortunately, you can't increment a for loop to an imaginary value.
Title: Re: Figure out if Ans is a Number or a String
Post by: miotatsu on January 03, 2010, 08:49:50 am
:DelVar A
:Prompt A
:0<abs(A
^best i got
Title: Re: Figure out if Ans is a Number or a String
Post by: bfr on January 03, 2010, 03:45:35 pm
Here's my 30-byte solution that works for any input:
Quote

DelVar AFor(B,1,1
Ans->A
Ans->B
A=B

One would probably want to close to have For(B,1,1:End instead if this routine were being used within a larger program.  Also, I was hoping I could get rid of DelVar A and set A and B to different values using just For( by defining B in terms of A, but I'm not sure how to do that in a small way that always works (for positive and negative values of A, etc.)

See the original post (http://www.unitedti.org/forum/index.php?showtopic=9191&st=0&p=139646&#entry139646).
Title: Re: Figure out if Ans is a Number or a String
Post by: ztrumpet on January 03, 2010, 04:21:07 pm
That's awesome!  You've done a very good job!  That's very, very smart.  Way to go!
Title: Re: Figure out if Ans is a Number or a String
Post by: Builderboy on January 03, 2010, 05:18:49 pm
Genius, just explodes my mind that this can be done through all the little exploits that exist on the calculator.
Title: Re: Figure out if Ans is a Number or a String
Post by: ztrumpet on January 03, 2010, 05:48:03 pm
Also, I was hoping I could get rid of DelVar A and set A and B to different values using just For( by defining B in terms of A, but I'm not sure how to do that in a small way that always works (for positive and negative values of A, etc.)
What about

For(B,A,A+1:End

Would that work?
Title: Re: Figure out if Ans is a Number or a String
Post by: bfr on January 03, 2010, 08:34:29 pm
I think it generally would, but:
- It would make the program be 31 bytes instead of 30
- The program would fail sometimes - for example, if A were a large value like 10^98 (E98) before the program ran
Title: Re: Figure out if Ans is a Number or a String
Post by: miotatsu on January 03, 2010, 10:11:41 pm
this is very impressive, it really shows me how much there still is for me to learn about this wonderful language
Title: Re: Figure out if Ans is a Number or a String
Post by: Builderboy on January 03, 2010, 10:21:09 pm
Before this, I never though there was even a remote chance that Basic would do something like type checking.  Its something we have been telling beginers is impossible for a long time.  Now we know differently!
Title: Re: Figure out if Ans is a Number or a String
Post by: Insorak on January 04, 2010, 03:35:24 pm
How about between a list and string, or between a matrix and list... *wince*
Title: Re: Figure out if Ans is a Number or a String
Post by: calc84maniac on January 04, 2010, 04:03:59 pm
How about between a list and string
Code: [Select]
Delvar LAAns->A
"->A
not(dim(LA

Edit: Fixed a bug

Edit2:
or between a matrix and list...
Code: [Select]
Delvar Adim(Ans->A
not(A
Title: Re: Figure out if Ans is a Number or a String
Post by: cooliojazz on January 04, 2010, 06:05:05 pm
Well, all those solutions are better than the one I had by far. :P Just how do you people think of these things?!?!
Title: Re: Figure out if Ans is a Number or a String
Post by: ztrumpet on January 06, 2010, 09:47:37 pm
Is it possible to tell if Ans is a matrix vs a string?  Have fun with this one!
Title: Re: Figure out if Ans is a Number or a String
Post by: Galandros on January 07, 2010, 08:40:13 am
I am trying to do the list versus number. The string or number works too for list and number but maybe there is a smaller solution.

I will put the routines in TI-BD for archive. (EDIT: the number or string is already there ;) )