tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
1
Task/Reverse-a-string/Python/reverse-a-string-1.py
Normal file
1
Task/Reverse-a-string/Python/reverse-a-string-1.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
raw_input()[::-1]
|
||||
1
Task/Reverse-a-string/Python/reverse-a-string-2.py
Normal file
1
Task/Reverse-a-string/Python/reverse-a-string-2.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
string[::-1]
|
||||
36
Task/Reverse-a-string/Python/reverse-a-string-3.py
Normal file
36
Task/Reverse-a-string/Python/reverse-a-string-3.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
'''
|
||||
Reverse a Unicode string with proper handling of combining characters
|
||||
'''
|
||||
|
||||
import unicodedata
|
||||
|
||||
def ureverse(ustring):
|
||||
'''
|
||||
Reverse a string including unicode combining characters
|
||||
|
||||
Example:
|
||||
>>> ucode = ''.join( chr(int(n, 16))
|
||||
for n in ['61', '73', '20dd', '64', '66', '305'] )
|
||||
>>> ucoderev = ureverse(ucode)
|
||||
>>> ['%x' % ord(char) for char in ucoderev]
|
||||
['66', '305', '64', '73', '20dd', '61']
|
||||
>>>
|
||||
'''
|
||||
groupedchars = []
|
||||
uchar = list(ustring)
|
||||
while uchar:
|
||||
if 'COMBINING' in unicodedata.name(uchar[0], ''):
|
||||
groupedchars[-1] += uchar.pop(0)
|
||||
else:
|
||||
groupedchars.append(uchar.pop(0))
|
||||
# Grouped reversal
|
||||
groupedchars = groupedchars[::-1]
|
||||
|
||||
return ''.join(groupedchars)
|
||||
|
||||
if __name__ == '__main__':
|
||||
ucode = ''.join( chr(int(n, 16))
|
||||
for n in ['61', '73', '20dd', '64', '66', '305'] )
|
||||
ucoderev = ureverse(ucode)
|
||||
print (ucode)
|
||||
print (ucoderev)
|
||||
Loading…
Add table
Add a link
Reference in a new issue