patmon1
patmon1

Reputation: 1

Changing the letters in a string to upper case and lower case before an inputted letter

I have a question that I have almost figured out but am stuck on one aspect of it. The question asks me to create a function that takes a letter as a parameter that scans a list of capitals and returns a list of those capitals containing that letter. But it also turns the capital all lower case and makes the letter before and after the parameter letter uppercase. I have been able to figure out how to do it when there is only a single letter but in the case of the capital brazzaville where the parameter letter is 'z' I am struggling to get it to return it as brAzzAville. Ican only get it to return brAzZaville.

This is the code that I have so far:

    def get_funny_case_capital_cities(letter):
        my_list = []
        for element in capitals:
            if letter.lower() in element.lower():
               capital_city = list(element.lower())
               capital_city.append("")
               for i, name in enumerate(capital_city):
                       if name == letter:
                               capital_city[i] = capital_city[i].lower()
                               capital_city[i-1] = (capital_city[i-1].upper())
                               capital_city[i+1] = (capital_city[i+1].upper())
                capital_city.pop()
                capital_city = "".join(capital_city)
    my_list.append(capital_city)
return my_list


    print(get_funny_case_capital_cities('z'))
 

this gives me an output of: ['brAzZaville', 'zAgreb', 'vadUz']

capitals is a list of all country capitals in the world.

     capitals = (
'Kabul', 'Tirana (Tirane)', 'Algiers', 'Andorra la Vella', 'Luanda', "Saint John's", 'Buenos Aires', 'Yerevan',
'Canberra', 'Vienna', 'Baku', 'Nassau', 'Manama', 'Dhaka', 'Bridgetown', 'Minsk', 'Brussels', 'Belmopan',
'Porto Novo',
'Thimphu', 'Sucre', 'Sarajevo', 'Gaborone', 'Brasilia', 'Bandar Seri Begawan', 'Sofia', 'Ouagadougou', 'Gitega',
'Phnom Penh', 'Yaounde', 'Ottawa', 'Praia', 'Bangui', "N'Djamena", 'Santiago', 'Beijing', 'Bogota', 'Moroni',
'Kinshasa', 'Brazzaville', 'San Jose', 'Yamoussoukro', 'Zagreb', 'Havana', 'Nicosia', 'Prague', 'Copenhagen',
'Djibouti', 'Roseau', 'Santo Domingo', 'Dili', 'Quito', 'Cairo', 'San Salvador', 'London', 'Malabo', 'Asmara',
'Tallinn', 'Mbabana', 'Addis Ababa', 'Palikir', 'Suva', 'Helsinki', 'Paris', 'Libreville', 'Banjul', 'Tbilisi',
'Berlin', 'Accra', 'Athens', "Saint George's", 'Guatemala City', 'Conakry', 'Bissau', 'Georgetown',
'Port au Prince',
'Tegucigalpa', 'Budapest', 'Reykjavik', 'New Delhi', 'Jakarta', 'Tehran', 'Baghdad', 'Dublin', 'Jerusalem', 'Rome',
'Kingston', 'Tokyo', 'Amman', 'Nur-Sultan', 'Nairobi', 'Tarawa Atoll', 'Pristina', 'Kuwait City', 'Bishkek',
'Vientiane', 'Riga', 'Beirut', 'Maseru', 'Monrovia', 'Tripoli', 'Vaduz', 'Vilnius', 'Luxembourg', 'Antananarivo',
'Lilongwe', 'Kuala Lumpur', 'Male', 'Bamako', 'Valletta', 'Majuro', 'Nouakchott', 'Port Louis', 'Mexico City',
'Chisinau', 'Monaco', 'Ulaanbaatar', 'Podgorica', 'Rabat', 'Maputo', 'Nay Pyi Taw', 'Windhoek',
'No official capital',
'Kathmandu', 'Amsterdam', 'Wellington', 'Managua', 'Niamey', 'Abuja', 'Pyongyang', 'Skopje', 'Belfast', 'Oslo',
'Muscat', 'Islamabad', 'Melekeok', 'Panama City', 'Port Moresby', 'Asuncion', 'Lima', 'Manila', 'Warsaw', 'Lisbon',
'Doha', 'Bucharest', 'Moscow', 'Kigali', 'Basseterre', 'Castries', 'Kingstown', 'Apia', 'San Marino', 'Sao Tome',
'Riyadh', 'Edinburgh', 'Dakar', 'Belgrade', 'Victoria', 'Freetown', 'Singapore', 'Bratislava', 'Ljubljana',
'Honiara',
'Mogadishu', 'Pretoria, Bloemfontein, Cape Town', 'Seoul', 'Juba', 'Madrid', 'Colombo', 'Khartoum', 'Paramaribo',
'Stockholm', 'Bern', 'Damascus', 'Taipei', 'Dushanbe', 'Dodoma', 'Bangkok', 'Lome', "Nuku'alofa", 'Port of Spain',
'Tunis', 'Ankara', 'Ashgabat', 'Funafuti', 'Kampala', 'Kiev', 'Abu Dhabi', 'London', 'Washington D.C.',
'Montevideo',
'Tashkent', 'Port Vila', 'Vatican City', 'Caracas', 'Hanoi', 'Cardiff', "Sana'a", 'Lusaka', 'Harare')

I am stuck on getting it too skip the second repeated letter.

Thanks for the help!

Upvotes: 0

Views: 288

Answers (1)

Prune
Prune

Reputation: 77880

You need to walk through this logic, perhaps using pencil & paper:

            capital_city[i] = capital_city[i].lower()
            capital_city[i-1] = (capital_city[i-1].upper())
            capital_city[i+1] = (capital_city[i+1].upper())

Note the trailing upper call in this trio: you capitalize position i-1, even if you've already determined that it must be lower case in the previous iteration.

Instead, just check to see whether it's the target letter:

            capital_city[i] = capital_city[i].lower()
            if i > 0 and capital_city[i-1] != letter:
                capital_city[i-1] = (capital_city[i-1].upper())
            if if i < len(capital_city)-1 and capital_city[i+1] != letter:
                capital_city[i+1] = (capital_city[i+1].upper())

Upvotes: 2

Related Questions