瀏覽代碼

Day 11

master
Chris Smith 8 年之前
父節點
當前提交
501903e762
共有 1 個檔案被更改,包括 38 行新增0 行删除
  1. 38
    0
      11.py

+ 38
- 0
11.py 查看文件

@@ -0,0 +1,38 @@
1
+#!/usr/bin/python
2
+
3
+from string import ascii_lowercase
4
+from itertools import takewhile
5
+import re
6
+
7
+input = 'cqjxjnds'
8
+
9
+run_regex = '|'.join(ascii_lowercase[i:i+3] for i in range(24))
10
+next_letter = dict((l, chr(ord(l) + 1)) for l in ascii_lowercase)
11
+next_letter['h'] = 'j'
12
+next_letter['n'] = 'p'
13
+next_letter['k'] = 'm'
14
+next_letter['z'] = 'a'
15
+
16
+
17
+def increment(str):
18
+    tail = sum(1 for _ in takewhile(lambda x: x == 'z', str[::-1]))
19
+    next = list(str)
20
+    if tail < len(next):
21
+        next[-tail - 1] = next_letter[next[-tail - 1]]
22
+    if tail > 0:
23
+        next[-tail:] = 'a' * tail
24
+    return ''.join(next)
25
+
26
+
27
+def matches(str):
28
+    has_run = re.search(run_regex, str)
29
+    has_bad_letters = re.search('[iol]', str)
30
+    has_pairs = re.search(r'(.)(\1).*?(?!\1)(.)(\3)', str)
31
+    return has_run and has_pairs and not has_bad_letters
32
+
33
+
34
+for _ in range(2):
35
+    input = increment(input)
36
+    while not matches(input):
37
+        input = increment(input)
38
+    print(input)

Loading…
取消
儲存