-
-
Save dketov/5633052fe8478207c895eda26dc14262 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
"""В системе авторизации есть ограничение: логин должен начинаться с | |
латинской буквы, состоять из латинских букв, цифр, точки и минуса, но | |
заканчиваться только латинской буквой или цифрой; минимальная длина | |
логина — один символ, максимальная — 20. Напишите код, проверяющий | |
соответствие входной строки этому правилу. Придумайте несколько | |
способов решения задачи и сравните их.""" | |
def check1(login): | |
if not 1 <= len(login) <= 20: | |
return False | |
if not login[0].isalpha(): | |
return False | |
for char in login[1:]: | |
if not char.isalnum() and char not in "-.": | |
return False | |
return True | |
from itertools import imap | |
def check2(login): | |
if not 1 <= len(login) <= 20: | |
return False | |
if not login[0].isalpha(): | |
return False | |
return all( | |
imap( | |
lambda char: char.isalnum() or char in "-.", | |
login[1:] | |
) | |
) | |
from string import digits, letters | |
allowedchars = set(digits + letters + "-.") | |
def check3(login): | |
return ( | |
1 <= len(login) <= 20 and | |
login[0].isalpha() and | |
not (set(login) - allowedchars) | |
) | |
if __name__ == "__main__": | |
import sys | |
me = sys.modules[__name__] | |
checkers = tuple( | |
getattr(me, name) | |
for name in dir(me) | |
if name.startswith("check") | |
) | |
logins = ["", "123456789012345678901", "1", "a", "A", "Ы", "-", ".", | |
"login", "login1234567890", "login.1234567890", "login-1234567890", | |
"login@1234567890", "логин" ] | |
def get_timings(f, N): | |
from time import time | |
start = time() | |
for i in xrange(N): | |
imap(f, logins) | |
return time() - start | |
print "%22s " % "login", "\t".join("%8s" % c.__name__ for c in checkers) | |
for l in logins: | |
print "[%20s] " % l, "\t".join("%8s" % c(l) for c in checkers) | |
print "%22s " % "T =" , "\t".join("%8f" % get_timings(c, 1000000) for c in checkers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment