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
- This is code-golf, so shortest answer in bytes wins.
- Standard input and output rules apply. You may take input (and output) in any reasonable format.
- Default Loopholes are forbidden.
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]
-
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– Wheat Wizard ♦2017年08月28日 17:03:29 +00:00Commented 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– user709742017年08月28日 18:06:04 +00:00Commented Aug 28, 2017 at 18:06
54 Answers 54
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.
-
\$\begingroup\$ Wow, that was really fast! \$\endgroup\$user70974– user709742017年08月28日 12:05:10 +00:00Commented 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– jferard2017年08月28日 20:09:20 +00:00Commented Aug 28, 2017 at 20:09 -
\$\begingroup\$ @jferard I fact, that's already Luis' solution. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年08月28日 20:12:25 +00:00Commented Aug 28, 2017 at 20:12
-
\$\begingroup\$ @Mr.Xcoder Oops! I didn't read the whole page... \$\endgroup\$jferard– jferard2017年08月28日 20:18:51 +00:00Commented Aug 28, 2017 at 20:18
-
\$\begingroup\$ Never say "above," as the ordering can change (I don't see your solution above) \$\endgroup\$Adalynn– Adalynn2017年08月28日 21:53:44 +00:00Commented Aug 28, 2017 at 21:53
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()
-
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\$Jonathan Allan– Jonathan Allan2017年08月28日 13:23:13 +00:00Commented Aug 28, 2017 at 13:23
-
2\$\begingroup\$ The worst "feature" ever...btw don't feel bad about it. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月28日 13:26:45 +00:00Commented Aug 28, 2017 at 13:26
-
\$\begingroup\$ Link to Kotlin Playground: pl.kotl.in/Qne3U_j4l \$\endgroup\$Eric Xue– Eric Xue2022年07月25日 21:49:39 +00:00Commented Jul 25, 2022 at 21:49
Jelly, 4 bytes
=×ばつ»
This uses the exact same approach as my APL answer, except Jelly has a builtin for adding one to a number!
-
\$\begingroup\$ Hate to be a spoilsport, but aren't some of those characters more than one byte each in any sensible encoding? \$\endgroup\$Cedric Knight– Cedric Knight2017年08月30日 01:26:54 +00:00Commented Aug 30, 2017 at 1:26
-
\$\begingroup\$ This uses the jelly codepage. \$\endgroup\$Adalynn– Adalynn2017年08月30日 01:37:21 +00:00Commented Aug 30, 2017 at 1:37
-
\$\begingroup\$ I finally won against competition! \$\endgroup\$Adalynn– Adalynn2017年09月07日 00:39:39 +00:00Commented 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\$Magic Octopus Urn– Magic Octopus Urn2017年10月27日 18:58:37 +00:00Commented Oct 27, 2017 at 18:58
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.)
-
\$\begingroup\$
j.value.split`,`.map(eval)oreval('['+j.value+']')? Alsox+ywould look neater IMHO. \$\endgroup\$Neil– Neil2017年08月28日 14:56:12 +00:00Commented 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– Shaggy2017年08月28日 16:13:17 +00:00Commented Aug 28, 2017 at 16:13
-
2\$\begingroup\$ Same byte count:
x!y=max x y+sum[x|x==y]. \$\endgroup\$nimi– nimi2017年08月28日 19:46:19 +00:00Commented Aug 28, 2017 at 19:46
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.
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.
-
\$\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– JDL2017年08月29日 15:29:29 +00:00Commented 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– Giuseppe2017年08月29日 15:55:17 +00:00Commented Aug 29, 2017 at 15:55
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!
-
\$\begingroup\$ Outgolfed!. +1 for the practically verbatim interpretation of the question. \$\endgroup\$Adalynn– Adalynn2017年08月28日 20:47:00 +00:00Commented Aug 28, 2017 at 20:47
-
\$\begingroup\$ ...and I've been caught out be forgetting that
»vectorises before! \$\endgroup\$Jonathan Allan– Jonathan Allan2017年08月28日 20:55:23 +00:00Commented Aug 28, 2017 at 20:55 -
\$\begingroup\$ What else would it do, take the maximum array in some convoluted way? \$\endgroup\$Adalynn– Adalynn2017年08月28日 21:13:16 +00:00Commented 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\$Jonathan Allan– Jonathan Allan2017年08月28日 21:20:35 +00:00Commented Aug 28, 2017 at 21:20 -
\$\begingroup\$ So, basically infinite-base? \$\endgroup\$Adalynn– Adalynn2017年08月28日 21:21:32 +00:00Commented Aug 28, 2017 at 21:21
-
\$\begingroup\$ Nice idea using
γ! \$\endgroup\$Emigna– Emigna2017年08月28日 13:20:21 +00:00Commented 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\$Erik the Outgolfer– Erik the Outgolfer2017年08月28日 13:21:17 +00:00Commented Aug 28, 2017 at 13:21 -
\$\begingroup\$ How about
øεMÃO? \$\endgroup\$Emigna– Emigna2017年08月28日 13:23:19 +00:00Commented Aug 28, 2017 at 13:23 -
1\$\begingroup\$ @Emigna Cool, thanks! (duh why didn't I think of
MÃ) yay got the lead now :p btwøεZÃOwould work too \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月28日 13:23:54 +00:00Commented Aug 28, 2017 at 13:23
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
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
-
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\$Olivier Grégoire– Olivier Grégoire2017年08月28日 12:18:58 +00:00Commented 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\$Olivier Grégoire– Olivier Grégoire2017年08月28日 12:25:21 +00:00Commented Aug 28, 2017 at 12:25 -
\$\begingroup\$ And you can save one more byte by putting
float A, Bin theforinitialization. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年08月28日 12:28:01 +00:00Commented 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\$Olivier Grégoire– Olivier Grégoire2017年08月28日 12:44:08 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2017年08月28日 12:53:10 +00:00Commented Aug 28, 2017 at 12:53
-
\$\begingroup\$ @Jakube That's Erik's solution already, sadly. I wanted to use that too, but I can't now \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年08月29日 13:56:54 +00:00Commented Aug 29, 2017 at 13:56
-
\$\begingroup\$ Oh, didn't see that one. \$\endgroup\$Jakube– Jakube2017年08月29日 13:57:29 +00:00Commented Aug 29, 2017 at 13:57
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
-
\$\begingroup\$ Wow, that was really fast! \$\endgroup\$user70974– user709742017年08月28日 12:05:08 +00:00Commented 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\$Erik the Outgolfer– Erik the Outgolfer2017年08月28日 13:01:18 +00:00Commented 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– Emigna2017年08月28日 13:16:58 +00:00Commented Aug 28, 2017 at 13:16
-
1\$\begingroup\$ @Emigna Tip: don't make rules out of your mind ;) \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月28日 13:19:06 +00:00Commented 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– Emigna2017年08月28日 13:19:57 +00:00Commented Aug 28, 2017 at 13:19
Mathematica, 31 bytes
MapThread[If[#==#2,2#,Max@##]&]
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)
-
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– Mr. Xcoder2017年08月28日 12:43:01 +00:00Commented Aug 28, 2017 at 12:43 -
\$\begingroup\$ @Mr.Xcoder Thanks! Good idea! \$\endgroup\$Luis Mendo– Luis Mendo2017年08月28日 12:47:07 +00:00Commented Aug 28, 2017 at 12:47
-
1
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).
TI-Basic, (削除) 23 (削除ここまで) 21 bytes
Prompt A,B
(ʟA=ʟB)ʟA+max(ʟA,ʟB
Too bad lists take up two bytes each...
-
\$\begingroup\$ You can save two bytes by prompting for
XandY, then usingʟXandʟYto access them, i.e. "Prompt X,Y:ʟX(ʟX=ʟY)+max(ʟ1,ʟ2". \$\endgroup\$Scott Milner– Scott Milner2017年08月29日 21:53:53 +00:00Commented Aug 29, 2017 at 21:53 -
\$\begingroup\$ Also, this is currently invalid, since
L1(L1=L2)attempts to get the element ofL1at a list, which throws an error. To fix that, swap the order, i.e.(L1=L2)L1. \$\endgroup\$Scott Milner– Scott Milner2017年08月29日 21:58:16 +00:00Commented Aug 29, 2017 at 21:58 -
\$\begingroup\$ @ScottMilner Thanks for pointing both of those out. \$\endgroup\$Timtech– Timtech2017年08月29日 23:04:49 +00:00Commented Aug 29, 2017 at 23:04
Python with numpy, 28 bytes
lambda a,b:a*(a>=b)+b*(b>=a)
Assumes input is given as two numpy arrays.
-
\$\begingroup\$ If we are using numpy then here is my worse solution:
lambda a,b:n.fmax(a,b)*((a==b)+1)\$\endgroup\$Erich– Erich2017年08月29日 18:09:18 +00:00Commented 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– user485432017年08月29日 18:22:08 +00:00Commented 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– Erich2017年08月29日 18:32:08 +00:00Commented Aug 29, 2017 at 18:32
-
\$\begingroup\$ @Erich In general, you can only refer to a variable
nif you've definednin your code, so imports must be explicit. By default, we allow functions or full programs as answers, which includes anonymous functions. \$\endgroup\$user48543– user485432017年08月29日 18:39:44 +00:00Commented 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 usingreturn? \$\endgroup\$Adalynn– Adalynn2017年08月29日 20:29:25 +00:00Commented Aug 29, 2017 at 20:29
Octave, 36 Byte
@(a,b)a.*(a>b)+b.*(b>a)+2.*a.*(a==b)
-
1\$\begingroup\$ Welcome to the site! You can save a few bytes with
@(a,b)max(a,b).*(1+(a==b))\$\endgroup\$Luis Mendo– Luis Mendo2017年08月28日 12:27:12 +00:00Commented Aug 28, 2017 at 12:27 -
5\$\begingroup\$ Or better, using your approach,
@(a,b)a.*(a>=b)+b.*(b>=a)\$\endgroup\$Luis Mendo– Luis Mendo2017年08月28日 12:29:17 +00:00Commented Aug 28, 2017 at 12:29
Common Lisp, (削除) 60 (削除ここまで) 59 bytes
(mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read))
-1 byte thanks to @Zacharý!
-
\$\begingroup\$ 59 bytes:
(mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read)). \$\endgroup\$Adalynn– Adalynn2017年08月29日 00:42:33 +00:00Commented 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– Adalynn2017年08月29日 08:11:46 +00:00Commented Aug 29, 2017 at 8:11
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;}
-
\$\begingroup\$ You can drop the LINQ answer by a few bytes by changing namespace System.LINQ to using System.LINQ \$\endgroup\$jkelm– jkelm2017年08月28日 18:27:17 +00:00Commented 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\$Dennis.Verweij– Dennis.Verweij2017年08月28日 19:23:30 +00:00Commented Aug 28, 2017 at 19:23
-
\$\begingroup\$ System.Linq is included in the "Visual C# Interactive Compiler". I am not totally sure about returning
ArrayvsIListvsIEnumerable, but if all are eligible then you can get the byte count to 37 - tio.run/##Sy7WTS7O/… \$\endgroup\$dana– dana2018年12月17日 04:22:56 +00:00Commented Dec 17, 2018 at 4:22
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
Vyxal, 6 bytes
=Þ∴=›*
= # Do both of these...
Þ∴ # Elementwise maximum
=› # Equality, incremented -> list of 1s where unequal, 2s where equal
* # Multiply by that.
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
-
\$\begingroup\$ Do you need spaces around
(x,y)and before?? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月28日 13:28:30 +00:00Commented 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– user709742017年08月28日 13:31:14 +00:00Commented 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\$AnonymousReality– AnonymousReality2017年08月28日 13:34:47 +00:00Commented 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– Mr. Xcoder2017年08月28日 13:38:33 +00:00Commented 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– Mr. Xcoder2017年08月28日 13:40:15 +00:00Commented Aug 28, 2017 at 13:40
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);}