19
\$\begingroup\$

Given a list of 2 or more strictly positive integers, sum the first and last half of the list, with the middle element being counted only in the right half if the list has an odd number of elements

Some examples of the middle element being counted in the right half:

[1, 2, 3, 4]       -> Left: [1, 2];    Right: [3, 4]
[1, 2, 3]          -> Left: [1];       Right: [2, 3]
[1, 2, 3, 4, 5]    -> Left: [1, 2];    Right: [3, 4, 5]
[1, 2, 3, 4, 5, 6] -> Left: [1, 2, 3]; Right: [4, 5, 6]

Test Cases

Input

[1, 9]
[9, 5, 5, 7]
[6, 7, 5, 1, 3, 9, 7, 1, 6]
[2, 8, 10, 9, 9, 3, 7, 8, 9, 8, 6, 1, 2, 9, 8, 3, 8, 9, 5]
[2, 3, 1, 8, 6, 2, 10, 6, 7, 6]

Output

[1, 9]
[14, 12]
[19, 26]
[65, 59]
[20, 31]

Reference Program With Two Output Methods

Rules

  • Input/Output can be Taken/Given in any convenient and reasonable format.
  • Functions and full programs are both acceptable.
\$\endgroup\$
4
  • \$\begingroup\$ So just to confirm, we're permitted to return the two sums in reverse order (right sum then left sum)? \$\endgroup\$ May 2 '20 at 18:17
  • \$\begingroup\$ Yes you are allowed to. \$\endgroup\$
    – lyxal
    May 2 '20 at 18:44
  • \$\begingroup\$ Can the output be in reverse order? \$\endgroup\$
    – Shaggy
    May 2 '20 at 19:28
  • 2
    \$\begingroup\$ @Shaggy The question is asked and answered above \$\endgroup\$ May 2 '20 at 19:59

37 Answers 37

1
2
1
\$\begingroup\$

PHP, 60 bytes

for(;$a=$argv[++$i];)$r[$i<count($argv)/2]+=$a;var_dump($r);

Try it online!

Like often, much shorter with a straight loop.. PHP and arrays, sigh

PHP, 79 bytes

fn($l,$g=array_sum,$h=array_slice)=>[$g($h($l,0,$n=count($l)/2)),$g($h($l,$n))]

Try it online!

PHP takes care itself of the right bias.. it's long mostly because array functions have lengthy names..

\$\endgroup\$
1
\$\begingroup\$

MathGolf, 8 bytes

h½‼<≥αmΣ

Try it online.

Explanation:

h        # Get the length of the (implicit) input-list (without popping the list itself)
 ½       # Halve it (integer-divided by 2)
  ‼      # Apply the following two commands separately:
   <     #  Take all elements of the list smaller than the length//2
   ≥     #  Take alle elements of the list larger than or equal to the length//2
    α    # Wrap both lists into a list
     m   # Map over this list of lists:
      Σ  #  And sum each inner list
         # (after which the entire stack joined together is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 68 bytes

(l,h=k=>k.reduce((a,e)=>a+e,0))=>([h(l.splice(0,l.length>>1)),h(l)])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Factor, 55 54 bytes

: f ( s -- s s ) dup length 2/ floor cut [ sum ] bi@ ;

Try it online!

\$\endgroup\$
1
\$\begingroup\$

MAWP, 17 bytes

%@[_2A{%M~}]%:1;:

Try it! (Only first line is taken as input)

Outputs sums in reverse.

\$\endgroup\$
1
\$\begingroup\$

Factor + math.unicode, 21 bytes

[ halves [ Σ ] bi@ ]

Try it online!

Explanation:

It's a quotation (anonymous function) that takes a sequence from the data stack as input and leaves two integers on the data stack as output.

  • halves Split a sequence in half. Luckily for us, it keeps the middle element with the latter sequence.
  • [ ... ] bi@ Apply a quotation to two objects.
  • Σ Sum a sequence.
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 7 bytes

Ṙ₅½⌈ẇv∑

Try it Online!

-1 thanks to lyxal

\$\endgroup\$
1
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.