Starting April 29, 2025, Gemini 1.5 Pro and Gemini 1.5 Flash models are not available in projects that have no prior usage of these models, including new projects. For details, see Model versions and lifecycle.
Stay organized with collections
Save and categorize content based on your preferences.
Generate a Python function given its docstring
You can query a model directly and test the results returned when using
different parameter values with the Cloud console, or by calling the
Vertex AI API directly.
fromcollectionsimportnamedtuplefrommathimportsqrt# A point has an x and a y coordinatePoint=namedtuple('Point','x y')# A triangle is defined by three pointsTriangle=namedtuple('Triangle','a b c')defdistance(p,q):"""Returns the distance between the provided pair of points"""returnsqrt((p.x-q.x)**2+(p.y-q.y)**2)defperimeter(triangle):"""Returns the perimeter of the provided triangle"""returndistance(triangle.a,triangle.b)+distance(triangle.b,triangle.c)+distance(triangle.c,triangle.a)defarea(triangle):"""Returns the area of the provided triangle using Heron's formula"""
Write the body of the area function, following its docstring.
Response
Of course. Here is the completed area function, which implements Heron's formula as requested.
Heron's formula first requires calculating the semi-perimeter (half the perimeter) of the triangle. Then, it uses the lengths of the three sides and the semi-perimeter to find the area.
fromcollectionsimportnamedtuplefrommathimportsqrt# A point has an x and a y coordinatePoint=namedtuple('Point','x y')# A triangle is defined by three pointsTriangle=namedtuple('Triangle','a b c')defdistance(p,q):"""Returns the distance between the provided pair of points"""returnsqrt((p.x-q.x)**2+(p.y-q.y)**2)defperimeter(triangle):"""Returns the perimeter of the provided triangle"""returndistance(triangle.a,triangle.b)+distance(triangle.b,triangle.c)+distance(triangle.c,triangle.a)defarea(triangle):"""Returns the area of the provided triangle using Heron's formula"""# Calculate the lengths of the three sidesside_ab=distance(triangle.a,triangle.b)side_bc=distance(triangle.b,triangle.c)side_ca=distance(triangle.c,triangle.a)# Calculate the semi-perimeter (s)s=(side_ab+side_bc+side_ca)/2# Apply Heron's formula: Area = sqrt(s * (s-a) * (s-b) * (s-c))returnsqrt(s*(s-side_ab)*(s-side_bc)*(s-side_ca))
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-18 UTC."],[],[],null,[]]