58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import re
|
||
import sys
|
||
|
||
delimiters = ["'", '"', '’', '-', ':', "|", ".", ",", " "]
|
||
def split_with_multiple_delimiters(text):
|
||
list = []
|
||
previousI = 0
|
||
for i, char in enumerate(text):
|
||
if char in delimiters:
|
||
list.append(text[previousI:i])
|
||
previousI = i
|
||
if i == len(text)-1:
|
||
list.append(text[previousI:i+1])
|
||
return list
|
||
|
||
|
||
def convert_case(original_line):
|
||
words = original_line.split()
|
||
changed_line = r''
|
||
for word in words:
|
||
word = word.strip()
|
||
split_word = split_with_multiple_delimiters(word)
|
||
for s_word in split_word:
|
||
converted_word = r''
|
||
lower_set = False
|
||
upper_set = False
|
||
for char in s_word:
|
||
if re.match(r'[a-ząćęłńóśźż]', char):
|
||
lower_set = True
|
||
elif re.match(r'[A-ZĄĆĘŁŃÓŚŹŻ]', char):
|
||
upper_set = True
|
||
else:
|
||
continue
|
||
if upper_set and lower_set:
|
||
for char in s_word:
|
||
if char.islower():
|
||
if re.match(r'[a-ząćęłńóśźż]', char):
|
||
converted_word += char.upper()
|
||
else:
|
||
converted_word += char
|
||
elif char.isupper():
|
||
if re.match(r'[A-ZĄĆĘŁŃÓŚŹŻ]', char):
|
||
converted_word += char.lower()
|
||
else:
|
||
converted_word += char
|
||
else:
|
||
converted_word += char
|
||
changed_line += converted_word
|
||
else:
|
||
changed_line += s_word
|
||
changed_line += ' '
|
||
return changed_line
|
||
|
||
|
||
for line in sys.stdin:
|
||
line = line.strip('\n')
|
||
converted_line = convert_case(line).strip()
|
||
print(converted_line) |