Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to count number of isosceles triangle from colored vertex regular polygon in Python
In a regular polygon with n sides, we need to count isosceles triangles formed by vertices of the same color. The vertices are colored either blue (0) or red (1) represented as a binary string. An isosceles triangle has at least two sides of equal length.
So, if the input is like polygon = "111010", then the output will be 2 because there are two triangles ACE and AFE as shown in the diagram ?
Algorithm Overview
The solution uses two helper functions to calculate the total count ?
- all(n) ? Counts all possible isosceles triangles in a regular n-sided polygon
- non(a, n) ? Counts triangles with vertices of different colors
- Final result ? Total triangles minus mixed-color triangles divided by 2
Implementation
def all(n):
"""Count all possible isosceles triangles in regular n-gon"""
if n % 2 == 1:
no = n * (n - 1) // 2
else:
no = n * (n // 2 - 1)
# Subtract equilateral triangles (counted multiple times)
if n % 3 == 0:
no -= n // 3 * 2
return no
def non(a, n):
"""Count triangles with vertices of different colors"""
if n % 2 == 1:
# Odd n case
s0 = s1 = 0
for i in range(n):
if a[i] == '0':
s0 += 1
else:
s1 += 1
s = s0 * s1 * 6
# Adjust for equilateral triangles
if n % 3 == 0:
n1 = n // 3
n2 = n1 * 2
for i in range(n):
if a[i] != a[(i + n1) % n]:
s -= 2
if a[i] != a[(i + n2) % n]:
s -= 2
else:
# Even n case
s00 = s01 = s10 = s11 = 0
# Count colors at even positions
for i in range(0, n, 2):
if a[i] == '0':
s00 += 1
else:
s01 += 1
# Count colors at odd positions
for i in range(1, n, 2):
if a[i] == '0':
s10 += 1
else:
s11 += 1
s = s00 * s01 * 8 + s10 * s11 * 8 + s00 * s11 * 4 + s10 * s01 * 4
# Adjust for diameter symmetry
n1 = n // 2
for i in range(n):
if a[i] != a[(i + n1) % n]:
s -= 2
# Adjust for equilateral triangles if n divisible by 3
if n % 3 == 0:
n1 = n // 3
n2 = n1 * 2
for i in range(n):
if a[i] != a[(i + n1) % n]:
s -= 2
if a[i] != a[(i + n2) % n]:
s -= 2
return s // 2
def solve(polygon):
"""Main function to count same-colored isosceles triangles"""
n = len(polygon)
total_isosceles = all(n)
mixed_color = non(polygon, n)
same_color = total_isosceles - mixed_color // 2
return int(same_color)
# Test the solution
polygon = "111010"
result = solve(polygon)
print(f"Number of same-colored isosceles triangles: {result}")
Number of same-colored isosceles triangles: 2
How It Works
The algorithm works by ?
- Calculate total isosceles triangles ? Use geometric properties of regular polygons
- Count mixed-color triangles ? Triangles with vertices of different colors
- Subtract to get same-color triangles ? Total minus mixed-color gives the answer
For a hexagon like "111010", vertices A, B, C, E are red (1) and D, F are blue (0). The isosceles triangles with same-colored vertices are ACE and AFE.
Key Points
- Regular polygons have special symmetry properties that create isosceles triangles
- The algorithm handles odd and even n cases differently due to geometric constraints
- Equilateral triangles require special handling as they're counted multiple times
- Integer division (//) is used to avoid floating-point precision issues
Conclusion
This solution efficiently counts same-colored isosceles triangles in regular polygons using combinatorial geometry. The key insight is calculating total isosceles triangles and subtracting those with mixed colors.
