Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 258 characters in body
Source Link
Giuseppe
  • 29.4k
  • 3
  • 33
  • 106

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)

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.

Try it online! (new version)

Try it online! (original)

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)

added 639 characters in body
Source Link
Giuseppe
  • 29.4k
  • 3
  • 33
  • 106

R, 31(削除) 31 (削除ここまで) 29 bytes

function(a,b)pmax(a,b)+a*(a==b)!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!a-b returns a list ofas TRUE where a==b and FALSE indices where the lists are equalotherwise, and they are coercedequivalent to 1a==b and. However, for whatever reason, R doesn't require parentheses around 0!a-b respectivelyas it does for a==b, which saved me two bytes.

Try it online! (new version)

Try it online! (original)

R, 31 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). a==b returns a list of TRUE and FALSE indices where the lists are equal, and they are coerced to 1 and 0 respectively.

Try it online!

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.

Try it online! (new version)

Try it online! (original)

Source Link
Giuseppe
  • 29.4k
  • 3
  • 33
  • 106

R, 31 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). a==b returns a list of TRUE and FALSE indices where the lists are equal, and they are coerced to 1 and 0 respectively.

Try it online!

AltStyle によって変換されたページ (->オリジナル) /