2
\$\begingroup\$

Note: This challenge has been changed to eliminate the strict whitespace manipulation requirements that made this challenge low-quality. Some rules regarding whitespace have been changed.


I'm rather good at mental math, particularly adding positive integers in my head. Here is my method for adding the numbers together (n is a positive integer):

  • Arrange the numbers in a horizontal row
  • Reorder the numbers so the 2n-1th number in the sequence is the nth smallest number in the sequence and the 2nth number in the sequence is the nth largest number in the sequence.
  • Add the 2n-1th number to the 2nth number. Print the results of these additions on the next line.
  • Repeat the previous step until a final result is obtained.

Your challenge is to write a program or function that prints each of the above steps until the sum of all the inputted numbers is reached. The final result must be separated from the adding steps by an additional blank newline. You may assume that all numbers inputted are unique so as to avoid confusion with the second step (and that the input is not empty). Numbers can be inputted in any format. Numbers in rows should be separated from each other by one space.

Test cases

For each of these, the input and output start a newline after their labels.

Case 1

Input: 
314 495 767 214 141

Output: 
141 767 214 495 314   //Reordering the input
908 709 314           //Adding
1617 314

1931                  //Final result

Case 2

Input: 
884

Output:
884   //Reordering the input

884   //Final result

Note that there is no adding step here because the reordering of the input produces the final result.

Case 3

Input: 
5033 7825 2167 6374 5050 5389 3915 7231 3025 1954 7249 5143 7567 9857 462 3504 6165 6681 6361 6367

Output:
462 9857 1954 7825 2167 7567 3025 7249 3504 7231 3915 6681 5033 6374 5050 6367 5143 6361 5389 6165
10319 9779 9734 10274 10735 10596 11407 11417 11504 11554
20098 20008 21331 22824 23058
40106 44155 23058
84261 23058

107769

This is code golf. Standard rules apply. Answer with the fewest bytes wins.

\$\endgroup\$
  • 6
    \$\begingroup\$ Hopefully your code will get the final result for the final test case faster than I did mentally. Though given that this is the Code Golf website, I'm not so sure :P \$\endgroup\$ – Arcturus Feb 25 '16 at 0:38
  • 2
    \$\begingroup\$ You expect us to write fast code???? Have you not been a member for 6 months? :P \$\endgroup\$ – Rɪᴋᴇʀ Feb 25 '16 at 1:10
  • \$\begingroup\$ That comment will probably be deleted in a mo, the mods said comments like that can be flagged as not constructive. Try chat instead. \$\endgroup\$ – Rɪᴋᴇʀ Feb 25 '16 at 2:51
  • 1
    \$\begingroup\$ Does the output after the initial row need to have the same spacing as shown, or is two spaces between numbers fine for every row? \$\endgroup\$ – Hotkeys Feb 25 '16 at 7:37
  • 3
    \$\begingroup\$ Well, it’s not very fair towards the Ruby answer I’d already spent a fair amount of time golfing. You should consider using the sandbox in the future. :( \$\endgroup\$ – Lynn Feb 28 '16 at 16:28
4
\$\begingroup\$

Ruby, 148 144 142 bytes

f=->a{a.sort!
(a.size/2).times{a[~-$.+=2,0]=a.pop}
a*="  "
puts$`+$&+$'while a.gsub!(/\d+\s+\d+/){"#{eval$&.split*?+}".ljust$&.size}
puts'',a}

So disappointingly long... To run:

f[ [314,495,767,214,141] ]

Prints:

141  767  214  495  314
908       709       314
1617                314

1931
\$\endgroup\$

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.