26
\$\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]
asked Aug 28, 2017 at 11:59
\$\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\$ Commented 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\$ Commented Aug 28, 2017 at 18:06

54 Answers 54

1
2
1
\$\begingroup\$

Japt, 13 bytes

íV,È\Y Ä *XwY

Try it online! with the -Q flag to format the output array.

answered Aug 28, 2017 at 16:00
\$\endgroup\$
1
  • \$\begingroup\$ Nicely done. I made 2 attempts at this earlier with both coming out at 17 bytes. I'd forgotten í could take a function as a second argument. \$\endgroup\$ Commented Aug 28, 2017 at 16:39
1
\$\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+=
answered Aug 28, 2017 at 22:26
\$\endgroup\$
1
\$\begingroup\$

Rust, (削除) 107 (削除ここまで) 97 bytes

|a:V,b:V|a.iter().zip(b).map(|(&x,y)|if x==y{x+y}else{x.max(y)}).collect::<V>();
type V=Vec<f32>;

Try it online!

Saved 8 bytes thanks to @mgc

answered Aug 28, 2017 at 20:16
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I guess you can save 8 bytes by using type inference on the collected Vec and by using the max method of f32s: |a:Vec<f32>,b:Vec<f32>|a.iter().zip(b).map(|(&x,y)|if x==y{x+y}else{x.max(y)}).collect::<Vec<_>>(); \$\endgroup\$ Commented Aug 28, 2017 at 22:26
  • 1
    \$\begingroup\$ @mgc Thanks! Type inference was a good idea, but in this case type alias is even shorter. \$\endgroup\$ Commented Aug 29, 2017 at 4:41
1
\$\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.
answered Aug 30, 2017 at 7:15
\$\endgroup\$
1
\$\begingroup\$

Swift 4, 41 bytes

{zip(0,ドル1ドル).map{0ドル==1ドル ?2*0ドル:max(0,ドル1ドル)}}

Test cases:

let f: ([Float], [Float]) -> [Float]
 = {zip(0,ドル1ドル).map{0ドル==1ドル ?2*0ドル:max(0,ドル1ドル)}}
let testcases: [(inputA: [Float], inputB: [Float], expected: [Float])] = [
 (
 inputA: [],
 inputB: [],
 expected: []
 ),
 (
 inputA: [1, 2, 3],
 inputB: [1, 3, 2],
 expected: [2, 3, 3]
 ),
 (
 inputA: [1, 3, 3.2, 2.3],
 inputB: [3, 1, 3.2, 2.6],
 expected: [3, 3, 6.4, 2.6]
 ),
 (
 inputA: [1,2,3,4,5,5,7,8,9,10],
 inputB: [10,9,8,7,6,5,4,3,2,1],
 expected: [10, 9, 8, 7, 6, 10, 7, 8, 9, 10]
 ),
 (
 inputA: [-3.2, -3.2, -2.4, 7, -10.1],
 inputB: [100, -3.2, 2.4, -7, -10.1],
 expected: [100, -6.4, 2.4, 7, -20.2]
 ),
]
for (caseNumber, testcase) in testcases.enumerated() {
 let actual = f(testcase.inputA, testcase.inputB)
 assert(actual == testcase.expected,
 "Testcase #\(caseNumber) \((testcase.inputA, testcase.inputB)) failed. Got \(actual), but expected \(testcase.expected)!")
 print("Testcase #\(caseNumber) passed!")
}
answered Sep 21, 2017 at 5:57
\$\endgroup\$
1
\$\begingroup\$

Stax, 8 bytes

Ç√'81⁄2B=☺

Run and debug it

answered Feb 4, 2021 at 7:40
\$\endgroup\$
1
\$\begingroup\$

Desmos, 26 bytes


f(a,b)=\{a=b:2a,a>b:a,b\}

Leading newline necessary.

Try it on Desmos!

answered Jul 25, 2022 at 10:16
\$\endgroup\$
2
  • \$\begingroup\$ In your TIO link, you might want to give an example giving it lists instead of numbers, like so: desmos.com/calculator/iil8pysjzu \$\endgroup\$ Commented Jul 25, 2022 at 16:50
  • \$\begingroup\$ @Steffan L1 and L2 are lists, they’re just defined through the table’s first two columns, and then the third column shows f(L1, L2) which is the result of passing the function the two lists. Desmos isn’t evaluating it row by row as it may initially seem \$\endgroup\$ Commented Jul 27, 2022 at 7:21
1
\$\begingroup\$

Python, (削除) 57 (削除ここまで) 56 bytes

lambda a,b:[x==y and x+y or max(x,y)for x,y in zip(a,b)]

Generators, (削除) 57 (削除ここまで) 56 bytes

lambda a,b:(x==y and x+y or max(x,y)for x,y in zip(a,b))

-1 each by Steffan

answered Jul 25, 2022 at 21:52
\$\endgroup\$
2
  • \$\begingroup\$ -1 byte using and and or: Try it online! \$\endgroup\$ Commented Jul 25, 2022 at 21:57
  • \$\begingroup\$ Thanks a lot! I used it on my lists and generators solutions. \$\endgroup\$ Commented Jul 25, 2022 at 23:17
1
\$\begingroup\$

BQN, (削除) 6 (削除ここまで) 5 bytes

-1 byte by porting emanresu A's Vyxal answer

×ばつ1+=

Anonymous tacit function that takes equal-length lists as left and right arguments. Try it at BQN online!

Explanation

×ばつ1+=
 = Compare the two lists itemwise (1 for equal, 0 for not equal)
 1+ Add 1 to each (2 for equal, 1 for not equal)
⌈ Get the itemwise maximums of the two lists
 ×ばつ Multiply (itemwise)

Original solution

The spec can be implemented very literally in 6 bytes:

=◶⌈‿+ ̈
 ̈ Apply this function to corresponding pairs of elements from the arguments:
= Test if the elements are equal (1 if so, 0 if not)
 ◶ Use that result to pick a function from this list and apply it:
 ⌈ If 0 (not equal), then max
 ‿+ If 1 (equal), then add
answered Jul 26, 2022 at 3:45
\$\endgroup\$
2
  • \$\begingroup\$ Turns out I unintentionally ported the Jelly/APL answers lol. \$\endgroup\$ Commented Jul 26, 2022 at 4:30
  • \$\begingroup\$ Ha--and the APL answer is actually identical to this one! \$\endgroup\$ Commented Jul 26, 2022 at 5:00
1
\$\begingroup\$

Factor, 36 bytes

[ [ 2dup = [ + ] [ max ] if ] 2map ]

Try it online!

answered Dec 28, 2022 at 22:07
\$\endgroup\$
1
\$\begingroup\$

POSIX Shell + Utilities, 40 bytes

paste $@|awk '0ドル=1ドル<2ドル?2ドル:1ドル>2ドル?1ドル:2*1ドル'

"Arrays" defined as "text files per POSIX".
Obviously buggy insofar as [0], [0] -> []. Doesn't come up in the validator, so. Append the 2-byte "" sequence to also handle that edge case.

Transcript of test cases:

$ cat cg; echo; wc -c cg
paste $@|awk '0ドル=1ドル<2ドル?2ドル:1ドル>2ドル?1ドル:2*1ドル'
40 cg
$ ./cg /dev/null /dev/null
$ ./cg <(printf '%s\n' 1 2 3) <(printf '%s\n' 1 3 2)
2
3
3
$ ./cg <(printf '%s\n' 1 3 3.2 2.3) <(printf '%s\n' 3 1 3.2 2.6)
3
3
6.4
2.6
$ ./cg <(printf '%s\n' 1 2 3 4 5 5 7 8 9 10) <(printf '%s\n' 10 9 8 7 6 5 4 3 2 1)
10
9
8
7
6
10
7
8
9
10
$ ./cg <(printf '%s\n' -3.2 -3.2 -2.4 7 -10.1) <(printf '%s\n' 100 -3.2 2.4 -7 -10.1)
100
-6.4
2.4
7
-20.2
answered Dec 29, 2022 at 22:56
\$\endgroup\$
0
\$\begingroup\$

CJam, 16 bytes

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

Try it online!

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

answered Aug 28, 2017 at 14:47
\$\endgroup\$
0
\$\begingroup\$

Clojure, 40 bytes

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

Try it online!

answered Aug 28, 2017 at 17:42
\$\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.

answered Aug 28, 2017 at 18:19
\$\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\$
2
  • \$\begingroup\$ You should be able to remove most of those spaces. \$\endgroup\$ Commented Aug 28, 2017 at 20:42
  • \$\begingroup\$ I got a 41 byte swift answer, check it out: codegolf.stackexchange.com/a/143367/11490 \$\endgroup\$ Commented Sep 21, 2017 at 5:58
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.
answered Aug 28, 2017 at 21:50
\$\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.

answered Aug 28, 2017 at 22:37
\$\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

Adalynn
6,2121 gold badge18 silver badges36 bronze badges
answered Aug 30, 2017 at 1:32
\$\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!

answered Sep 21, 2017 at 6:25
\$\endgroup\$
2
  • \$\begingroup\$ Is the space in .[1] then not omittable? \$\endgroup\$ Commented Sep 21, 2017 at 6:27
  • \$\begingroup\$ 44 bytes: transpose|map(if.0==.1then add else max end). \$\endgroup\$ Commented Sep 21, 2017 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!

answered Oct 27, 2017 at 23:11
\$\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)

answered Mar 20, 2018 at 20:52
\$\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!

answered Oct 21, 2017 at 16:34
\$\endgroup\$
0
\$\begingroup\$

APL(Dyalog Unicode), (削除) (削除ここまで)15 bytes SBCS

(×ばつ1+⊣=⊢)/ ̈, ̈

Try it on APLgolf!

Can be golfed as:

((×ばつ1+=)/ ̈, ̈)×ばつ1+=

So it ends up being the same as the other APL answer...

answered Feb 4, 2021 at 13:25
\$\endgroup\$
0
\$\begingroup\$

J-uby, 31 bytes

Accidental port of ymbirtt's Ruby answer.

:zip|:*&:[]%[-[+:+,:max],+:<=>]

Attempt This Online!

answered May 8 at 15:18
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.