# What calculator is this datafile for?

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.

• @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. Jun 15 '20 at 16:19
• 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. Jun 15 '20 at 16:33
• Suggested test case (contains another version string in the gibberish): **TI86*asdf*9**TI92Pasd Jun 15 '20 at 19:31
• I should have bought a TI calculator... You know TI-BASIC?
– user92069
Jun 15 '20 at 22:56
• @pppery albeit this is a valid testcase, it would invalidate a lot of currently existing answers, so I'll let it pass :p Jun 16 '20 at 7:27

# brainfuck, 3938 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:

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

# 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.

### 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

# 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!.

• @pppery Ugh, that means I'm going to have to add a header to the test suite...
– Neil
Jun 15 '20 at 21:56

# 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.

,,..34{<{__5|..,;,;#@$_#_.@; Try it online! Accepts input on stdin and prints to stdout. # Explanation 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.

# 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!

# 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.

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

Try it online!

# 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)

# 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
]
};
}

# PHP, 3938 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!

• 'TI-'.$s[4].$s[5] can be written as "TI-$s[4]$s[5]", to save 2 bytes. Jun 16 '20 at 14:48

# C (gcc), 5048 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!

# 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.

# 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_(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.

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

# 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.

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

# Bash, 51 bytes

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.