Skip to content

Instantly share code, notes, and snippets.

@cidrblock
Created August 25, 2022 16:45
Show Gist options
  • Save cidrblock/f848e0fd05c70ca418e3f2039ea79928 to your computer and use it in GitHub Desktop.
Save cidrblock/f848e0fd05c70ca418e3f2039ea79928 to your computer and use it in GitHub Desktop.
Triangle solver
"""Convert to feet/inches."""
import math
def div32(number: float) -> int:
"""Return the closest 32nd of an inch.
:param number: number to be divided
:return: closest number divided by 32
"""
return round((number - int(number)) * 1000 / 32.0)
def gcd(numerator: int, denominator: int) -> int:
"""Determine the greates common divisor.
Euclidean Algorithm (recursive form)
:param numerator: The numerator
:param denominator: The denominator
:return: The greatest common divisor
"""
if denominator == 0:
return numerator
return gcd(denominator, numerator % denominator)
def mkinch(number):
"""Convert to feet/inches.
:param number: The number to convert
:return: The number in feet/inches
"""
numer = div32(number)
thegcd = gcd(numer, 32)
feet = int(int(number) / 12)
inches = int(number) - 12 * feet
return f"{feet}' {inches} {int(numer/thegcd)}/{int(32/thegcd)}\""
def playset():
"""Find the diagonal of the palyset."""
left = 93
right = 93
hypotenuse = math.sqrt(left**2 + right**2)
print(mkinch(hypotenuse))
if __name__ == "__main__":
playset()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment