| Bytes | Lang | Time | Link |
|---|---|---|---|
| 031 | Juby | 250508T151858Z | Jordan |
| 006 | Uiua | 231017T064239Z | chunes |
| 040 | POSIX Shell + Utilities | 221229T225634Z | набиячлэ |
| 036 | Factor | 221228T220724Z | chunes |
| 005 | BQN | 220726T034559Z | DLosc |
| 056 | Python | 220725T215245Z | Eric Xue |
| 026 | Desmos | 220725T101657Z | fad |
| 006 | Vyxal | 220725T015123Z | emanresu |
| 007 | Vyxal | 220723T045843Z | tybocopp |
| 015 | APLDyalog Unicode | 210204T132542Z | Kamila S |
| 008 | Stax | 210204T074014Z | Razetime |
| 057 | Tcl | 171021T163435Z | sergiol |
| 120 | SmileBASIC | 180320T205204Z | 12Me21 |
| 028 | Perl 6 | 170829T201137Z | nwellnho |
| nan | Perl 5 | 171027T231113Z | Xcali |
| 044 | Jq 1.5 | 170921T062549Z | jq170727 |
| 041 | Swift 4 | 170921T055754Z | Alexande |
| 004 | Jelly | 170828T203626Z | Adalynn |
| nan | C# .NET Core | 170828T142259Z | Dennis.V |
| 061 | Scala | 170830T071523Z | Llew Val |
| 065 | Erlang | 170830T013209Z | JoshRage |
| 028 | Python with numpy | 170829T144651Z | user4854 |
| 021 | TIBasic | 170828T180835Z | Timtech |
| 045 | Python 2 | 170828T120332Z | Mr. Xcod |
| 044 | Python 3 | 170828T120717Z | ovs |
| 029 | R | 170828T130615Z | Giuseppe |
| 059 | Kotlin | 170828T130122Z | Valentin |
| 042 | Ruby | 170829T064851Z | ymbirtt |
| 059 | Common Lisp | 170828T190425Z | Renzo |
| 097 | Rust | 170828T201637Z | jferard |
| 006 | J | 170828T222655Z | Adalynn |
| 012 | K | 170828T223701Z | Adalynn |
| 020 | MY | 170828T215007Z | Adalynn |
| 005 | Dyalog APL | 170828T203230Z | Adalynn |
| 080 | Swift | 170828T201006Z | Fennelou |
| 007 | J | 170828T140455Z | cole |
| 074 | PHP | 170828T181923Z | Titus |
| 045 | Python 3 | 170828T124216Z | Luis Men |
| 040 | Clojure | 170828T174204Z | MattPutn |
| 043 | JavaScript ES6 | 170828T121252Z | Shaggy |
| 013 | Japt | 170828T160018Z | Justin M |
| 016 | CJam | 170828T144724Z | Erik the |
| 075 | C | 170828T133432Z | Steadybo |
| 031 | Mathematica | 170828T135033Z | alephalp |
| 079 | Swift 3 | 170828T132751Z | Anonymou |
| 005 | 05AB1E | 170828T131819Z | Erik the |
| 007 | 05AB1E | 170828T120312Z | Emigna |
| 007 | Pyth | 170828T131536Z | Erik the |
| 063 | Java 8 | 170828T121127Z | Kevin Cr |
| 034 | Haskell | 170828T124912Z | Cristian |
| 007 | MATL | 170828T122246Z | Luis Men |
| 011 | Pyth | 170828T122934Z | Mr. Xcod |
| 006 | Jelly | 170828T121140Z | Jonathan |
| 036 | Octave | 170828T122015Z | user7378 |
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
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
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
Vyxal, 6 bytes
₌Þ∴=›*
₌ # Do both of these...
Þ∴ # Elementwise maximum
=› # Equality, incremented -> list of 1s where unequal, 2s where equal
* # Multiply by that.
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.
Explanation:
Z # Zip
ƛ # On each pair
[ # If
≈ # All are equal
∑ # Sum
| # Otherwise
G # Get the greatest value
APL(Dyalog Unicode), 15 bytes SBCS
(⊣⌈⊢×1+⊣=⊢)/¨,¨
Can be golfed as:
((⌈×1+=)/¨,¨)
⌈×1+=
So it ends up being the same as the other APL answer...
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)
Perl 5, 59 + 1 (-p) = 60 bytes
say join$",map$_>($t=$F[$i++])?$_:$_<$t?$t:$_*2,split/ /,<>
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!
Swift 4, 41 bytes
{zip($0,$1).map{$0==$1 ?2*$0:max($0,$1)}}
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!")
}
Jelly, 4 bytes
=‘×»
This uses the exact same approach as my APL answer, except Jelly has a builtin for adding one to a number!
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()
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;}
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.
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
Python with numpy, 28 bytes
lambda a,b:a*(a>=b)+b*(b>=a)
Assumes input is given as two numpy arrays.
TI-Basic, 23 21 bytes
Prompt A,B
(ʟA=ʟB)ʟA+max(ʟA,ʟB
Too bad lists take up two bytes each...
Python 2, 45 bytes
A mix of my initial solution and @ovs'.
lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)
Python 2, 49 bytes
lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]
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)]
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.
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.
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()
Common Lisp, 60 59 bytes
(mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read))
-1 byte thanks to @Zacharý!
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>;
Saved 8 bytes thanks to @mgc
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.
Comparison with APL
>.*1+=
⌈ ×1+=
K, 12 bytes
{x|y*(1+x=y)}
Exact same strategy as my other answers.
Any golfing tips are welcome, as I don't really know K.
MY, 20 bytes
αωD6ǵ'69ǵ';ƒ⇹(αω=‘×↵
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.
Dyalog APL, 5 bytes
⌈×1+=
How?
⌈, element-wise maximum of the arguments×, element-wise multiply1+=, 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.
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
J, 7 bytes
>.`+@.=
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).
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.
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)
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.)
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);}
Mathematica, 31 bytes
MapThread[If[#==#2,2#,Max@##]&]
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
05AB1E, 9 8 7 bytes
Saved a byte as Erik the Outgolfer pointed out that a list of lists is valid input.
øεMsËi·
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
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:
(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
MATL, 7 bytes
X>tG=s*
Input is a two-row matrix, where each row is one of the arrays.
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
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!
Octave, 36 Byte
@(a,b)a.*(a>b)+b.*(b>a)+2.*a.*(a==b)