# Compare four integers, return word based on maximum

This function should take four integer inputs (a,b,c,d) and return a binary word based on which values equal the maximum of the four.

The return value will be between 1 and 0xF.

For example:

a = 6, b = 77, c = 1, d = 4

returns 2 (binary 0010; only 2nd-least significant bit is set corresponding to b being sole max value)

a = 4, b = 5, c = 10, d = 10

returns 0xC (binary 1100; 3rd- and 4th-least significant bits set corresponding to c and d equaling max value)

a = 1, b = 1, c = 1, d = 1

returns 0xF (binary 1111; all four bits set because all values equal the max)

Here is a simple implementation:

int getWord(int a, int b, int c, int d)
{
int max = a;
int word = 1;
if (b > max)
{
max = b;
word = 2;
}
else if (b == max)
{
word |= 2;
}
if (c > max)
{
max = c;
word = 4;
}
else if (c == max)
{
word |= 4;
}
if (d > max)
{
word = 8;
}
else if (d == max)
{
word |= 8;
}
return word;
}

return value can be string of 0's and 1's, bool / bit vector, or integer

• I have a solution in a golfing language, which uses the builtins Reverse, Maximum, Equality-check, Join, Convert from binary to integer, Convert from integer to hexadecimal. Does this means my score is 1 due to the Equality-check? I have the feeling this is too much focused on regular languages, and even for those it's not 100% clear what the scoring for let's say a Maximum-builtin.. :S – Kevin Cruijssen Mar 4 at 7:39
• I would suggest you try to: 1. change this question to code-golf, which only care about the number of bytes. 2. or, restrict to some certain language (certain version of compiler/interpreter please), and list all statements and operators allowed, and how to score them. – tsh Mar 4 at 7:43
• 1 is a better option, IMO. I think this makes a perfectly good code-golf question and I can't see any benefit that would come from restricting the languages available for answers – senox13 Mar 4 at 7:54
• I updated my question to remove the criteria. Let me know it it's still unclear – Mr Anderson Mar 4 at 8:09
• Should I output a decimal number? Or may I output 4 binary digits instead? – tsh Mar 4 at 8:51

# Jelly, 2 bytes

Takes input as [d,c,b,a]. Returns a list of Booleans.

Ṁ=

Try it online!

Maximum

= equal to (implies the other argument is the original argument; vectorises)

# R, 17 bytes

max(a<-scan())==a

Try it online!

Returns a vector of booleans. Since this output has been confirmed, this is preferable over numerical output, as that one is almost twice longer:

### R, 33 bytes

sum(2^which(max(a<-scan())==a)/2)

Try it online!

# APL (Dyalog Unicode), 4 bytesSBCS

Anonymous tacit prefix function. Takes [a,b,c,d] as argument. Returns a bit-Boolean array.*

⌈/=⌽

Try it online!

⌈/ Does the maximum of the argument

= equal (vectorises)

the reverse of the argument?

* Note that APL stores arrays of Booleans using one bit per value, so this does indeed return a 4-bit word, despite the display form being 0 0 1 0.

2 bytes saved thanks to proud haskeller

map=<<(==).maximum

Try it online!

• You can output a string. – Adám Mar 5 at 5:44
• Writing map instead of (<$>) would be two bytes shorter! – proud haskeller Mar 7 at 6:23 • @proudhaskeller Good catch. Can't believe that I didn't see that one. – Wheat Wizard Mar 7 at 13:40 # Perl 6, 12 bytes {$_ X==.max}

Try it online!

Anonymous code block that takes a list of integers and returns a list of booleans. If we need to return as a number, it's +4 bytes to wrap the inside of the code block with 2:[...].

### Explanation:

{          }  # Anonymous code block
10010

## Explanation

import sys
# convert list of string parameters to list of integers
v=[*map(int,sys.argv[1:])]
# get max
m=max(v)
# init outstring
s=""
# walk through list
for e in v:
# prepend to outstring: int(True)=>1, int(False)=>0
s=str(int(e==m))+s
# print out result
print(s)

# C# (Visual C# Interactive Compiler), 26 bytes

n=>n.Select(a=>a==n.Max())

Try it online!

Takes input in format [d,c,b,a]. All the others down below take input as [a,b,c,d]

# C# (Visual C# Interactive Compiler), 35 bytes

n=>n.Select((a,b)=>n[3-b]==n.Max())

Returns an IEnumerable<bool>.

Try it online!

# C# (Visual C# Interactive Compiler), 39 bytes

n=>n.Select((a,b)=>n[3-b]==n.Max()?1:0)

Returns an IEnumerable<int>, which represent bits.

Try it online!

# C# (Visual C# Interactive Compiler), 49 bytes

n=>{for(int i=4;i-->0;Write(n[i]==n.Max()?1:0));}

Prints a binary string to STDOUT.

Try it online!

• IEnumerable<bool> is acceptable. – Adám Mar 5 at 5:45

# PHP, 54 bytes

while($i<4)$argv[++$i]<max($argv)||$r+=1<<$i-1;echo$r; or while($i<4)$argv[++$i]<max($argv)||$r+=1<<$i;echo$r/2;

take input from command line arguments. Run with -nr or try them online.

Here's a JS version that outputs as binary

update: Shorter with join, and without the lookup:

# JavaScript (Node.js), 42 bytes

a=>a.map(x=>+(x==Math.max(...a))).join('')

Try it online!

Previous, with lookup, 49 bytes

a=>a.map(x=>[0,1][+(x==Math.max(...a))]).join('')

Try it online!

Previous, with reduce, 52 bytes:

a=>a.reduce((y,x)=>y+[0,1][+(x==Math.max(...a))],'')

Try it online!

fa=>a.map(x=>+(x==Math.max(...a))).join('')
console.log(f([ 4, 1,77, 6])) // 0010
console.log(f([10,10, 5, 4])) // 1100
console.log(f([ 1, 1, 1, 1])) // 1111

• You can safely remove [0,1][...] since you're using an index that already is either $0$ or $1$. – Arnauld Mar 4 at 16:38
• @Arnauld seems obvious now. Thanks! – Pureferret Mar 4 at 16:54

# C# (Visual C# Interactive Compiler), 51 bytes

x=>{for(int m=x.Max(),i=4;i-->0;)x[i]=x[i]==m?1:0;}

Try it online!

Above is an anonymous function that outputs by modifying an argument. Output is an array of 1's and 0's.

Below is a recursive function that outputs an integer.

# C# (Visual C# Interactive Compiler), 60 bytes

int f(int[]x,int i=3)=>i<0?0:2*f(x,i-1)|(x[i]==x.Max()?1:0);

Try it online!

Both functions take input as a 4-element array.

[d, c, b, a]
• You don't need to output an integer. – Adám Mar 5 at 5:43
• @Adam - thanks :) I realized this after posting while I was working on my other answer. Before I had a chance to change, there was another C# answer that used a lot of the good tricks. – dana Mar 5 at 19:11

# Python 2, 35 bytes

lambda i:[int(x==max(i))for x in i]

Try it online!

Takes input in the format [d,c,b,a] as with the accepted answer from Adám so I guess it is OK.

Alternative for 41 if it's not...

# Python 2, 41 bytes

lambda i:[int(x==max(i))for x in i][::-1]

Try it online!

# Python 3, 42 bytes

f=lambda a:list(map(lambda x:x==max(a),a))

Simply returns a list of whether the element is the max for each element in the input. -2 bytes if you don't count the f= assignment.

Try it online!

• f= doesn't count except in recursive functions – ASCII-only Mar 13 at 3:47

## Batch, 92 bytes

@set m=%1
@set f=@for %%i in (%*)do @
%f%set/a"m=m+(m-=%%i)*(m>>31)
%f%cmd/cset/a!(m-%%i)

Takes arguments as command-line parameters in reverse order. Works by arithmetically calculating the maximum of the parameters by reducing over them and adding on only positive differences from the running maximum, then mapping over each parameter again this time comparing it to the maximum. Conveniently cmd/cset/a doesn't output a newline, so the results are automatically concatenated together. The %f% simply saves 5 bytes on what would be a repeated construct.