@SnoopJeDi/

SugaryAlertListeners

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
A simple class for the traveling salesman problem, based on a question
in #python on freenode on Oct 31, 2018.
"""

import itertools

class City:
    """ Represents a city at the position `(x, y)` """
    def __init__(self, x, y, name):
        self.x = x
        self.y = y
        self.name = name

    def __str__(self):
        return self.name
      
    def __sub__(self, other):
        """ Calculate the Euclidean distance between two City objects """
        return ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5

cities = [
  City(1, 2, 'A'),
  City(3, 4, 'B'),
  City(5, 6, 'C'),
]

for c1, c2 in itertools.combinations(cities, 2):
  print(f'Distance between {c1} and {c2} is {c1-c2:.2f}')