Automate the Boring Stuff with Python:
Practical Programming for Total Beginners
(No Starch Press)
Al Sweigart (著)
開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Automate the Boring Stuff with Python: Practical Programming for Total Beginners (Al Sweigart (著)、No Starch Press)のPart 2.(Automating Tasks)、Chapter 7.(Pattern Matching with Regular Expressions)、Practice Projects(Strong Password Detection)を解いてみる。
Practice Projects(Strong Password Detection)
コード(Emacs)
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import re
def is_string_password(password):
eight = re.compile(r'.{8,}')
uppercase = re.compile(r'[A-Z]')
lowercase = re.compile(r'[a-z]')
digit = re.compile(r'[0-9]')
if eight.match(password) and uppercase.search(password) and \
lowercase.search(password) and digit.search(password):
return True
return False
passwords = ['Aa34567', 'a2345678', 'A2345678', 'Aacdefgh', 'Aa345678',
'Aa345678ij']
for password in passwords:
print('{0}: {1}'.format(password, is_string_password(password)))
入出力結果(Terminal, IPython)
$ ./sample1.py Aa34567: False a2345678: False A2345678: False Aacdefgh: False Aa345678: True Aa345678ij: True $
0 コメント:
コメントを投稿