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

Commonmark migration
Source Link

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#How?

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.

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#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.

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


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.

added 26 characters in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#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 "semi-weak" typesbool class being a subclass of int.

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#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 "semi-weak" types.

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#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.

added 2 characters in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221

Python 2, 45 bytes

A mix of my initial solution above and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#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 "semi-weak" types.

Python 2, 45 bytes

A mix of my solution above and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#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 "semi-weak" types.

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

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)]

Try it online!


#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 "semi-weak" types.

[Edit removed during grace period]
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading
deleted 1 character in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading
added 826 characters in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading
added 4 characters in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading
added 596 characters in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading
deleted 1 character in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading

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