19
\$\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\$
  • \$\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 '17 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 '17 at 18:06

43 Answers 43

0
\$\begingroup\$

CJam, 16 bytes

{.{a+_:e>\:=)*}}

Try it online!

Oh hey look, my code is cute :=) but a big dude \:=)*

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

Clojure, 40 bytes

#(map(fn[x y]((if(= x y)+ max)x y))% %2)

Try it online!

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

PHP, 74 bytes

function($a,$b){foreach($a as$i=>$p)echo$p*($p>=$q=$b[$i])+$q*($q>=$p),_;}

Prints the results separated by an underscore. Try it online.

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

Swift, 80 bytes

var o = [Int]();for (c, d) in zip(a,b){o.append(c==d ? c*2 : max(c,d))};return o
  • Create the output array
  • zip together a & b
  • for each loop through with c & d
  • append result to output array
  • return output array
\$\endgroup\$
0
\$\begingroup\$

MY, 20 bytes

αωD6ǵ'69ǵ';ƒ⇹(αω=‘×↵

Try it online!

This uses the exact same approach as my APL and Jelly answers, except MY doesn't have some of the builtins:

  • D6ǵ'69ǵ';ƒ, this pushes <SPACE>⍐ in MY's codepage (where <SPACE> is a physical space) as a function, which turns two items into a list, then finds the maximum.
  • ⇹(, map over each argument, then apply the function.
\$\endgroup\$
0
\$\begingroup\$

K, 12 bytes

{x|y*(1+x=y)}

Exact same strategy as my other answers.

Try it online!

Any golfing tips are welcome, as I don't really know K.

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

J, 6 bytes

>.*1+=

A direct translation of my APL answer (and thus works the same as my MY and Jelly answers). Any tips are welcome, since I don't really know J.

Try it online!

Comparison with APL

>.*1+=
⌈ ×1+=
\$\endgroup\$
0
\$\begingroup\$

Erlang, 65 bytes

fun(A,B)->lists:zipwith(fun(C,C)->C+C;(D,E)->max(D,E)end,A,B)end

example:

1> (fun(A,B)->lists:zipwith(fun(C,C)->C+C;(D,E)->max(D,E)end,A,B)end)([1, 3, 3.2, 2.3], [3, 1, 3.2, 2.6] ).
[3,3,6.4,2.6]

just a straightforward anonymous fun

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

Scala, 61 Bytes

x=>y=>x zip y map(v=>if(v._1==v._2)v._1*2 else v._1 max v._2)

The above is a function literal in Scala. Here's an explanation.

x=>y=>         // Function literal taking 2 * Vector[Float], x and y.
x zip y        // Zip x and y into one list of pairs.
map(           // Replace every element in the list via a function.
v=>            // Function literal that takes a pair of floats.
if(v._1==v._2) // If the pair are equal.
v._1*2         // Set the element to the first member of the pair multiplied by 2.
else           // Otherwise.
v._1 max v._2) // Set it to their max.
\$\endgroup\$
0
\$\begingroup\$

Jq 1.5, 49 44 bytes

transpose|map(if.0==.1then add else max end)

Sample input

[[], []]
[[1, 2, 3], [1, 3, 2]]
[[1, 3, 3.2, 2.3], [3, 1, 3.2, 2.6]]
[[1,2,3,4,5,5,7,8,9,10], [10,9,8,7,6,5,4,3,2,1]]
[[-3.2, -3.2, -2.4, 7, -10.1], [100, -3.2, 2.4, -7, -10.1]]

Sample run

$ jq -Mc 'transpose|map(if.0==.1then add else max end)' input
[]
[2,3,3]
[3,3,6.4,2.6]
[10,9,8,7,6,10,7,8,9,10]
[100,-6.4,2.4,7,-20.2]

Character count

$ echo -n 'transpose|map(if.0==.1then add else max end)' | wc -c
      44

Thanks again to Jonathan Frech for helping eliminate 5 bytes!

\$\endgroup\$
  • \$\begingroup\$ Is the space in .[1] then not omittable? \$\endgroup\$ – Jonathan Frech Sep 21 '17 at 6:27
  • \$\begingroup\$ 44 bytes: transpose|map(if.0==.1then add else max end). \$\endgroup\$ – Jonathan Frech Sep 21 '17 at 6:28
0
\$\begingroup\$

Perl 5, 59 + 1 (-p) = 60 bytes

say join$",map$_>($t=$F[$i++])?$_:$_<$t?$t:$_*2,split/ /,<>

Try it online!

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

SmileBASIC, 120 bytes

An answer using ONLY the ARYOP function.

DEF B A,B,L
DIM C[L],D%[L]ARYOP 6,C,A,A,B
ARYOP.,A,A,B
ARYOP 1,B,A,C
ARYOP 1,B,C,B
ARYOP 6,D%,B,-1,0ARYOP 4,A,C,D%,A
END

Function is called as B array1 , array2 , length. Output is stored in array1.

Explanation:

'get minimum, sum, and maximum of B and A
ARYOP #AOPCLP,C,A,A,B 'c=clamp(a,a,b) (gets the minimum of a and b)
ARYOP #AOPADD,A,A,B 'a=a+b (get the sum)
ARYOP #AOPSUB,B,A,C 'b=a-c (get the maximum)
'check if min==max
ARYOP #AOPSUB,B,C,B 'b=c-c (min-max, will be negative or 0)
ARYOP #AOPCLP,D%,B,-1,0 'd%=clamp(b,-1,0) (d% is an integer array. 0=equal, -1=not equal)
'add subtract min from sum if elements were not equal
ARYOP #AOPMAD,A,C,D%,A 'a=c*d%+a

This challenge seemed boring at first, but there are actually a lot of different ways to do it. For example:

-calculate the maximum, add the minimum if B==A

-calculate the sum, subtract A if A<B, subtract B if B<A

-b+max(a-b,0)+a*(a==b)

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

Tcl, 57 bytes

proc M A\ B {lmap a $A b $B {expr $a-$b?max($a,$b):2*$a}}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy