Skip to content

Instantly share code, notes, and snippets.

@ty-porter
Created October 1, 2020 18:22
Show Gist options
  • Save ty-porter/5afbfd9c0c7804f16f54ac2df3d6e168 to your computer and use it in GitHub Desktop.
Save ty-porter/5afbfd9c0c7804f16f54ac2df3d6e168 to your computer and use it in GitHub Desktop.
Codewars Solutions
# Your task is to find the minimum SQUARE area in which you can place two identical rectangles. The sides of the rectangles should be parallel to the sides of the square.
# You are given two identical rectangles with side lengths a and b with 1 <= a, b <= 100.
# Find the square with minimum area that contains both given rectangles. Rectangles can be rotated (both or just one) and moved, but the sides of the rectangles should be parallel to the sides of the desired square.
def minimal_square(a, b):
# This solution abuses the fact that we can multiply by True/False:
# For instance, 1 * True = 1 and 1 * False = 0
#
# A step further:
# (a * (a > b ) + b * ( b >= a )) is equivalent to max(a, b)
#
# And:
# (a * ( a < b ) + b * ( b <= a )) is equivalent to min(a, b)
#
# Putting it all together, the following is equivalent to:
# max(min(a, b)*2, max(a, b)) ** 2
return (((a*(a<b)+b*(b<=a))*2)*(((a*(a<b)+b*(b<=a))*2)>(a*(a>b)+b*(b>=a))))+\
((a*(a>b)+b*(b>=a))*(((a*(a<b)+b*(b<=a))*2)<=(a*(a>b)+b*(b>=a)))) ** 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment