I'm trying to write code that will remove dashes from a string if it's in the middle of a word and not between them. If the dash comes before or after a line break, this will also remove the line break. According to the rules of this assignment, a dash that needs to be removed will always border a line break, a space, or another dash. If the character to the left of the dash is a letter and the character to the right is a letter, it should not be removed.
def remove_dashes(string):
lst = list(string)
for i in range(len(lst)-1):
if lst[i] == '-' and lst[i+1] == (' ' or '-' or '\n') or lst[i-1] == (' ' or '-' or '\n'):
lst[i] = ''
if lst[i+1] == '\n':
lst[i+1] = ''
elif lst[i-1] == '\n':
lst[i-1]
elif lst[i] == '-' and i == len(lst)-1 and lst[i-1] == (' ' or '-' or '\n'):
lst[i] = ''
if lst[i-1] == '\n':
lst[i-1] = ''
return "".join(lst)
So in theory "rem-\nove the-\nse da\n-shes--" would return as "remove these dashes" without any line breaks. But "not-these-dashes" would just return as "not-these-dashes". However, my code is not working. Can anyone help?