Created
July 28, 2025 11:08
-
-
Save enderahmetyurt/e0c60fe5bed3fb25b05b78562028a1dc to your computer and use it in GitHub Desktop.
Given an array of side lengths, write a function to determine they can form a hexagon with three side-length pairs (as in, three pairs of equal sides needed). Return true if possible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def can_form_hexagon(sides) | |
return false unless sides.size == 6 | |
tally = sides.tally | |
return false unless tally.size == 3 && tally.values.all?(2) | |
a, b, c = tally.keys.sort | |
[a, b, c].all? { |x| x < (a + b + c - x) } | |
end | |
puts can_form_hexagon([2, 3, 8, 8, 2, 3]) | |
puts can_form_hexagon([1, 2, 3, 4, 5, 6]) | |
puts can_form_hexagon([2, 2, 2, 2, 2, 2, 2]) | |
puts can_form_hexagon([1, 1, 10, 10, 1, 1]) | |
puts can_form_hexagon([3, 3, 4, 4, 5, 5]) | |
puts can_form_hexagon([1, 2, 2, 3, 3, 4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment