Creates and returns a tuple of all the countries that have consecutive repeated letters

Viewed 225
def get_doubled_letter_countries()
This function creates and returns a tuple of all the countries that have consecutive repeated letters. When called it returns that tuple, in alphabetical order by the doubled letters:
('morocco', 'greece', 'marshall islands', 'seychelles', 'cameroon', 'philippines', 'andorra', 'sierra leone', 'guinea-bissau', 'russia', 'saint kitts and nevis')
Notice they are in alphabetical order by the doubled letters: c, e, l, l, o, p, r, r, s, s, t

This is my code:

def get_doubled_letter_countries(double_letter_list):
    """"
    This function creates and returns a tuple of all the countries that have consecutive repeated letters.
    When called it returns that tuple, in alphabetical order by the doubled letters:
    """
    double_letter = re.compile(r'.*(.)\1.*', re.IGNORECASE)
    double_letter_list = []
    for countrie in countries:
        for word in countrie.split(" "):
            match = double_letter.match(word)
            if match:
                double_letter_list.sort()
                double_letter_list.append(match.group())
    print(tuple(double_letter_list))

It prints this: ('Andorra', 'Cameroon', 'Greece', 'Guinea-Bissau', 'Kitts', 'Marshall', 'Morocco', 'Philippines', 'Russia', 'Seychelles', 'Sierra')

I need it to print this: ('morocco', 'greece', 'marshall islands', 'seychelles', 'cameroon', 'philippines', 'andorra', 'sierra leone', 'guinea-bissau', 'russia', 'saint kitts and nevis')

2 Answers

Your regex statement does not match what you are trying to achieve.

Remove the .* from either side of your capture group.

(.)\1

Given the requirement to sort by consecutive letters, we can store both the country and the matched regex in a list of tuples. Then we can use sorted() and lambda to sort by the matched elements.


countries= ('morocco', 'greece', 'marshall islands', 'seychelles', 'cameroon', 'philippines', 'andorra', 'sierra leone', 'guinea-bissau', 'russia', 'saint kitts and nevis')

def sorted_doubles(countries):

    result = []
    pattern = re.compile(r'(.)\1', re.IGNORECASE)

    for c in countries:
        match = pattern.search(c)
        if match:
            result.append((c, match.group(0)))

    sorted_r = sorted(result, key=lambda x: x[1])

    return tuple([country for country, _ in sorted_r])

print(sorted_doubles(countries))
#('morocco', 'greece', 'marshall islands', 'seychelles', 'cameroon', 'philippines', 'andorra', 'sierra leone', 'guinea-bissau', 'russia', 'saint kitts and nevis')
def get_doubled_letter_countries(countries):
    double_letter_pattern = re.compile(r'(.)\1', re.IGNORECASE)
    double_latter_countries=[country for country in countries if double_letter_pattern.search(country)]
    double_letter_list=sorted(double_latter_countries, key=lambda x: double_letter_pattern.findall(x))
    return tuple(double_letter_list)

you can pass the list of countries in function and it will return the list in alphabetical order of the double letters.

Related