17
\$\begingroup\$

The challenge today is very simple. The task is to determine the TI calculator for which the submitted datafile was made.

The datafiles always start with a string **TI, the version (described below), and other data you can ignore.

Now, the versions you need to recognize are:

95* => TI-95
92P => TI-92+
92* => TI-92
89* => TI-89
86* => TI-86
85* => TI-85
84P => TI-84+
84* => TI-84
83* => TI-83
83P => TI-83+
82* => TI-82
81* => TI-81
74* => TI-74
73P => TI-73+

Examples

**TI95*   => TI-95
**TI83P   => TI-83+
**TI73P   => TI-73+
**TI85*   => TI-85
**TI83PGG => TI-83+
**TI86*asdf*9**TI92Pasd => TI-86

Rules

You can assume the input is always correct, is at least 7 characters long, and has been crafted for one of the TI calculators listed above.

This is code golf, so the shortest answer wins.

I/O rules and loopholes apply.

\$\endgroup\$
  • 1
    \$\begingroup\$ @Adám ">You can assume the input is always correct, is at least 7 characters long" - the input can be longer than 7 characters, and for the output to be correct, you can't output any gibberish other than the version. \$\endgroup\$ – Kamila Szewczyk Jun 15 at 16:19
  • 1
    \$\begingroup\$ Perhaps if gibberish can be there, and should be ignored, you can 1) indicate where that gibberish can occur, and 2) add some more examples. \$\endgroup\$ – Abigail Jun 15 at 16:33
  • 1
    \$\begingroup\$ Suggested test case (contains another version string in the gibberish): **TI86*asdf*9**TI92Pasd \$\endgroup\$ – Zgarb Jun 15 at 19:31
  • 3
    \$\begingroup\$ I should have bought a TI calculator... You know TI-BASIC? \$\endgroup\$ – user92069 Jun 15 at 22:56
  • 2
    \$\begingroup\$ @pppery albeit this is a valid testcase, it would invalidate a lot of currently existing answers, so I'll let it pass :p \$\endgroup\$ – Kamila Szewczyk Jun 16 at 7:27

24 Answers 24

16
\$\begingroup\$

brainfuck, 39 38 35 bytes

Saved 3 bytes thanks to @Dorian.

,+++>,>,.,.<<.-->>,.,.,[-<->]<[<.<]

Try it online!

This abuses the fact that the characters *,+, and - are very close to each other in ASCII.

Ungolfed:

,+++>  create a minus from the first asterisk
,>      store the second asterisk for comparison later
,.      display T
,.      display I
<<.-->> display the minus from line 1 and turn it into a plus
,.      display first number
,.      display second number

,[-<->]<[  if the last char is not an asterisk from line 2
  <.<      display the plus from line 5
]

Original 38 byte solution:

>,+++>,>,.,.<<.-->>,.,.,<[->-<]>[<<.<]

Original 39 byte solution:

,+++>,>,.,.<<.-->>,.,.,<[->-<]>[<<.[-]]
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Really nice code. 34 bytes with a few unnecessary commands removed. \$\endgroup\$ – Dorian Jun 16 at 9:16
  • \$\begingroup\$ @Dorian That doesn't work, it outputs a trailing (some unprintable byte) at the end. \$\endgroup\$ – user202729 Jun 16 at 9:17
  • 1
    \$\begingroup\$ For the TIO implementation at least it's possible to delete the first > because the tape wraps around. \$\endgroup\$ – user202729 Jun 16 at 9:18
  • 1
    \$\begingroup\$ Oh, you're right, it outputs a nul char after the plus sign. Well then 35 bytes ;-) \$\endgroup\$ – Dorian Jun 16 at 9:23
  • 1
    \$\begingroup\$ I didn't even think about tape wrapping - nice optimizations! Thanks! \$\endgroup\$ – nununoisy Jun 16 at 14:37
13
\$\begingroup\$

TI-BASIC (TI-83), 40 bytes

Ans→Str1
sub(Ans,5,2
If sub(Str1,7,1)="P":Ans+"+
"TI-"+Ans

Takes input as a string via Ans (allowed by default). The character count differs from the byte count because TI-BASIC is tokenised: Str1 and sub( are 2-byte tokens; Ans, , If , and all other characters used (including newlines) are 1-byte tokens.

Sample output

Uses this emulator.

enter image description here

Explanation

Ans→Str1                    # store input in Str1
sub(Ans,5,2                 # implicitly set Ans to 2-digit calculator ID (5th and 6th input characters)
If sub(Str1,7,1)="P":Ans+"+ # if the 7th input character is P, add "+" to Ans
"TI-"+Ans                   # output implicitly
| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

perl -pl, 24 bytes

s;(\d..).*;-$1;;y;P*;+;d

Try it online!

Keep the first digit and the next two characters, removes anything after that, and inserts a - before the first digit. Replaces any P with a +. Removes any *.

Reads lines from STDIN, writes versions to STDOUT.

Editted to deal with trailing garbage.

| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

TI-Basic (TI-84 Plus CE), 54 bytes (50 tokens) 61 bytes (56 tokens)

"TI-"+sub(Ans,5,3
If sub(Ans,6,1)="P
Then
sub(Ans,1,5)+"+
Else
sub(Ans,1,5
End
Ans

Usage: "**TI89*":prgmNAME if the program is named NAME.

TI-Basic is a tokenized language, the sub( token is two bytes and all the other tokens used here are one byte (e.g. digits, punctiation, newline, Ans, If, Then, Else, End).

Takes the input in Ans and implicitly prints the result stored in Ans.

Encodes the - with the subtraction - (0x71), not the negative - (0xB0)

Explanation:

"TI-"+sub(Ans,5,3  # 19 tokens, 21b: "**TIXX?..." -> "TI-XX?"
If sub(Ans,6,1)="P # 12 tokens, 13b: If the "?" above is "P"
Then               #  2 tokens,  2b: Then
sub(Ans,1,5)+"+    # 11 tokens, 12b:   Replace "P" with "+"
Else               #  2 tokens,  2b: Else
sub(Ans,1,5        #  7 tokens,  8b:   Remove the last character
End                #  2 tokens,  2b: End If
Ans                #  1 tokens,  1b: Last line's Ans is implicitly printed
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Can you give a quick example on how (before calling the program) one can set Ans to have the value we want? \$\endgroup\$ – Ismael Miguel Jun 16 at 15:40
  • 2
    \$\begingroup\$ @IsmaelMiguel Sure, "**TI89*":prgmNAME if the program is named NAME. I added this to the answer \$\endgroup\$ – pizzapants184 Jun 16 at 21:11
  • 1
    \$\begingroup\$ Thank you, I didn't knew you could do that :O over 10 years later and I'm still learning stuff about these calculators. \$\endgroup\$ – Ismael Miguel Jun 16 at 21:15
5
\$\begingroup\$

Keg, -ir, 15 bytes

__,,\-,,,P=[\+,

Try it online!

Huh, who'da thunk a simple stack approach would beat everyone else?

Explained

__,,\-,,,P=[\+,
__              # Pop the two asterisks at the start
  ,,            # Print the "TI"
    \-,         # Followed by a dash
       ,,       # Then the number embedded in the input
         P=     # See if the last character is P
           [\+, # If it is, print a "+", otherwise, do nothing and end execution
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Now you're a total genius. I can't even golf it. \$\endgroup\$ – user92069 Jun 16 at 5:33
5
\$\begingroup\$

05AB1E, 16 bytes

Sorry, didn't understand the comment...

7£¦¦…*PI„ +„I-ª‡

Try it online!

Explanation

7£               Take a 7-char prefix.
  ¦¦             Remove the first 2 characters.
    …*PI         "*PI"
        „ +„I-ª  With: [" ", "+", "I-"] respectively
               ‡ Transliterate
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yeah it does? Maybe my comment wasn't too clear (sorry about that), but I meant to replace „*P„ +‡'I¡„I-ý with …*PI„ +„I-ª‡ for -2. The 7£¦¦ still remains as is. Try it online: 7£¦¦…*PI„ +„I-ª‡. \$\endgroup\$ – Kevin Cruijssen Jun 16 at 8:53
5
\$\begingroup\$

JavaScript (ES6), 34 bytes

s=>'TI-'+s[4]+s[5]+[{P:'+'}[s[6]]]

Try it online!

Commented

s =>         // s = input string: **TIddp[…]
             //                   0123456
  'TI-' +    // append the prefix
  s[4] +     // append the first digit (5th character)
  s[5] +     // append the second digit (6th character)
  [          // wrapper to make sure that undefined is turned into an empty string
    {P: '+'} //   define an object with a single key 'P' mapped to the value '+'
    [s[6]]   //   and attempt to retrieve this '+', using the 7th character
             //   (which is either 'P' or '*')
  ]          // end of wrapper
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Nice, I ended up with the exact same code. I don't think there's a shorter version in js. +1 for ""+[undefined] \$\endgroup\$ – C5H8NNaO4 Jun 17 at 13:54
5
\$\begingroup\$

Python 3, 52, 43 36 bytes

lambda x:"TI-"+x[4:6]+"+"*(x[6]>"*")

Try it online!

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Nice, you can save a few bytes using "+" * condition instead of condition and.. or... Try it online!. \$\endgroup\$ – Surculose Sputum Jun 16 at 0:46
  • 1
    \$\begingroup\$ Well done! for the future, TIO has a separate section for codegolf submissions in the link section, which you can just copy and paste into the answer for a nice, preformatted submission \$\endgroup\$ – Dion Jun 17 at 4:50
  • \$\begingroup\$ @Dion thank you, that makes formatting my answers a lot easier \$\endgroup\$ – Daniel H. Jun 17 at 11:24
3
\$\begingroup\$

QuadR, 21 bytes

I
^\*.|\*.*
P.*
I-

+

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Looks like you can do ^.. instead of ^\*. \$\endgroup\$ – math junkie Jun 15 at 17:04
3
\$\begingroup\$

J, 24 bytes

'TI-',4 5&{,'+'#~'P'=6&{

Try it online!

How it works

'TI-',4 5&{,'+'#~'P'=6&{
                 'P'=6&{ 6th position = 'P'?
            '+'#~        either take 0 or 1 '+' and
      4 5&{,             append it to the 4th and 5th char
'TI-',                   prepend 'TI-'
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ nb: out of pure boredom, I engineered an unpacker for programs too, written in J, and this has inspired me for this challenge. extract =: (2=('**TI'-:4$])+[:+/(14 3$'95*92P92*89*86*85*84P84*83*83P82*81*74*73P')-:"1~14 3$4(}.)7{.]){[:>'Incorrect datafile.';~[:(_2&}.)73}.] \$\endgroup\$ – Kamila Szewczyk Jun 16 at 7:31
3
\$\begingroup\$

Retina 0.8.2, 19 bytes

1M!`TI..P?
P
+
I
I-

Try it online! Link includes test cases. Explanation:

1M!`TI..P?

Extract the TI, 2 digits, and a possible trailing P.

P
+

If there was a P then change it to a +.

I
I-

Add a - after the I.

Retina 1 saves a byte because it uses 0L instead of 1M!.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @pppery Ugh, that means I'm going to have to add a header to the test suite... \$\endgroup\$ – Neil Jun 15 at 21:56
3
\$\begingroup\$

Charcoal, 16 bytes

TI-§θ⁴§θ⁵×+⁼P§θ⁶

Try it online! Link is to verbose version of code. Explanation:

TI-

Print the initial TI-.

§θ⁴§θ⁵

Print the 4th and 5th characters of the input (0-indexed).

×+⁼P§θ⁶

Print as many +s as there are Ps equal to the 6th character of the input.

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Hexagony, 28 bytes

,,..34{<{__5|..,;,;#@$_#_.@;

Try it online! Accepts input on stdin and prints to stdout.

Explanation

Color

I'll use the test case **TI83P for illustration. Execution starts at the top left corner and follows instruction pointer 0 (IP0) along the red path.

  • ,, reads and discards the first two asterisks from stdin.
  • ,; reads the T from stdin and prints it to stdout.
  • ,; does the same but for the letter I.

At this point, the current memory edge holds the integer 73 (the ASCII character I).

  • # takes the current memory edge modulo 6 (1 in this case) and transfers control to the corresponding instruction pointer.

This pauses IP0 at the @ command and picks up execution in the top right corner, following IP1 along the blue path.

  • { moves the memory pointer to its left neighbor. Memory edges are 0 by default, so this is easier than trying to zero the previous edge.
  • 45; prints - to stdout.
  • ,;,; reads the two digits of the model number and prints them. The zigzag pattern allows reuse of the same instructions that printed the letters TI.
  • { moves the memory pointer again. This instruction is superfluous, however, because
  • , overwrites the current memory edge with the next character read from stdin.

This character is either * (ASCII 42) or P (ASCII 80).

  • If it is *,
    • # transfers control back to IP0 (because 42 mod 6 = 0). IP0 picks up at the @ instruction, which finally terminates the program.
  • If it is P,
    • # transfers control to IP2 (because 80 mod 6 = 2), which starts in the right corner and follows the grey path.
    • $ skips the ; instruction to avoid printing P to stdout instead of +.
    • {43; prints + to stdout.
    • @ terminates the program.

I had a lot of fun (ab)using the # instruction with this solution.

Image courtesy of Timwi's HexagonyColorer.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

str, 21 bytes

2G2G'-:2G:g'P='+x:O;q

Explanation:

2G                     Read the `**` at the beginning (this stays on the stack for the entire program, but does nothing)
  2G                   Read the string `TI`
    '-:                Concatenate a - to it, to get `TI-`
       2G:             Read the next two characters (the version number) and concatenate them to the assembled string
          g            Read the next character (either `P` to signify I need to add a plus sign, or garbage)
           'P=         Check if it's equal to `P`, producing a zero or one
              '+x:     Repeat the string `+` a number of times equal to the number on top of the stack (zero or one in this case), and concatenate it to the assembled string
                   O;q Output the result, then tell the interpreter to ignore the rest of the input

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Befunge-93, 27 bytes

~~~,~,"-",~,~,~"P"-#@_"+",@

Try it online!

Read two characters (and ignore them). Read a char (T) and print it. Read another char (I) and print it. Print a -. Read a char (a digit) and print it. Read another char (a digit) and print it. Read a char, end the program if it's not a P, else print a + and end the program.

We cannot just read a number and print a number, as that will be printed with an additional trailing space.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Haskell, 35 bytes

f s="TI-"++s!!4:s!!5:['+'|s!!6>'*']

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

MathGolf, 16 bytes

7<2/╞├'-⌐~'P='+*

Try it online.

Explanation:

7<                 # Leave the first 7 characters of the (implicit) input-string
  2/               # Split it into parts of size 2
    ╞              # Discard the first part (the "**")
     ├             # Remove and push the first part to the stack (the "TI")
      '-          '# Push "-"
        ⌐          # Rotate the stack once towards the left (so the remaining pair is at
                   # the top again)
         ~         # Pop and dump its contents onto the stack (the number and "*"/"P")
          'P=     '# Check if the top of the stack equal "P" (1 if truthy; 0 if falsey)
             '+*  '# Repeat "+" that many times ("+" if it was "P"; "" if not)
                   # (output the entire stack joined together implicitly as result)
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Rust macro, 135 bytes

macro_rules!f{(* * T I$($n:literal)*P$($x:tt)*)=>{[84,73,45,$($n+48,)*43]};(* * T I$($n:literal)**$($x:tt)*)=>{[84,73,45,$($n+48),*]};}

Defines a macro f that takes a list of tokens and returns an array of integers (ASCII chars).

try it online

Explanation

macro_rules! f {
    (                  // if the input has the following tokens:
        * * T I        //  * * T I
        $($n:literal)* //  zero or more literals (called n)
        P              //  P
        $($x:tt)*      //  anything
    ) => {             // expand to this:
        [              //  an array
           84, 73, 45, //   with the ASCII codes for TI-
           $($n+48,)*  //   add 48 to each n and append a comma
           43          // the ASCII code for +
        ]
    };
    (                  // if the input has the following tokens:
        * * T I        //  * * T I
        $($n:literal)* //  zero or more literals (called n)
        *              //  *
        $($x:tt)*      //  anything
     ) => {            // expand to this:
        [              //  an array
           84, 73, 45, //   with the ASCII codes for TI-
           $($n+48),*  //   add 48 to each n and join with commas
        ]
    };
}
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

PHP, 39 38 36 bytes

fn($s)=>"TI-$s[4]$s[5]".'+'[$s[6]<P]

Try it online!

Basically a port of Arnauld's answer with a bit of inspiration from Sqepia's one for the "+".. Let's say that's what I would have done anyway ;)

EDIT: saved 1 byte using < instead of !=

EDIT2: Thanks to Ismael Miguel for saving another 2 bytes using vars in double quotes!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 'TI-'.$s[4].$s[5] can be written as "TI-$s[4]$s[5]", to save 2 bytes. \$\endgroup\$ – Ismael Miguel Jun 16 at 14:48
1
\$\begingroup\$

C (gcc), 50 48 47 bytes

Thanks to Arnauld for the initial suggestion.

Not much to say here except that I conditionally print a plus sign at the end of the string if a P is in the seventh position.

f(char*s){printf("TI-%.2s%s",s+4,"+"+s[6]%80);}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Java 8, 35 bytes

a->"TI-"+a[4]+a[5]+(a[6]>79?"+":"")

Try it online.

Explanation:

a->           // Method with character-array parameter and String return-type
  "TI-"       //  Return "TI-"
  +a[4]+a[5]  //  Appended with the (0-based) 4th and 5th characters of the input
  +(a[6]>79?  //  And if the (0-based) 6th character is larger than 'O' (thus 'P'):
     "+"      //   Append a "+"
    :         //  Else:
     "")      //   Append nothing more

C# .NET, 35 bytes

s=>"TI-"+s[4]+s[5]+(s[6]>79?"+":"")

Only difference is the => instead of ->, and the input parameter is a string instead of character-array. Apart from that it's the same as the Java lambda above.

Try it online.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Whitespace, 150 bytes

[S S S N
_Push_0][S N
S _Dupe_0][S N
S _Dupe_0][S N
S _Dupe_0][T    N
T   S _Read_as_character_(*1)][T    N
T   S _Read_as_character_(*2)][T    N
T   S _Read_as_character_(T)][T T   T   _Retrieve][S N
S _Dupe][S N
S _Dupe][T  N
S S _Print_as_character][T  N
T   S _Read_as_character_(I)][T T   T   _Retrieve][S N
S _Dupe][S N
S _Dupe][T  N
S S _Print_as_chartacer][S S S T    S T T   S T N
_Push_45_-][T   N
S S _Print_as_character][T  N
T   S _Read_as_character_(digit1)][T    T   T   _Retrieve][S N
S _Dupe][S N
S _Dupe][T  N
S S _Print_as_character][T  N
T   S _Read_as_character_(digit2)][T    T   T   _Retrieve][S N
S _Dupe][S N
S _Dupe][T  N
S S _Print_as_character][T  N
T   S _Read_as_character_(*/P)][T   T   T   _Retrieve][S S S T  S T S S S S N
_Push_80][T S S T   _Subtract][N
T   S N
_If_0_Jump_to_Label_P][N
N
N
_Exit][N
S S N
_Create_Label_P][S S S T    S T S T T   N
_Push_43_+][T   N
S S _Print_as_character]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Character c = STDIN as character   (the first leading "*")
c = STDIN as character             (the second leading "*")
c = STDIN as character             (the "T")
Print c as character to STDOUT
c = STDIN as character             (the "I")
Print c as character to STDOUT
Print '-' as character to STDOUT
c = STDINT as character            (the first digit)
Print c as character to STDOUT
c = STDIN as character             (the second digit)
Print c as character to STDOUT
c = STDIN as character             (the '*'/'P')
If(c == 'P'):
  Print '+' as character to STDOUT
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

SimpleTemplate, 52 bytes

So far, it is the 2nd longest answer, but works...

{@setA argv.0}TI-{@echoA.4,A.5}{@ifA.6 is equal"P"}+

Just naively grabs the characterss from the string, at a predefined position. Nothing fancy...


Ungolfed:

Both codes behave exactly the same:

{@set argument argv.0}
{@echo "TI-", argument.4, argument.5}
{@if argument.6 is equal to "P"}
    {@echo "+"}
{@/}

Everything outside the code is just printed out.
Basically, TI- and {@echo "TI-"} do the same exact thing.


You can try this on http://sandbox.onlinephpfunctions.com/code/1a2faee21e43109e148b057df65d2f119780ca45

I've implemented this version, and an aditional version as a function, to compare outputs.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Bash, 51 bytes

read a
b=${a:6:1}
b=${b%\*}
echo TI-${a:4:2}${b:++}

Try it online!

Uses standard input and output.

a holds the first line, any other input is ignored.

b holds the * or P after the number, and then the * is removed.

data is assumed to be correct.

The echo outputs the three pieces, adding the + only if b is non-blank.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.