-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathPython.py
40 lines (31 loc) · 1.12 KB
/
Python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#****************************************#
#* *#
#* CodinGame.com Solutions by pathosDev *#
#* *#
#* Puzzle: Text formatting *#
#* Difficulty: Easy *#
#* Date solved: 13.11.2018 *#
#* *#
#****************************************#
import re
#Read input.
text = input().lower().strip()
#Remove excessive spaces.
text = re.sub(r'\s{2,}', ' ', text)
#Remove spaces before and after punctuations.
text = re.sub(r'\s?[^\s\w\d]\s?', lambda match: match.group().strip(), text)
#Remove repeated punctuations.
text = re.sub(r'[^\s\w\d]+', lambda match: match.group().strip()[0], text)
#Converts any string to pascal case.
def stringToPascalCase(s):
if not s:
return ''
chars = list(s)
chars[0] = chars[0].upper()
return ''.join(chars)
#First letter uppercase.
text = '.'.join(stringToPascalCase(s) for s in text.split('.'))
#Add spaces after punctuations.
text = re.sub(r'[^\s\w\d]', lambda match: match.group() + ' ', text)
#Output formatted text.
print(text.strip())