44
\$\begingroup\$

iOS 11 has a bug that makes the result of 1+2+3 to be 24. This is related to the animation speed, but anyway:

The task is to make 1 + 2 + 3 == 24. But only that. So you should provide a function that correctly sums most sequences but returns 24 when the arguments are 1, 2 and 3 in any order.

Example inputs:

1 2 => 3
3 4 => 7
1 2 3 4 5 6 7 8 9 => 45
3 2 1 => 24
2 1 3 => 24
1 1 => 2
1 2 3 => 24
40 2 => 42
1 2 2 4 => 9
1 2 3 4 1 2 3 => 16
1 => 1
1 23 => 24
0 1 2 => 3
3 2 3 => 8

Input can be in any format as long as your code accepts any number of arguments.

  • Support for negative numbers isn't required (all non negative numbers are required to work, that includes 0)
  • We assume correct input

Differences from another similar question: "What do you get when you multiply 6 by 9? (42)":

  • In this case your function is required to accept any number of arguments. The old question specifies exactly 2.
  • In this case order doesn't matter, while the old question specified that order 6 9 is required and 9 6 should be evaluated correctly.
\$\endgroup\$
  • 23
    \$\begingroup\$ Also, iOS 11 doesn't work like that. It should be like this instead. (code explanation) \$\endgroup\$ – user202729 Oct 24 '17 at 9:39
  • 3
    \$\begingroup\$ @user202729 The question is probably inspired by iOS 11. I don't think the OP is asking you to replicate it entirely. \$\endgroup\$ – Okx Oct 24 '17 at 9:42
  • 2
    \$\begingroup\$ @Okx exactly. This is for fun, not to implement it 1 to 1. Of course this could be changed to the user202729 proposal, but if he want he can create new challenge with such task. \$\endgroup\$ – Hauleth Oct 24 '17 at 9:45
  • 3
    \$\begingroup\$ Are the inputs integer? \$\endgroup\$ – xnor Oct 24 '17 at 15:46
  • 9
    \$\begingroup\$ One reason this is a beautiful challenge is because of the property linked to wherein this combination of numbers is very special. The other reason this is a beautiful challenge is that it pokes fun at Apple for prioritizing (their idea of) UX over functionality. \$\endgroup\$ – NH. Oct 24 '17 at 22:05

43 Answers 43

1
2
0
\$\begingroup\$

C++, 109 bytes

#import<list>
int f(std::list<int>l){l.sort();int s=0;for(int i:l)s+=i;return l==std::list<int>{1,2,3}?24:s;}

Try it online!

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

Perl 5, 25 bytes

[sort@_]~~[1..3]?24:sum@_

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ As it stands, this is a code fragment. You need to include all of the bytes in your score, including command line options. \$\endgroup\$ – Xcali Oct 30 '17 at 22:30
0
\$\begingroup\$

Racket, 49 bytes

(λ x(if(equal?(sort x <)'(1 2 3))24(apply + x)))

Try it online!

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

Perl 5, 34 bytes

[sort@_]~~[1..3]?24:0+map{1..$_}@_

Try it online!

This is similar to my other answer, but does not use the import sum function. Instead it creates an array whose size is the sum of the number. But only works for non-negative integers.

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

Ruby, 29 bytes

->a{a.sort==[*1..3]?24:a.sum}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 20 15 bytes

5 bytes saved thanks to @EriktheOutgolfer

{4*⍵[⍋⍵]≡⍳3}×+/

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ {4*⍵[⍋⍵]≡⍳3}×+/ \$\endgroup\$ – Erik the Outgolfer Oct 24 '17 at 19:08
0
\$\begingroup\$

q/kdb+, 24 bytes

Solution:

(sum x;24)1 2 3~x:asc(),

Try it online!

Note: Link is to a K (oK) port of the K4 version below as there is no TIO for q/kdb+.

Examples:

q)(sum x;24)1 2 3~x:asc(),1
1
q)(sum x;24)1 2 3~x:asc(),1 2
3
q)(sum x;24)1 2 3~x:asc(),1 2 3
24
q)(sum x;24)1 2 3~x:asc(),1 2 3 4
10
q)(sum x;24)1 2 3~x:asc(),1 3 2
24

Explanation:

q is interpreted right-to-left:

(sum x;24)1 2 3~x:asc(), / the solution
                     (), / convert to list if not already a list (cannot sort atom)
                  asc    / sort list ascending
                x:       / store input in variable x                      
          1 2 3~         / is sorted list equal to list 1 2 3? boolean 0 or 1
(     ;  )               / two item list which we index into
       24                / index 1 (true), the fake result 24
 sum x                   / index 0 (false), sum the input list

Notes:

  • K4 version that aligns with TIO link: (+/x;24)1 2 3~x@<x:(), for 22 bytes
  • Q is just syntactic sugar on top of K4, sum is +/, asc is x@<x
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 111 153 bytes

#define A(x)if(v[x]>v[x+1]&c>2)t=v[x],v[x]=v[x+1],v[x+1]=t;
i;s;t;f(c,v)int*v;{for(s=i=0;i<c;s+=v[i++]);A(0)A(1)A(0)return c!=3|*v-1|v[1]-2|v[2]-3?s:24;}

Try it Online!

+42: Fixed {3,2,3}, {1,1,1}, {2,2,2}, etc.

I will try to golf more later.

f is a function which takes the length of the array as an int and a pointer to the first element of the array (of unsigned ints) and returns the 'IOS 11 Calculator Sum' of the array.

Ungolfed (Old, broken version):

#define not_one_two_or_three(index) (arr[index] > 3 || arr[index] == 0)
int counter;
int sum;
int ios_sum(int length, unsigned int *arr) {
    sum = 0;
    for (counter = 0; counter < length; counter++) {
        sum += arr[counter];
    }
    if (length != 3 || not_one_two_or_three(0) || not_one_two_or_three(1) || not_one_two_or_three(2))
        return sum;
    else return 24;
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This doesn't work for input 3, 2, 3. \$\endgroup\$ – cleblanc Oct 26 '17 at 12:55
  • \$\begingroup\$ 148 bytes \$\endgroup\$ – ceilingcat Nov 2 '19 at 9:00
0
\$\begingroup\$

Jq 1.5, 35 bytes

if[1,2,3]==sort then 24else add end

Assumes input is an array e.g. [2,1,3]

Try it online!

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

Ruby, 28 bytes

->s{s&[*1..3]==s&&24||s.sum}

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

Haskell, 47 bytes

import Data.List
f l=sum$l++[18|sort l==[1..3]]

Try it online!

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

Add++, 39 bytes

D,f,?!,db*@b+6B]B=VaE#3RBcB=B]b*G*18*As

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ OP says negative numbers don't have to be supported, implying that non-negative numbers do... however on this answer 1 + 2 + 3 + 0 = 24 \$\endgroup\$ – FlipTack Oct 30 '17 at 19:52
  • \$\begingroup\$ Given 3, 2, 1, this outputs 6. \$\endgroup\$ – FlipTack Oct 30 '17 at 20:26
0
\$\begingroup\$

Japt -x, 14 11 bytes

n e3õ)?24:U

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice technique, but I think this would fail on inputs [1,23] and [123]. \$\endgroup\$ – ETHproductions Oct 24 '17 at 20:02
  • \$\begingroup\$ @ETHproductions Thanks. I wish there were a better way to compare arrays. \$\endgroup\$ – Oliver Oct 24 '17 at 20:59
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.