Capitalize a character Before and After Nth character in a string in a Python list

596 Views Asked by At

Here is my code I am trying uppercase letters before and after a specific letter in a list. Uppercase any letter before and after uppercase the previous and next letters that come before and after each "z" in a capital city. All other letters are lowercase. All cities that contain that letter will be stored in a list and returned. If I could get some input that would be great. Also if I need to change the code completely please let me know of other ways. I am new to this any input would be appreciated. Thanks

lst = ['brazzaville', 'zagreb', 'vaduz']

lst2 = []
for wrd in lst:
    newwrd = ''
    for ltr in wrd:
        if ltr in 'ua':
            newwrd += ltr.capitalize()
        else:
            newwrd += ltr

    lst2.append(newwrd)
print(lst2)

I keep getting this:

['brAzzAville', 'zAgreb', 'vAdUz']

But I need this:

['brAzzAville', 'zAgreb', 'vadUz']
3

There are 3 best solutions below

2
pakpe On BEST ANSWER

The following strategy consists of iterating through the word and replacing the letters at index-1 and index+1 of z (if they exist) with upper case letters:

lst2 = []
for wrd in lst:
    wrd = wrd.lower()
    for idx, letter in enumerate(wrd):
        if letter == 'z':
            if idx-1 > 0 and wrd[idx - 1] != 'z':
                wrd = wrd.replace(wrd[idx - 1], wrd[idx - 1].upper())
            if idx+1 < len(wrd) and wrd[idx + 1] != 'z':
                wrd = wrd.replace(wrd[idx + 1], wrd[idx + 1].upper())
    if "z" in wrd:
        lst2.append(wrd)

print(lst2)
#['brAzzAville', 'zAgreb', 'vadUz']
0
Karthik S V On

I think this code gives correct answer , verify once

def findOccurrences(s, ch):
    return [i for i, letter in enumerate(s) if letter == ch]


lst = ['brazzaville', 'zagreb', 'vaduz']

lst2 = []
result = []
for wrd in lst:
    newwrd = ''
    result = findOccurrences(wrd, 'z')
    for i in range(len(wrd)):
        if (i + 1 in result or i - 1 in result) and wrd[i] != 'z':
            newwrd += wrd[i].capitalize()
        else:
            newwrd += wrd[i]

    lst2.append(newwrd)
print(lst2)
0
Aditya Patnaik On

Capitalize Nth character in a string

res = lambda test_str,N: test_str[:N] + test_str[N].upper() + test_str[N + 1:] if test_str else ''

Pseudocode

  1. Loop through the list and filter the list for strings that contain 'z'.
[check(i) for i in lst if 'z' in i]
  • For each item in the list:
  1. find the index and capitalize the preceding character to the first occurence of 'z' without rotation.
preind = list(i).index('z')-1 if list(i).index('z')-1>0 else None
k =  res(stri,preind) if(preind) else i
  1. find the index and capitalize the succeeding character to the last occurence of 'z' without rotation.
postind = i.rfind('z')+1 if i.rfind('z')+1<len(i) else None
stri =  res(i,preind) if(preind) else stri

Code

lst = ['brazzaville', 'zagreb', 'vaduz']

def check(i):
    stri = ""
    k = ""
    i = i.lower()
    
    # lambda expression to capitalise Nth character in a string 
    res = lambda test_str,N: test_str[:N] + test_str[N].upper() + test_str[N + 1:] if test_str else ''
    
    # find index of the preceeding character to 'z'
    preind = list(i).index('z')-1 if list(i).index('z')-1>0 else None
    
    # find index of the succeeding character to 'z'
    postind = i.rfind('z')+1 if i.rfind('z')+1<len(i) else None
    
    # capitalise preceeding character to 'z'
    stri =  res(i,preind) if(preind) else i
    # capitalise succeeding character to 'z'
    k = res(stri,postind) if(postind) else stri

    # return the processed string 
    return k
    
    

print([check(i) for i in lst if 'z' in i ])
#output
['brAzzAville', 'zAgreb', 'vadUz']