# CodeGolf - Heads or Tails

### Instructions

Given an unknown input string i with a value of either heads or tails, return 1 for heads or -1 for tails with the shortest code.

### Sample not golfed code (55b):

if(i == "heads"){
print(1);
}else{
print(-1);
}


### Sample golfed code (16b):

print("t">i||-1)


Javascript was used for the example but it's not a requirement. Sorry if it's too simple for most users, it can be improved.

• Welcome to PPCG! This is too simple to be an interesting challenge. In the future, please consider posting challenges to the Sandbox, where they can get feedback before posting them live. May 16, 2015 at 4:06
• 6 answers, not so a bad challenge indeed. Try again May 16, 2015 at 13:33
• At 4 upvotes and 5 downvotes, your question wasn't really unpopular; it just got mixed reviews. While the task at hand is a little basic, it is well-defined and attracted 7 answers so far which feature several different approaches. Not that bad for a first attempt. May 16, 2015 at 16:31
• I'm so confused right now, yesterday -4, now +4, this sure ain't stackoverflow :P May 17, 2015 at 7:26
• I've been waiting for a short challenge like this for ages, most of the challenges are too long and complicated for a novice like me. May 18, 2015 at 11:52

# SmileBASIC, 15 bytes

H=2?VAR(I[T])-1


Not the shortest possible solution, but I didn't want to post a boring program that's the same as all the others.

The VAR "function" converts a variable name into a variable. VAR("X") = 1 is equivalent to X = 1, for example.

The program sets H to 2, then uses VAR() on the first character in the input string.
(I[T] is the same as I[0], because T is 0. I had to include T here because VAR is not allowed to create new variables).

If the input is heads, VAR() returns the variable H, which has a value of 2.
If the input is tails, VAR() returns the variable T, which has a value of 0.

Then, it subtracts 1 from this and outputs either -1 or 1.

Because variable names in SmileBASIC are not case sensitive, this program works for any capitalization of the input string.

Note:

1

bash-5.0$gema 't=-;*=1' <<< 'tails' -1  Try it online! # Zsh, 13 12 bytes -1 from @Isaac <<<$[#1%7-5]


man zshmisc, under Arithmetic Evaluation:

An expression of the form ##x where x is any character sequence such as a, ^A, or \M-\C-x gives the value of this character and an expression of the form #name gives the value of the first character of the contents of the parameter name.

# Java (JDK), 18 bytes

i->i=="heads"?1:-1


Try it online!

• Welcome! This is a well-golfed solution but when the input is heads it only works when i is interned because of the reference comparison (==). For this reason strings are usually compared in Java using String#equals. Your TIO works because your inputs are string literals, but this approach doesn't work in general. I don't think assuming strings are interned/literal is typical on here, so you should probably make sure your solution doesn't rely on that. Aug 19, 2019 at 18:58
• @Jakob yeah, but it's 25 now... Aug 19, 2019 at 19:40
• True, but it works for any input, which is the most important thing. Now the question is can you find a shorter version? (I think you'll find there's a 17-byte solution). Aug 20, 2019 at 2:45

# Brain-Flak, 52 34 bytes

({}[{}()()()])({<{{}}>[()()]}()<>)


Output is the top element of the stack

-18 bytes thanks to Jo King

Try it with tails!

• 34 bytes. Try to combine consecutive pops and pushes, especially in that first section
– Jo King
Aug 19, 2019 at 14:40
• Oh wow, could you make an explanation, since I don't really understand the second part? Aug 19, 2019 at 14:45
• Explanation plus another byte saved in the loop
– Jo King
Aug 19, 2019 at 15:01

# C-INTERCAL, 177 bytes

This is my first, and hopefully last, INTERCAL program. INTERCAL is a truly remarkable language. Those who haven't heard of it should immediately investigate (here is the manual I used).

PLEASEDO,1<-#2PLEASEDOWRITEIN,1PLEASEDO.1<-',1SUB#1'~#4DO(2)NEXTDO,1SUB#1<-#76(1)DO,1SUB#2<-#40(2)DO(3)NEXTDO,1<-#1DO,1SUB#1<-#116DOCOMEFROM(1)DOREADOUT,1(3)DOFORGET.1DORESUME#1


The program reads from standard input and writes to standard output. After printing the appropriate string, the program crashes with one of two humorous error messages depending on the input.

Try It Online

## Ungolfed

    PLEASE DO ,1 <- #2
PLEASE DO .1 <- ',1 SUB #1' ~ #4
DO (2) NEXT
DO ,1 SUB #1 <- #76
(1) DO ,1 SUB #2 <- #40
(2) DO (3) NEXT
DO ,1 <- #1
DO ,1 SUB #1 <- #116
DO COME FROM (1)
(3) DO FORGET .1
DO RESUME #1


Well, that didn't help much...

In the first three lines, we read 2 (#2 in INTERCAL) characters from the input into array ,1 and store in variable .1 whether the first character read has bit 2 set (mask of 4); in ASCII h is 0b1101000 and t is 0b1110100, so this variable now indicates whether the input is tails. This uses the "select" operator ~, which in this case performs both masking and shifting to select bit 2 as 0 or 1.

The rest of the program is an if/else conditional and a print statement. Unfortunately, control flow is very limited in INTERCAL. The only mechanism is a call stack that's pushed to when the program executes DO (x) NEXT. DO RESUME #1 then operates like a return statement (but with no return value), popping the stack and jumping to the next statement after the most recent DO (x) NEXT. But the value passed to DO RESUME can be any number. DO RESUME #3 pops the call stack three times and jumps to the third-from-last DO NEXT.

A similar construct is DO FORGET, which only pops the stack the specified number of times but does not jump. DO RESUME always jumps, so passing it 0 is a run-time error. But the instruction DO FORGET #0 is valid, and doesn't do anything.

And so with the statements DO (2) NEXT and DO (3) NEXT I push two locations onto the call stack. At (3) I then discard .1 frames, which has the effect of "forgetting" the call at (2) if and only if the input was tails. As a result, we end up just after either DO (2) NEXT or DO (3) NEXT after the return on the last line, depending on the input.

Each branch then loads the appropriate character data into ,1 (see here for why the values don't make any sense), and COME FROM (1), an inverted goto, joins the two branches at the output statement DO READ OUT. Control then continues, but luckily in either case the program crashes before printing anything else.

## Befunge, 28 bytes

28 bytes including whitespace and newlines:

<v"heads"
>94+%v
,,"-1"_1.@


Assumes that either heads or tails is put into the string above (this is the equivalent of assuming the input is stored in i). Works by performing modulo 13 on the top character on the stack - h divides exactly, t does not.

I also made a 22 character version (excluding whitespace/newlines) but the input has to be backwards:

"sdaeh"v
9
4
+
%
@,,"-1"_1.

• You can make this code shorted by using d instead of 94+ to get the number 13. Nov 28, 2015 at 18:19

# Unefunge 98, 15 or 16 bytes

'h-4j@.-1_1.@


The above code assumes input is stored on top os the stack in reverse order. There are two 16-byte solutions that don't make this assumption:

$'d-4j@.-1_1.@  Assuming input is on top of the stack in normal order. ~'h-4j@.-1_1.@  Reading input from stdin # Whitespace, 40 bytes [S S S N _Push_0][S N S _Duplicate_0][T N T S _Read_input_as_character][T T T _Retrieve][S S S T T T N _Push_7][T S T T _Modulo][S S S T S T N _Push_5][T S S T _Subtract][T N S T _Print_as_integer]  Letters S (space), T (tab), and N (new-line) added as highlighting only. [..._some_action] added as explanation only. Since Whitespace inputs one character at a time, I use that to my advantage here by only looking at the first character and porting @recursive's Stax answer (so make sure to upvote him as well!) Try it online (with raw spaces, tabs and new-lines only). Explanation in pseudo-code: Integer c = STDIN as character code-point c = c % 7 c = c - 5 Print c to STDOUT as integer  # 33, 12 bytes P110mz6dzcmo  Try it online! Requires heads or tails non-capitalised. This only accepts the first character of the input. If it needs to accept all input, replace the P with Ij. ## Explanation P | Get the first character of input as its ASCII value | ( h | t ) 110mz | Subtract 110 (-6 | 6 ) 6dz | Divide by 6 (-1 | 1 ) cm | Flip sign ( 1 |-1 ) o | Print it  # jq, 14 characters index("e")//-1  That's searching character position in the string. Unfortunately on not found it returns null, so the // (alternative operator) has to be used. (Still shorter than an .[:1]<"i" check as that results boolean and there is no way to cast that to integer.) Sample run: bash-5.0$ jq -R 'index("e")//-1' <<< 'heads'
1

bash-5.0\$ jq -R 'index("e")//-1' <<< 'tails'
-1


Try it online!

# ><>, 9 bytes

ab*i-6,n;


Try it online!

I'm honestly surprised that there wasn't a fish answer yet.

Subtracts the ASCII value of the first character input from 110 and divides by 6.

# brainfuck, 42 bytes

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


Try it online!

Checks if it is heads or tails by checking if the ordinal values of first two characters are only three away from each other.

# MathGolf, 3 bytes

'e=


Try it online!

Basically identical to a lot of other solutions. Checks the index of 'e', and returns -1 if not found.

# JavaScript (V8) - 19 Bytes

Since OPs solution returns 'true' and '-1' (see here), the following function:

f=i=>i[0]=='h'?1:-1


returns '1' and '-1' - see here. Just to be a smart-ass and so.

// Adaption of Java (SDK) version as a JS function.

# Runic Enchantments, 10 bytes

1i3%1C=?Z@


Works by determining if the 4th character is a d (compare to number literal 100) and if not, multiplies a 1 by -1, then prints and terminates.

1              Push 1 (the output)
3%          Get 4th character (0-indexed)
1C=?      Compare with 100 (decimal value of d)
Z     If false, multiply TOS with -1
@    Output entire stack (the only thing left with be a 1 or -1) and terminate


# R, 58 55 bytes

for(i in scan(,''))print(if(utf8ToInt(i)[1]%%8,-1,1))


Try it online!

Really poor attempt, but I the utf8ToInt values for "h","H","t","T" are 104, 72, 116, 84 respectively. As a result, "h" and "H" values are both evenly divisible by 8, while "t" and "T" are not. So a conversion and check for even divisibility yields the desired results.

This method compared to the following method takes different spellings of heads/Heads/HEADS/etc. and tails/Tails/TAILS/etc. As long as it starts with an h/H or t/T it will work properly.

EDIT: -3 bytes switching to a for loop. Prints each value on newline instead of a single array of 1's and -1's.

# R, 49 bytes

for(i in scan(,''))print(ifelse(i=="heads",1,-1))


This method was suggested by @user5957401 and is much simpler. The difference here is that this method assumes that the user will input strictly "heads" or "tails" (technically anything but "heads").

Try it online!

• ifelse(i=="heads",1,-1) seems like a quicker way to vectorize Aug 19, 2019 at 20:11
• That's pretty simple actually, I don't know why I didn't think of that. I'll add yours in. Aug 19, 2019 at 20:13

# Vyxal, 8 bytes

ec[1|u


Try it Online!

u is -1.