Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Get the best of two Arrays

You will be given two arrays of floating-point numbers. Your task is to pair the corresponding elements of the two arrays, and get the maximum of each pair. However, if the two corresponding elements are equal, you must take their sum instead.

For example, given the lists [1, 3, 3.2, 2.3] and [3, 1, 3.2, 2.6], you must do the following:

  • Pair the elements (or zip): [[1, 3], [3, 1], [3.2, 3.2], [2.3, 2.6]].

  • Go through each pair and apply the process above: [3, 3, 6.4, 2.6].


Specs

  • The arrays / lists will always have equal length. They may however be empty.

  • The numbers they contain will always fit your language's capabilities, as long as you do not abuse that. They may be positive, zero or negative, you must handle all types.

  • If it helps you reduce your byte count, you may also take the length of the lists as input.

Rules


Test Cases

Array_1, Array_2 -> Output
[], [] -> []
[1, 2, 3], [1, 3, 2] -> [2, 3, 3]
[1, 3, 3.2, 2.3], [3, 1, 3.2, 2.6] -> [3, 3, 6.4, 2.6]
[1,2,3,4,5,5,7,8,9,10], [10,9,8,7,6,5,4,3,2,1] -> [10, 9, 8, 7, 6, 10, 7, 8, 9, 10]
[-3.2, -3.2, -2.4, 7, -10.1], [100, -3.2, 2.4, -7, -10.1] -> [100, -6.4, 2.4, 7, -20.2]

Answer*

Draft saved
Draft discarded
Cancel
3
  • \$\begingroup\$ You can drop the LINQ answer by a few bytes by changing namespace System.LINQ to using System.LINQ \$\endgroup\$ Commented Aug 28, 2017 at 18:27
  • \$\begingroup\$ @jkelm yeah, I've been wondering if the 'using System;` is to be included or not like that, I guess not. I'll clean it up \$\endgroup\$ Commented Aug 28, 2017 at 19:23
  • \$\begingroup\$ System.Linq is included in the "Visual C# Interactive Compiler". I am not totally sure about returning Array vs IList vs IEnumerable, but if all are eligible then you can get the byte count to 37 - tio.run/##Sy7WTS7O/… \$\endgroup\$ Commented Dec 17, 2018 at 4:22

AltStyle によって変換されたページ (->オリジナル) /