35
\$\begingroup\$

Based on this question from Code Review.

Given precisely three positive integers, return the product of all of the distinct inputs. If none of the inputs are distinct, return 1. That is, implement the function:

\$ f(a,b,c) = \cases{1 & $a = b = c $ \\ a & $ b = c $ \\ b & $ a = c $ \\ c & $ a = b $ \\ a b c & otherwise } \$

Shortest code in each language wins.

Test cases

a, b, c -> f(a,b,c)
7, 7, 7 -> 1
3, 3, 5 -> 5
2, 6, 6 -> 2
3, 4, 3 -> 4
1, 2, 3 -> 6
12, 9, 16 -> 1728
\$\endgroup\$
  • 1
    \$\begingroup\$ "(a=b)∩(b=c)" - did you mean \wedge instead of \cap? \$\endgroup\$ – ngn Feb 11 at 6:54
  • 12
    \$\begingroup\$ I’m pretty sure simply writing a = b = c is more readable. \$\endgroup\$ – Fatalize Feb 11 at 8:42
  • \$\begingroup\$ I wrote it the way I did thinking it would be clearer to an average reader of this site, but really I was probably overthinking it. I've changed it now, thanks for the suggestions! \$\endgroup\$ – FryAmTheEggman Feb 11 at 16:18
  • \$\begingroup\$ @ngn I like \land. same effect, more meaningful (stupid \cap and \cup) \$\endgroup\$ – qwr Feb 12 at 3:33

37 Answers 37

1
2
1
\$\begingroup\$

Ruby, 38 bytes

->*a{r=1;a.map{|w|(a-[w])[1]&&r*=w};r}

Try it online!

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

Whitespace, 178 bytes

[S S S N
_Push_0][S N
S _Dupe_0][T    N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_A][S N
S _Dupe_input_A][S N
S _Dupe_input_A][T  N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_B][S N
S _Dupe_input_B][S N
S _Dupe_input_B][T  N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_C][S N
S _Dupe_input_C][S T    S S T   S N
_Copy_0-based_2nd_input_B][T    S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_C==B][S N
S _Dupe_input_C][S T    S S T   T   N
_Copy_0-based_3rd_input_A][T    S S T   _Subtract][N
T   S T N
_If_0_Jump_to_Label_C!=B&&C==A][S T S S T   S N
_Push_0-based_2nd_input_A][S T  S S T   S N
_Push_0-based_2nd_input_B][T    S S T   _Subtract][N
T   S N
_If_0_Jump_to_Label_PRINT][T    S S N
_Multiply][T    S S N
_Multiply][N
S N
N
_Jump_to_Label_PRINT][N
S S T   N
_Create_Label_C!=B&&C==A][S N
N
_Discard_input_C][N
S N
N
_Jump_to_Label_PRINT][N
S S S N
_Create_Label_C==B][S N
S _Dupe_input_C][S T    S S T   T   N
_Copy_0-based_3rd_input_A][T    S S T   _Subtract][N
T   S T T   N
_If_0_Jump_to_Label_C==B&&C==A][S N
N
_Discard_input_C][S N
N
_Discard_input_B][N
S N
N
_Jump_to_Label_PRINT][N
S S T   T   N
_Create_Label_C==B&&C==A][S S S T   N
_Push_1][N
S S N
_Create_Label_PRINT][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.

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

Whitespace work-flow:

Integer A = STDIN as integer
Integer B = STDIN as integer
Integer C = STDIN as integer                   (the stack now contains [A,B,C])
If(C==B): Jump to Label C==B
If(C==A): Jump to Label C!=B&&C==A
If(A==B): Jump to Label PRINT                  (top of the stack is here C)
Multiply top two
Multiply top two
Jump to Label PRINT                            (top of the stack is here A*B*C)

Label C!=B&&C==A:
  Discard top (C)
  Jump to Label PRINT                          (top of the stack is here B)
Label C==B:
  If(C==A): Jump to Label C==B&&C==A
  Discard top (C)
  Discard top (B)
  Jump to Label PRINT                          (top of the stack is here A)

Label C==B&C==A
  Push 1                                       (top of the stack is here 1)
Label PRINT
  Print top of the stack as integer to STDOUT

Since the inputs are guaranteed to be positive, it uses heap-address 0 for input A; heap-address A for input B; and heap-address B for input C.

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

Kotlin, 52 51 43 bytes

filter{z->count{z==it}<2}.fold(1){s,c->s*c}

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Not sure if defining the function in the header like I did is allowed, if it isn't i'll have to go back to the 51 byte answer \$\endgroup\$ – Quinn Feb 12 at 19:23
0
\$\begingroup\$

Racket, 74 bytes

λ(n)(apply *(map(λ(x)(if(=(length x)1)(car x)1))(group-by identity n))))

Try it online!

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

Excel, 60 bytes

=IF(A1=B1,IF(A1=C1,1,C1),IF(B1=C1,A1,IF(A1=C1,B1,A1*B1*C1)))

Does not use features like FILTER, only available to Office Insiders.

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

SQL, 245 155 bytes

 DECLARE @a INT=2,@b INT=4,@c INT=8;WITH v AS(SELECT x FROM(VALUES(@a),(@b),(@c))t(x) GROUP BY x HAVING COUNT(*)=1)SELECT ISNULL(EXP(SUM(LOG(x))), 1) FROM v

Expanded:

DECLARE @a INT=2,
        @b INT=4,
        @c INT=8;

WITH v
     AS (SELECT x
           FROM(VALUES(@a),
                      (@b),
                      (@c))t(x)
          GROUP BY x
         HAVING COUNT(*) = 1)
SELECT ISNULL(EXP(SUM(LOG(x))), 1)
  FROM v

The straightforward solution is only 150 .. but it is boring. :)

DECLARE @a INT=2,@b INT=4,@c INT=2;SELECT CASE WHEN @a=@b AND @a=@c THEN 1 WHEN @a=@b THEN @c WHEN @b=@c THEN @a WHEN @a=@c THEN @b ELSE @a*@b*@c END
DECLARE @a INT=2,
        @b INT=4,
        @c INT=2;

SELECT CASE
         WHEN @a = @b
              AND @a = @c THEN 1
         WHEN @a = @b THEN @c
         WHEN @b = @c THEN @a
         WHEN @a = @c THEN @b
         ELSE @a * @b * @c
       END 
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I'm not too familiar with SQL golfing, but the DECLARE statement seems out of place. I feel as though it either shouldn't be part of the answer, or what you've written isn't precisely reusable? I could easily be wrong - I'm just stating my current understanding. \$\endgroup\$ – FryAmTheEggman Feb 13 at 4:55
  • \$\begingroup\$ Fair point - I am uncertain as well.. I can "assume" the @a, @b, and @c are the parameters passed into a function whose body is the SELECT statement, but is that correct? I could use some guidance there as well. \$\endgroup\$ – Forty3 Feb 14 at 2:59
0
\$\begingroup\$

Stax, 13 bytes

üΩ1↑♂Åò←3♠'W▐

Run and debug it

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks for your post! Unfortunately this doesn't seem to meet the requirements for the case where two of the numbers are the same (i.e. for 5 5 7 this returns 35 when it should return 7). Since I don't understand Stax myself, it would also be great if you could include an explanation of what approach your code is using. \$\endgroup\$ – FryAmTheEggman Feb 20 at 18:47
1
2

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.