-
Notifications
You must be signed in to change notification settings - Fork 0
/
009.py
executable file
·37 lines (28 loc) · 1.11 KB
/
009.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import random
def randrepl(matchobj):
# Strategy:
# 'abcde' -> ['a'], ['b', 'c', 'd'], ['e']
# -> ['a'], ['c', 'd', 'b'], ['e']
# -> ['a', 'c', 'd', 'b', 'e']
# -> 'abdbe'
inner = list(matchobj.group(1))
random.shuffle(inner)
return ''.join(
[matchobj.group(0)[:1]] + inner + [matchobj.group(0)[-1:]])
def main():
"""09. Typoglycemia
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,
それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.
ただし,長さが4以下の単語は並び替えないこととする.
"""
s = ("I couldn't believe that I could actually understand what "
"I was reading : the phenomenal power of the human mind .")
s = re.sub(r"\w([\w']{3,})\w", randrepl, s)
print s
# I cdlou'nt bvieele that I colud alcultay undearsntd what I was readnig :
# the pnomneahel power of the huamn mind .
if __name__ == '__main__':
main()