United-TI: Detect Whether Ans is a String or a Number - United-TI

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Detect Whether Ans is a String or a Number Teaser 72

#1 User is offline   bfr Icon

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 108
  • Joined: 13-February 06

Posted 29 December 2009 - 05:19 PM

In Ans is a string or a number (real or complex). Make a program that puts 0 or 1 in Ans depending on whether initially Ans was a string or a number. Note that this should work for any string or number.

I'm going to keep the size of mine hidden for now.
Mine is 30 bytes (after subtracting the length of the title from the memory menu size). ;P

This post has been edited by bfr: 29 December 2009 - 09:39 PM


#2 User is offline   Weregoose Icon

  • Authentic INTJ
  • Icon
  • Group: UTI Staff
  • Posts: 3,957
  • Joined: 25-November 04
  • Gender:Male
  • Location:CA, USA
  • Interests:TI-Basic and Web Development

Posted 30 December 2009 - 06:40 PM

19 bytes say any string and only zero will produce 1.

#3 User is offline   bfr Icon

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 108
  • Joined: 13-February 06

Posted 30 December 2009 - 08:37 PM

Yeah, I have a version that works like yours (with 0 being treated as a string) that is 18 bytes (19 bytes without subtracting the length of the program name).

I guess I might as well give the size for my robust version that works for "any string or number," which is 30 bytes (you can confirm that I haven't improved it since yesterday by highlighting the line after the "I'm going to keep..." line in my original post, which was last edited yesterday, if you really care :P).

#4 User is offline   Galandros Icon

  • Active Member
  • PipPipPipPipPip
  • Group: Members
  • Posts: 533
  • Joined: 29-August 08
  • Gender:Male
  • Location:dead end of Europe
  • Interests:knowledge

Posted 31 December 2009 - 09:51 AM

I have tried to solve this and haven't reach far...

But in the process I provoked some odd errors and curious behaviours... :blink:
Hobbing in TI projects.

#5 User is offline   DarkerLine Icon

  • ceci n'est pas une |
  • Icon
  • Group: UTI Staff
  • Posts: 8,328
  • Joined: 04-November 03
  • Gender:Male
  • Location:Durham, NC

Posted 31 December 2009 - 05:17 PM

I've gotten a version that works for any string or number in 23 bytes (returning 0 for a string and 1 for a number).

#6 User is offline   bfr Icon

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 108
  • Joined: 13-February 06

Posted 31 December 2009 - 06:25 PM

After reading your reply, I have a version that is 23 bytes large and returns 1 for a number and 0 for a string. However, strictly speaking, it doesn't work for any number or string...although exception is really insignificant. I'll PM you or something about the exception.

#7 User is offline   Builderboy2005 Icon

  • Advanced Newbie
  • PipPip
  • Group: Members
  • Posts: 51
  • Joined: 19-April 09
  • Gender:Male
  • Interests:Programming, making movies, handing out with friends :).

Posted 01 January 2010 - 05:11 PM

I am completely at a loss as to how to achieve this. I was able to get a basic technique that only worked for non-decimal non-zero non-negative numbers, but that was a very hacky way and used over 300 bytes. It would seem you can only use operators that can take either a string or a umber as arguments? The only ones i have found are the + operator, and the = and != operators. Am i on the right track? If not, could somebody bump me in the right direction? :)

#8 User is offline   bfr Icon

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 108
  • Joined: 13-February 06

Posted 01 January 2010 - 06:57 PM

You're on the right track. You can highlight the following facts (maybe just the first would be enough) that give hints about the way I made my versions. You pretty much just have to know these in order to make this routine the way I did.

Useful fact: You can store a string to a list variable (normally for attaching a formula to a list).

Another useful fact: You can store to the list LA simply by storing to the A character alone. For example: {0,1}->A .

Also, the (small) exception I was talking about in my 23-byte version, and possibly in DarkerLine's 23-byte version too: The fact that the + operator can be applied to strings and numbers is useful. However, an error is returned when one attempts to use the + operator with the "empty string" (which can be created by entering a quote on the home screen and then pressing enter). It's kind of a really insignificant and cheap exception though. =P I did not use the + operator in by 30-byte version that works for any number and any string (well, it works for any number and string as far as I know) .

This post has been edited by bfr: 01 January 2010 - 08:08 PM


#9 User is offline   Builderboy2005 Icon

  • Advanced Newbie
  • PipPip
  • Group: Members
  • Posts: 51
  • Joined: 19-April 09
  • Gender:Male
  • Interests:Programming, making movies, handing out with friends :).

Posted 01 January 2010 - 09:19 PM

Ahhhh, I knew both of those two facts, but failed to put them together! That is unimaginably clever!

#10 User is offline   ztrumpet Icon

  • Active Member
  • PipPipPipPipPip
  • Group: Members
  • Posts: 554
  • Joined: 06-May 09
  • Gender:Male
  • Location:Michigan
  • Interests:I'm mostly into calcs...

Posted 02 January 2010 - 01:21 AM

This is unimaginably clever! I can't figure this out at all. Those who have fiured it out are amazing. Great job!

After an hour of screaming at my calculator :) I've only come up with an 8 byte thing of code that works for every input except for the number zero...
Projects:
Gravity Dude 30%
Homescreen Game Pack 10%
Elmgon 6%

#11 User is offline   calcdude84se Icon

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 198
  • Joined: 09-August 09
  • Gender:Male
  • Location:I live in the U.S. and am on Eastern Time. That's all I'm telling you.

Posted 02 January 2010 - 04:19 AM

Until that was explained, I, also, was at a total loss. :)
People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster.
-Adam Osborne

#12 User is offline   bfr Icon

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 108
  • Joined: 13-February 06

Posted 03 January 2010 - 07:36 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.)


The 23-byte version that doesn't work for the empty string (the string that can be created by entering a quote on the home screen and pressing enter) - likely similar to DarkerLine's:

Quote


DelVar AAns->A
Ans=Ans+Ans or A


The 18-byte version that doesn't work for 0 - likely similar to Weregoose's and ztrumpet's:

Quote


DelVar AAns->A
not(A


Sorry for any confusion there may have been for "Make a program that puts 0 or 1 in Ans depending on whether initially Ans was a string or a number." My wording was unclear. I meant that if initially Ans is a string, then the resulting Ans could be a 0 or a 1 (it didn't have to be a 0), and if initially Ans is a number, then the resulting Ans would be 0 if the 1 indicated a string, and the resulting ans would be 1 if 0 indicated a string (the resulting Ans for an integer didn't have to be 1 - the resulting Ans for an integer would be 1 minus the resulting Ans for a string).

#13 User is offline   Weregoose Icon

  • Authentic INTJ
  • Icon
  • Group: UTI Staff
  • Posts: 3,957
  • Joined: 25-November 04
  • Gender:Male
  • Location:CA, USA
  • Interests:TI-Basic and Web Development

Posted 03 January 2010 - 08:15 PM

My smaller program was one byte larger than your smaller program for one reason: it's hard to use not( on a complex number.

#14 User is offline   bfr Icon

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 108
  • Joined: 13-February 06

Posted 03 January 2010 - 08:22 PM

Ah, I didn't know about that...thanks for informing me. I didn't really test my 18-byte and 23-byte ones anyway, though, because they already had at least one exception.

#15 User is offline   ztrumpet Icon

  • Active Member
  • PipPipPipPipPip
  • Group: Members
  • Posts: 554
  • Joined: 06-May 09
  • Gender:Male
  • Location:Michigan
  • Interests:I'm mostly into calcs...

Posted 03 January 2010 - 09:23 PM

Very nice brf! Insorak (on Omnimaga) had that small solution also. Great job!
Projects:
Gravity Dude 30%
Homescreen Game Pack 10%
Elmgon 6%

#16 User is offline   IAmACalculator Icon

  • In a state of quasi-hiatus
  • PipPipPipPipPipPipPipPip
  • Group: UTI Members
  • Posts: 1,568
  • Joined: 21-October 05
  • Location:Lost in Pi
  • Interests:My single biggest contribution is probably starting the speedcoding competitions. I'm only a mediocre programmer at best, but I don't really mind. My focus now is on music and composing. Violin rocks!

Posted 04 January 2010 - 12:11 PM

There's also the lame 68k version:
getType(ans(1))
If you're new to the art of the calc, please read this FAQ and save yourself a world of pain. You should also read the manual.

"Be true to who you want to become, but accept who you are and what you are meant to be." ~ IAmACalc (now with new addendum)

#17 User is offline   Galandros Icon

  • Active Member
  • PipPipPipPipPip
  • Group: Members
  • Posts: 533
  • Joined: 29-August 08
  • Gender:Male
  • Location:dead end of Europe
  • Interests:knowledge

Posted 07 January 2010 - 01:35 PM

View Postbfr, on Jan 3 2010, 07:36 PM, said:

Here's my 30-byte solution that works for any input:

Quote


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


This also can detect whether Ans is a list or a number.
More generally, it can detect if Ans is number or something else.
Really clever.

A little off topic now. max(, min(, arithmetic and comparison can be used to both numbers and lists...
I am trying to do a routine that detects a number or a list (not formula lists) but smaller than this. If I had found a smaller solution, I would have opened the teaser.
Hobbing in TI projects.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users