25
\$\begingroup\$

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]
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You say that the numbers will always fit "within your language's" capabilities". As long as you do not "abuse" that. Would only supporting integers in a language that does not have floats be considered an abuse? The question does say floating point but I don't really see a reason why it has to be floats. The same process can be done on integers. I would like to solve this in Brain-Flak but Brain-flak only supports ints. \$\endgroup\$
    – Wheat Wizard
    Aug 28, 2017 at 17:03
  • \$\begingroup\$ @WheatWizard I can make an exception for that. Go ahead and post your answer and mention I allowed it to avoid confusion. \$\endgroup\$
    – user70974
    Aug 28, 2017 at 18:06

53 Answers 53

13
\$\begingroup\$

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

Python 2, 46 bytes

@ovs suggested this method to save 3 bytes.

lambda*x:[max(a,b)*-~(a==b)for a,b in zip(*x)]

Try it online!


How?

First off, we pair the corresponding elements, by using either * or zip(). That allows us to do our further golfing by working either with a map or a list comprehension.

The cool trick in this answer is this part: max(x,y)*-~(x==y). How does that work? - Well, as most of you already know, Python auto-converts bool values to integers when they are used in arithmetic operations. Hence, (x==y) gets evaluated as 1, if the condition is met. However, if the two values are not equal, it returns 0 instead. Then, the bitwise operation -~ increments the value returned from the bool by 1, giving us either 2 or 1. max(a,b) gives the maximum value of the pair and * multiplies it by the value returned above (so it gets multiplied by 2 only if they are equal, in which case max() returns the value of both).

This is based on the fact that the sum of two equal numbers is in fact either of them doubled, and kind of "abuses" Python's bool class being a subclass of int.

\$\endgroup\$
6
  • \$\begingroup\$ Wow, that was really fast! \$\endgroup\$
    – user70974
    Aug 28, 2017 at 12:05
  • \$\begingroup\$ more straightforward, same number of bytes: lambda*a:map(lambda x,y:(x<=y)*y+(x>=y)*x,*a) \$\endgroup\$
    – jferard
    Aug 28, 2017 at 20:09
  • \$\begingroup\$ @jferard I fact, that's already Luis' solution. \$\endgroup\$
    – Mr. Xcoder
    Aug 28, 2017 at 20:12
  • \$\begingroup\$ @Mr.Xcoder Oops! I didn't read the whole page... \$\endgroup\$
    – jferard
    Aug 28, 2017 at 20:18
  • \$\begingroup\$ Never say "above," as the ordering can change (I don't see your solution above) \$\endgroup\$
    – Adalynn
    Aug 28, 2017 at 21:53
11
\$\begingroup\$

Kotlin, 78 75 71 66 65 59 bytes

It's my first attempt, be cool :D

a.zip(b).map{(a,b)->when{b>a->b;a>b->a;else->a*2}}.toList()

TIO doesn't work with this solution (and i don't know why), source code for testing below

fun main(args: Array<String>) {
    bestOfTwo(floatArrayOf(), floatArrayOf()).print()
    bestOfTwo(floatArrayOf(0F), floatArrayOf(0F)).print()
    bestOfTwo(floatArrayOf(1F,2F,3F), floatArrayOf(1F,3F,2F)).print()
    bestOfTwo(floatArrayOf(1F,3F,3.2F,2.3F), floatArrayOf(3F,1F,3.2F,2.6F)).print()
    bestOfTwo(floatArrayOf(1F,2F,3F,4F,5F,5F,7F,8F,9F,10F), floatArrayOf(10F,9F,8F,7F,6F,5F,4F,3F,2F,1F)).print()
    bestOfTwo(floatArrayOf(-3.2F,-3.2F,-2.4F,7F,-10.1F), floatArrayOf(100F,-3.2F,2.4F,-7F,-10.1F)).print()
}


fun bestOfTwo(a :FloatArray, b :FloatArray): List<Float> =
    a.zip(b).map{(a,b)->when{b>a->b;a>b->a;else->a*2}}.toList()


fun List<Float>.print() {
    this.forEach { print("$it, ") }; println("")
}

EDIT:

-3 by replace "a+b[i]" by "a*2"

-4 by replace "mapIndexed" method by "zip" (Thanks to @AnonymousReality Swift solution)

-5 by replace "Math.max" method by when condition

-1 by change when condition order

-6 by change toFloatArray() by toList()

\$\endgroup\$
3
  • 10
    \$\begingroup\$ Welcome to PPCG! Please don't be discouraged by the downvote (it's the result of a slight quirk of the system that happens when a new user's first post is auto-flagged for quality and then they improve said post!!) \$\endgroup\$ Aug 28, 2017 at 13:23
  • 2
    \$\begingroup\$ The worst "feature" ever...btw don't feel bad about it. \$\endgroup\$ Aug 28, 2017 at 13:26
  • \$\begingroup\$ Link to Kotlin Playground: pl.kotl.in/Qne3U_j4l \$\endgroup\$
    – Eric Xue
    Jul 25, 2022 at 21:49
11
\$\begingroup\$

Python 3, 48 46 44 bytes

-2 bytes thanks to @nwellnhof

lambda*a:map(lambda*x:max(x)*2/len({*x}),*a)

Try it online!

\$\endgroup\$
0
9
\$\begingroup\$

Jelly, 4 bytes

=‘×»

Try it online!

This uses the exact same approach as my APL answer, except Jelly has a builtin for adding one to a number!

\$\endgroup\$
4
  • \$\begingroup\$ Hate to be a spoilsport, but aren't some of those characters more than one byte each in any sensible encoding? \$\endgroup\$ Aug 30, 2017 at 1:26
  • \$\begingroup\$ This uses the jelly codepage. \$\endgroup\$
    – Adalynn
    Aug 30, 2017 at 1:37
  • \$\begingroup\$ I finally won against competition! \$\endgroup\$
    – Adalynn
    Sep 7, 2017 at 0:39
  • 2
    \$\begingroup\$ @Zacharý ONE MAN, 4 btytes... THIS SUMMER... You... WILL... BE... JELLY OF HIM... rated J for Jelly. \$\endgroup\$ Oct 27, 2017 at 18:58
9
\$\begingroup\$

JavaScript (ES6), 53 49 45 43 bytes

a=>b=>a.map((x,y)=>(y=b[y])>x?y:y<x?x:x+y)
  • 4 bytes saved by borrowing a trick from Mr. Xcoder.
  • 2 bytes saved thanks to Arnauld.

Try it

o.innerText=(f=

a=>b=>a.map((x,y)=>(y=b[y])>x?y:y<x?x:x+y)

)(i.value=[1,3,3.2,2.3])(j.value=[3,1,3.2,2.6]);oninput=_=>o.innerText=f(i.value.split`,`.map(eval))(j.value.split`,`.map(eval))
<input id=i><input id=j><pre id=o>


Explanation

a=>b=>

Anonymous function taking the 2 arrays as arguments via parameters a and b, in currying syntax (i.e., call with f(a)(b)

a.map((x,y)=>                      )

Map over the first array, passing each element through a function where x is the current element and y is the current index.

(y=b[y])

Get the element at index y in the second array and assign that as the new value of y.

>x?y

Check if y is greater than x and, if so, return y.

:y<x?x

Else, check if y is less than x and, if so, return x

:x+y

Else, return the sum of x and y. (Multiplying x or y by 2 would also work here, for the same number of bytes.)

\$\endgroup\$
2
  • \$\begingroup\$ j.value.split`,`.map(eval) or eval('['+j.value+']')? Also x+y would look neater IMHO. \$\endgroup\$
    – Neil
    Aug 28, 2017 at 14:56
  • \$\begingroup\$ @Neil: 1) I find the former easier to type. Also, I have a couple of Snippet templates on one of my machines; it's easier just to tack .map(eval) onto them. 2) Agreed, will edit in momentarily. \$\endgroup\$
    – Shaggy
    Aug 28, 2017 at 16:13
8
\$\begingroup\$

Haskell, 34 bytes

x!y|x>y=x|x<y=y|1<2=x+y
zipWith(!)

Try it online.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Same byte count: x!y=max x y+sum[x|x==y]. \$\endgroup\$
    – nimi
    Aug 28, 2017 at 19:46
7
\$\begingroup\$

Dyalog APL, 5 bytes

⌈×1+=

Try it online!

How?

  • , element-wise maximum of the arguments
  • ×, element-wise multiply
  • 1+=, 1 added to the element-wise equality of the arguments

This works because if the numbers are unequal, 1+= will be 1, which when multiplied by the maximum, is the maximum. When the numbers are equal, 1+= will return 2, when that is multiplied by the maximum, we get twice the maximum, or the maximum added to itself.

\$\endgroup\$
7
\$\begingroup\$

R, 31 29 bytes

function(a,b)pmax(a,b)+a*!a-b

pmax takes the parallel maximum of the two (or more) arrays (recycling the shorter as needed).

I was looking at Luis Mendo's comment and obviously I realized the approach could work for R as well. That got me to 30 bytes, but then I started playing around with different ways of getting indices instead to improve my original answer, and stumbled upon !a-b as TRUE where a==b and FALSE otherwise, equivalent to a==b. However, for whatever reason, R doesn't require parentheses around !a-b as it does for a==b, which saved me two bytes.

As mentioned by JDL in the comments, this works because ! (negation) has lower precedence than the binary operator - in R, which is strange.

Try it online! (new version)

Try it online! (original)

\$\endgroup\$
2
  • \$\begingroup\$ It turns out that unary "!" has lower precedence in R than binary "-", which I think is quite unusual (and I hadn't realised until reading this answer!) \$\endgroup\$
    – JDL
    Aug 29, 2017 at 15:29
  • \$\begingroup\$ @JDL yeah I almost always have to open up the R Syntax page while golfing in case of weird quirks like this...and also because I can never remember the precedence of : when interacting with arithmetic. \$\endgroup\$
    – Giuseppe
    Aug 29, 2017 at 15:55
5
\$\begingroup\$

Jelly, 6 bytes

żSṀE?€

A dyadic link taking a list of numbers on each side and returning the resulting list.

Try it online! or see a test-suite*.

How?

żSṀE?€ - Link: list of numbers L, list of numbers R   e.g. [1,3,3.2,2.3], [3,1,3.2,2.6]
ż      - zip - interleave L & R                       [[1,3],[3,1],[3.2,3.2],[2.3,2.6]]
     € - for each pair:
    ?  - { if:
   E   -   ...condition: equal                          0      0       1         0
 S     -   ...then: sum                                               6.4
  Ṁ    -   ...else: maximum                             3      3                2.6
       - }                                    ... ->   [3     ,3     ,6.4      ,2.6]

An alternative is this monadic link taking a list of the two lists, also 6 bytes:

+»⁼?"/

* I don't think I've ever created a test-suite footer almost three times the byte count of the code before!

\$\endgroup\$
6
  • \$\begingroup\$ Outgolfed!. +1 for the practically verbatim interpretation of the question. \$\endgroup\$
    – Adalynn
    Aug 28, 2017 at 20:47
  • \$\begingroup\$ ...and I've been caught out be forgetting that » vectorises before! \$\endgroup\$ Aug 28, 2017 at 20:55
  • \$\begingroup\$ What else would it do, take the maximum array in some convoluted way? \$\endgroup\$
    – Adalynn
    Aug 28, 2017 at 21:13
  • \$\begingroup\$ No need for any convoluted definitions, Python manages - for example max([1,1,0],[1,0,3]) -> [1,1,0] (not [1,1,3]). \$\endgroup\$ Aug 28, 2017 at 21:20
  • \$\begingroup\$ So, basically infinite-base? \$\endgroup\$
    – Adalynn
    Aug 28, 2017 at 21:21
5
\$\begingroup\$

05AB1E, 5 bytes

øεMÃO

Try it online!

-1 thanks to Emigna.

\$\endgroup\$
4
  • \$\begingroup\$ Nice idea using γ! \$\endgroup\$
    – Emigna
    Aug 28, 2017 at 13:20
  • \$\begingroup\$ @Emigna I really wanted "maximal elements", and {γθ is probably the shortest I can get to for that. \$\endgroup\$ Aug 28, 2017 at 13:21
  • \$\begingroup\$ How about øεMÃO? \$\endgroup\$
    – Emigna
    Aug 28, 2017 at 13:23
  • 1
    \$\begingroup\$ @Emigna Cool, thanks! (duh why didn't I think of ) yay got the lead now :p btw øεZÃO would work too \$\endgroup\$ Aug 28, 2017 at 13:23
4
\$\begingroup\$

MATL, 7 bytes

X>tG=s*

Input is a two-row matrix, where each row is one of the arrays.

Try it online!

Explanation

X>   % Implicit input. Maximum of each column
t    % Duplicate
G    % Push input
=    % Is equal? Element-wise with broadcast. Gives a two-row matrix
s    % Sum of each column. Gives a row vector containing 1 and 2
*    % Multiply, element-wise. Implicit display
\$\endgroup\$
4
\$\begingroup\$

Java 8, 80 69 67 66 65 64 63 bytes

(a,b,l)->{for(;l-->0;)if(a[l]>=b[l])b[l]=a[l]*(a[l]>b[l]?1:2);}

Modifies the second input-array instead or returning a new float-array to save bytes.

-11 bytes by taking the length as additional integer-input, which is allowed according to the challenge rules.
-5 bytes thanks to @OliverGrégoire (one byte at a time.. xD)
-1 byte indirectly thanks to @Shaggy's JS answer, by using a[l]*2 instead of a[l]+b[l].

Explanation:

Try it here.

(a,b,l)->{          // Method with 2 float-array and integer parameters and no return-type
  for(;l-->0;)      //  Loop over the array
    if(a[l]>=b[l])  //   If the current value in `a` is larger or equal to `b`:
      b[l]=         //   Modify the second input-array:
       a[l]*        //    Use `a` multiplied by:
        (a[l]>b[l]? //     If the current value in `a` is larger than `b`:
          1         //      Multiply by 1
         :          //     Else (`a` is smaller of equal to `b`):
          2)        //      Multiply by 2
                    //  End of loop (implicit / single-line body)
}                   // End of method
\$\endgroup\$
9
  • 2
    \$\begingroup\$ "If it helps you reduce your byte count, you may also take the length of the lists as input." It will definitely reduce your byte-count ;) \$\endgroup\$ Aug 28, 2017 at 12:18
  • 1
    \$\begingroup\$ Also, 2 bytes shorter: a->b->l->{float A,B;for(;l-->0;b[l]=(A=a[l])<B?B:A>B?A:A+B)B=b[l];} \$\endgroup\$ Aug 28, 2017 at 12:25
  • \$\begingroup\$ And you can save one more byte by putting float A, B in the for initialization. \$\endgroup\$ Aug 28, 2017 at 12:28
  • 1
    \$\begingroup\$ Or this: (a,b,l)->{for(;l-->0;)if(a[l]>=b[l])b[l]=a[l]*(a[l]>b[l]?1:2);} (63 bytes) \$\endgroup\$ Aug 28, 2017 at 12:44
  • 3
    \$\begingroup\$ @OlivierGrégoire Lol.. with golfing every byte helps, but that doesn't mean you need to golf it one byte at a time. ;p \$\endgroup\$ Aug 28, 2017 at 12:53
4
\$\begingroup\$

Ruby, 42 bytes

->a,b{a.zip(b).map{|x,y|[x+y,x,y][x<=>y]}}

Try it online!

The spaceship operator is great.

\$\endgroup\$
3
\$\begingroup\$

Pyth, 11 bytes

m*eSdhnd{dC

Try it here!

Pyth, 12 bytes

m*eSdhqhdedC

Try it here!

or

m*eSdh!tl{dC

Try it here!

\$\endgroup\$
2
  • \$\begingroup\$ @Jakube That's Erik's solution already, sadly. I wanted to use that too, but I can't now \$\endgroup\$
    – Mr. Xcoder
    Aug 29, 2017 at 13:56
  • \$\begingroup\$ Oh, didn't see that one. \$\endgroup\$
    – Jakube
    Aug 29, 2017 at 13:57
3
\$\begingroup\$

05AB1E, 9 8 7 bytes

Saved a byte as Erik the Outgolfer pointed out that a list of lists is valid input.

øεMsËi·

Try it online!

Explanation

ø          # zip the lists
 ε         # apply to each pair
  M        # get max
   s       # swap the top 2 elements on the stack
    Ëi     # if all elements are equal
      ·    # double the max
\$\endgroup\$
5
  • \$\begingroup\$ Wow, that was really fast! \$\endgroup\$
    – user70974
    Aug 28, 2017 at 12:05
  • \$\begingroup\$ You can save a byte by removing the and inputting as a pair of a list and a list. \$\endgroup\$ Aug 28, 2017 at 13:01
  • \$\begingroup\$ @EriktheOutgolfer: True. I assumed we weren't allowed to, but I see that the challenge does specify standard I/O rules. Thanks for notifying :) \$\endgroup\$
    – Emigna
    Aug 28, 2017 at 13:16
  • 1
    \$\begingroup\$ @Emigna Tip: don't make rules out of your mind ;) \$\endgroup\$ Aug 28, 2017 at 13:19
  • 1
    \$\begingroup\$ @EriktheOutgolfer: Yeah I really need to stop doing that. Especially rules which make my programs longer ;) \$\endgroup\$
    – Emigna
    Aug 28, 2017 at 13:19
3
\$\begingroup\$

Mathematica, 31 bytes

MapThread[If[#==#2,2#,Max@##]&]
\$\endgroup\$
3
\$\begingroup\$

Python 3, 49 46 45 bytes

3 bytes removed thanks to @Mr.Xcoder (splat instead of two arguments), and 1 byte thanks to @ovs (map instead of list comprehension)

lambda*x:map(lambda a,b:a*(a>=b)+b*(b>=a),*x)

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 46 bytes: lambda*c:[a*(a>=b)+b*(b>=a)for a,b in zip(*c)]. Turns out this is quite good too :) - Too bad there is no place for further golfing \$\endgroup\$
    – Mr. Xcoder
    Aug 28, 2017 at 12:43
  • \$\begingroup\$ @Mr.Xcoder Thanks! Good idea! \$\endgroup\$
    – Luis Mendo
    Aug 28, 2017 at 12:47
  • 1
    \$\begingroup\$ 45 bytes by using map instead of zip. \$\endgroup\$
    – ovs
    Aug 28, 2017 at 17:35
3
\$\begingroup\$

J, 7 bytes

>.`+@.=

Try it online!

Takes one list as the left argument and the other as the right.

Luckily, equality is a rank zero operation.

Explanation

>.`+@.=
      =  Compare equality pairwise
    @.   If equal
   +       Sum
>.       (Else) take greater value

@. isn't really an if statement, but in this case it functions as one (it indexes into the gerund >.`+ based on the result of its right argument and applies that to the input).

\$\endgroup\$
3
  • \$\begingroup\$ Nice job, I know I couldn't do this, even though you have beenoutgolfed by my translation of my APL. >_< \$\endgroup\$
    – Adalynn
    Aug 29, 2017 at 0:15
  • \$\begingroup\$ J really shines here \$\endgroup\$
    – Jonah
    Aug 29, 2017 at 8:50
  • \$\begingroup\$ @Zacharý rats, well-golfed nonetheless. \$\endgroup\$
    – cole
    Aug 29, 2017 at 13:49
3
\$\begingroup\$

TI-Basic, 23 21 bytes

Prompt A,B
(ʟA=ʟB)ʟA+max(ʟA,ʟB

Too bad lists take up two bytes each...

\$\endgroup\$
3
  • \$\begingroup\$ You can save two bytes by prompting for X and Y, then using ʟX and ʟY to access them, i.e. "Prompt X,Y:ʟX(ʟX=ʟY)+max(ʟ1,ʟ2". \$\endgroup\$ Aug 29, 2017 at 21:53
  • \$\begingroup\$ Also, this is currently invalid, since L1(L1=L2) attempts to get the element of L1 at a list, which throws an error. To fix that, swap the order, i.e. (L1=L2)L1. \$\endgroup\$ Aug 29, 2017 at 21:58
  • \$\begingroup\$ @ScottMilner Thanks for pointing both of those out. \$\endgroup\$
    – Timtech
    Aug 29, 2017 at 23:04
3
\$\begingroup\$

Uiua, 6 bytes

×+1⊃=↥

Try it!

×+1⊃=↥
   ⊃=↥  # elementwise max and equality mask
 +1     # increment
×       # multiply
\$\endgroup\$
2
\$\begingroup\$

Octave, 36 Byte

@(a,b)a.*(a>b)+b.*(b>a)+2.*a.*(a==b)
\$\endgroup\$
2
2
\$\begingroup\$

Pyth, 7 bytes

ms.MZdC

Try it here.

\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 60 59 bytes

(mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read))

Try it online!

-1 byte thanks to @Zacharý!

\$\endgroup\$
2
  • \$\begingroup\$ 59 bytes: (mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read)). \$\endgroup\$
    – Adalynn
    Aug 29, 2017 at 0:42
  • \$\begingroup\$ You're welcome, I don't know lisp that well, I just translated my other answers into Lisp which ended up saving a byte. \$\endgroup\$
    – Adalynn
    Aug 29, 2017 at 8:11
2
\$\begingroup\$

Python with numpy, 28 bytes

lambda a,b:a*(a>=b)+b*(b>=a)

Assumes input is given as two numpy arrays.

\$\endgroup\$
7
  • \$\begingroup\$ If we are using numpy then here is my worse solution: lambda a,b:n.fmax(a,b)*((a==b)+1) \$\endgroup\$
    – Erich
    Aug 29, 2017 at 18:09
  • \$\begingroup\$ @Erich I like the idea, but to do that I would need to import numpy as n. I get away without it here because it's implicit in the input. \$\endgroup\$
    – user48543
    Aug 29, 2017 at 18:22
  • \$\begingroup\$ I guess i'm a bit shaky on the explicit byte counting, often python answers are simply lambdas, when an actual implementation of an answer would require assigning it to something. for this reason I wonder if it is allowable to get away with an implicit import statement as well? \$\endgroup\$
    – Erich
    Aug 29, 2017 at 18:32
  • \$\begingroup\$ @Erich In general, you can only refer to a variable n if you've defined n in your code, so imports must be explicit. By default, we allow functions or full programs as answers, which includes anonymous functions. \$\endgroup\$
    – user48543
    Aug 29, 2017 at 18:39
  • 1
    \$\begingroup\$ Well, this only needs input as numpy arrays, rather than importing numpy. But does this even work without using return? \$\endgroup\$
    – Adalynn
    Aug 29, 2017 at 20:29
2
\$\begingroup\$

C# (.NET Core), using Linq 47+18=65 bytes

x=>y=>x.Zip(y,(a,b)=>a>b?a:b>a?b:b+a).ToArray()

Try it online!

C# (.NET Core), 82 bytes

x=>y=>l=>{for(int i=0;i<l;i++)x[i]=x[i]>y[i]?x[i]:y[i]>x[i]?y[i]:y[i]*2;return x;}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ You can drop the LINQ answer by a few bytes by changing namespace System.LINQ to using System.LINQ \$\endgroup\$
    – jkelm
    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\$ 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\$
    – dana
    Dec 17, 2018 at 4:22
2
\$\begingroup\$

Perl 6, 34 28 bytes

{map {.max*2/set $_},[Z] $_}

Try it online!

\$\endgroup\$
2
+100
\$\begingroup\$

Vyxal, 8 7 bytes

Zƛ≈[∑|G

-1 byte thanks to @emanresu A

Add the flag if you want to see the numbers in decimal format.

Try it Online!

Explanation:

Z        # Zip
 ƛ       # On each pair
   [     # If
  ≈      # All are equal
    ∑    # Sum
     |   # Otherwise
      G  # Get the greatest value
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 6 bytes

₌Þ∴=›*

Try it Online!

₌      # Do both of these...
 Þ∴    # Elementwise maximum
   =›  # Equality, incremented -> list of 1s where unequal, 2s where equal
     * # Multiply by that.
\$\endgroup\$
1
\$\begingroup\$

Swift 3, 81 79 Bytes

func n(a:[Double],b:[Double]){for(x,y)in zip(a,b){print((x==y) ?x+y:max(x,y))}}

Swift has an interesting property in that an Int isn't directly castable to a Double, so you have to specify any arrays as being arrays of Doubles before passing them to the function.

(e.g.) var k:[Double] = [1,2,3,4,5,5,7,8,9,10]

Edit: -2 bytes thanks to @EriktheOutgolfer

\$\endgroup\$
6
  • \$\begingroup\$ Do you need spaces around (x,y) and before ?? \$\endgroup\$ Aug 28, 2017 at 13:28
  • \$\begingroup\$ @EriktheOutgolfer The one before ? is needed because Swift would treat them as optional types instead of ternaries (which they aren't). The others aren't. Apart from that, this can be drastically golfed. \$\endgroup\$
    – user70974
    Aug 28, 2017 at 13:31
  • \$\begingroup\$ @EriktheOutgolfer - TheIOSCoder has already answered you partly, but you're right, you don't need the ones in the for loop, interesting! \$\endgroup\$ Aug 28, 2017 at 13:34
  • \$\begingroup\$ 73 bytes: func n(a:[Float],b:[Float]){print(zip(a,b).map{$0==$1 ?2*$0:max($0,$1)})} (the float inaccuracies need not to be handled by default) \$\endgroup\$
    – Mr. Xcoder
    Aug 28, 2017 at 13:38
  • \$\begingroup\$ Or 74 bytes: func n(a:[Float],b:[Float]){print(zip(a,b).map{($0==$1 ?2:1)*max($0,$1)})} \$\endgroup\$
    – Mr. Xcoder
    Aug 28, 2017 at 13:40
1
\$\begingroup\$

C, 76 75 bytes

Thanks to @Kevin Cruijssen for saving a byte!

f(a,b,n)float*a,*b;{for(;n--;++a,++b)printf("%f ",*a>*b?*a:*b>*a?*b:*a*2);}

Try it online!

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.