Brad Lucas

A blog mostly about programming
March 23, 2015

Countries without the letter 'A' in their name

The problem. Find all the countries in the world without an ‘A’ in their name.

  1. Find a list of the world’s countries. [http://www.listofcountriesoftheworld.com(http://www.listofcountriesoftheworld.com)
  2. Cut/paste the list to a file. countries.txt
  3. Note the file is a list of numbers and names. Need to remove the numbers and filter out the ones without ‘A’s.
 def remove_country_line_numbers():
    return [l.strip() for l in  open('countries.txt').readlines() \
                 if not l.strip().isdigit() and l.lower().find('a')==-1]

Usage:

In [81]: ", ".join(remove_country_line_numbers())
Out[81]: "Belgium, Belize, Benin, Brunei, Burundi, Chile, Comoros, Congo, 
Republic of the, Cote d'Ivoire, Cyprus, Czech Republic, Djibouti, Egypt,
 Fiji, Greece, Guernsey, Hong Kong, Jersey, Lesotho, Liechtenstein, Luxembourg, 
Mexico, Morocco, Niger, Niue, Peru, Philippines, Puerto Rico, Reunion, Seychelles, 
Sweden, Timor-Leste, Togo, Turkey, United Kingdom, Yemen"


Tags: python