This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
ProgrammingChallenges/constellation.py

58 lines
1.3 KiB
Python
Raw Normal View History

2019-05-02 15:01:00 +00:00
class Star:
2019-05-07 09:48:40 +00:00
def __init__(self):
self.x = None
self.y = None
2019-05-02 15:01:00 +00:00
2019-05-07 09:48:40 +00:00
def dist_sqr(self, star):
return (self.x - star.x) ** 2 + (self.y - star.y) ** 2
2019-05-02 15:01:00 +00:00
2019-05-07 10:11:11 +00:00
def __repr__(self):
return str(self)
def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
def print_M(M):
for i in range(len(M)):
print(M[i])
print()
2019-05-07 09:48:40 +00:00
def find_constellations(M, S):
for i in range(len(S)):
for j in range(i+1, len(S)):
M[i][j] = Star.dist_sqr(S[i], S[j])
M[j][i] = M[i][j]
2019-05-07 10:11:11 +00:00
#print(str(S[i]) + " - " + str(S[j]) + ": " + str(M[i][j]))
#print_M(M)
#print(S)
2019-05-07 09:48:40 +00:00
c = 0
2019-05-02 15:01:00 +00:00
for i in range(len(S)):
2019-05-07 09:48:40 +00:00
D = {}
for j in range(len(S)):
if i == j:
continue
if M[i][j] not in D:
D[M[i][j]] = 1
else:
D[M[i][j]] += 1
for k in D:
if D[k] >= 2:
c += int(D[k] * (D[k] - 1) / 2)
return c
if __name__ == "__main__":
n = int(input())
2019-05-07 10:11:11 +00:00
M = []
for i in range(n):
M.append([None] * n)
2019-05-07 09:48:40 +00:00
S = []
for i in range(n):
st = input().split()
star = Star()
star.x = int(st[0])
star.y = int(st[1])
S.append(star)
print(find_constellations(M, S))
2019-05-02 15:01:00 +00:00