In mathematics, the four colour theorem is a proposition that states that it is possible to colour any land map in which the countries form one unit (without the exclaves) using only four colours without giving two neighbouring countries the same colour. Here, two countries are neighbouring if they have a boundary in common, not if they are only connected by one point. You will see below that this isn't possible when using less than four colours. Luxemburg, Germany, France and Belgium are all neighbouring countries, so at least four colours are needed.
Define a class CountryMap which can be used to represent coloured maps. The objects of this class must contain at least the following methods:
An initializing method that allows to initialize objects based on two given text files. The locations of both these files must be given when making a new object. The first file contains a ist of all countries and the colour that the countries were coloured with. Every line of the file contains the name of a country and the name of a colour, separated by a tab. The second file contains a list of all neighbouring countries. Every line of the file contains the names of two neighbouring countries, separated by a tab.
A method numberCountries that prints the number of countries that occur on the map. No arguments must be given to this method.
A method numberColours that prints the number of colours that are used to colour the countries on the map. No arguments must be given to this method.
A method colour to which the name of a country must be given. The method must print the colour that was used for the country on the map.
A method neighbours to which the name of a country must be given. The method must print a collection that contains all neighbouring countries of the given country.
A method areNeighbours to which the names of two countries must be given. The method must print a Boolean value, that indicates whether two countries are neighbours on the map.
A method invalidNeighbours to which no arguments must be given. The method must print a list of all neighbouring countries that are indicated with the same colour on the map. Every element of this list is a tuple of three elements, where the first two elements represent the names of the neighbouring countries that were indicated with the same colour on the map. This colour is the third element in the tuple. The names of the neighbouring countries must be ordered alphabetically in the tuples, and the list must be sorted, first on the name of the first country and then on the name of the second country.
When teh name of a country is given as an argument to a method, the method must verify whether that name occurs in the list of countries from the first text file that was given when making the object of the class CountryMap. If this isn't the case, the method should raise an AssertionError with the text unknown country.
In the example session below, we assume that the text files colours1.txt1, colours2.txt2 and neighbouringcountries.txt3 are situated in the current directory.
>>> countrymap = CountryMap('colours1.txt', 'neighbouringcountries.txt')
>>> countrymap.numberCountries()
208
>>> countrymap.numberColours()
5
>>> countrymap.colour('Belgium')
'rood'
>>> countrymap.neighbours('Belgium')
{'Netherlands', 'Germany', 'Luxembourg', 'France'}
>>> countrymap.areNeighbours('Belgium', 'Germany')
True
>>> countrymap.areNeighbours('Belgium', 'Italy')
False
>>> countrymap.invalidNeighbours()
[]
>>> countrymap = CountryMap('kleuren2.txt', 'buurlanden.txt')
>>> countrymap.invalidNeighbours()
[('Angola', 'Democratic Republic of the Congo', 'blauw'), ('Democratic Republic of the Congo', 'Uganda', 'blauw')]
>>> countrymap.colour('Oz')
Traceback (most recent call last):
AssertionError: unknown country
>>> countrymap.neighbours('Oz')
Traceback (most recent call last):
AssertionError: unknown country
>>> countrymap.areNeighbours('Belgium', 'Oz')
Traceback (most recent call last):
AssertionError: unknown country
>>> countrymap.areNeighbours('Oz', 'Belgium')
Traceback (most recent call last):
AssertionError: unknown country