Vertex AI documentation is no longer being updated
Vertex AI's services are now part of Gemini Enterprise Agent Platform. See the most up-to-date information in the Agent Platform documentation.
Python from documentation
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.
|
from collections import namedtuple
from math import sqrt
# A point has an x and a y coordinate
Point = namedtuple('Point', 'x y')
# A triangle is defined by three points
Triangle = namedtuple('Triangle', 'a b c')
def distance(p, q):
"""Returns the distance between the provided pair of points"""
return sqrt((p.x - q.x)**2 + (p.y - q.y)**2)
def perimeter(triangle):
"""Returns the perimeter of the provided triangle"""
return distance(triangle.a, triangle.b) + distance(triangle.b, triangle.c) + distance(triangle.c, triangle.a)
def area(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.
from collections import namedtuple
from math import sqrt
# A point has an x and a y coordinate
Point = namedtuple('Point', 'x y')
# A triangle is defined by three points
Triangle = namedtuple('Triangle', 'a b c')
def distance(p, q):
"""Returns the distance between the provided pair of points"""
return sqrt((p.x - q.x)**2 + (p.y - q.y)**2)
def perimeter(triangle):
"""Returns the perimeter of the provided triangle"""
return distance(triangle.a, triangle.b) + distance(triangle.b, triangle.c) + distance(triangle.c, triangle.a)
def area(triangle):
"""Returns the area of the provided triangle using Heron's formula"""
# Calculate the lengths of the three sides
side_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))
return sqrt(s * (s - side_ab) * (s - side_bc) * (s - side_ca))
|
| Model: |
gemini-2.5-pro |
| Temperature: |
0.2 |
| Max output tokens: |
8192 |
| TopK: |
40.0 |
| TopP: |
0.95 |
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-06-13 UTC.
[null,null,["Last updated 2026-06-13 UTC."],[],[]]