Python
stringOperation
listOperation
#migrated
CheckIO

CheckIO (Python) > House Password > 実装してみた > PEP8: do not assign a lambda expression, use a def

https://py.checkio.org/mission/house-password/
詳細はリンク先を参照 (手抜き)。

Python > 実装 > 数値が入っているか | alphabet(upper and lower case)が入っているか > sum(), map(), str.isalphaの使用にて@SaitoTsutomu さんに教えていただいた方法を使用しました。

v0.1

PEP8準拠してません。

numlower = lambda s:sum(map(str.islower, s))
numupper = lambda s:sum(map(str.isupper, s))
numdigit = lambda s:sum(map(str.isdigit, s))

def checkLowerUpperDigit(data):
    return (numlower(data) >0 and 
       numupper(data) >0 and
       numdigit(data) >0)

def checkio(data):
    print(data)
    if len(data) < 10:
        return False
    if checkLowerUpperDigit(data) == False:
        return False

    return True

#Some hints
#Just check all conditions


if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio('A1213pokl') == False, "1st example"
    assert checkio('bAse730onE4') == True, "2nd example"
    assert checkio('asasasasasasasaas') == False, "3rd example"
    assert checkio('QWERTYqwerty') == False, "4th example"
    assert checkio('123456123456') == False, "5th example"
    assert checkio('QwErTy911poqqqq') == True, "6th example"

run
A1213pokl
bAse730onE4
asasasasasasasaas
QWERTYqwerty
123456123456
QwErTy911poqqqq

全assertionをパスした。

v0.2

PEP8のWarning対応

http://pep8online.com を使用。

以下のWarningは残っている。

Line1, Column 1: do not assign a lambda expression, use a def
Line2, Column 1: do not assign a lambda expression, use a def
Line3, Column 1: do not assign a lambda expression, use a def

numlower = lambda s: sum(map(str.islower, s))
numupper = lambda s: sum(map(str.isupper, s))
numdigit = lambda s: sum(map(str.isdigit, s))


def checkLowerUpperDigit(data):
    return (numlower(data) > 0 and
            numupper(data) > 0 and
            numdigit(data) > 0)


def checkio(data):
    print(data)
    if len(data) < 10:
        return False
    if checkLowerUpperDigit(data) is False:
        return False

    return True

# Some hints
# Just check all conditions


if __name__ == '__main__':
    # These "asserts" using only for self-checking and
    #   not necessary for auto-testing
    assert checkio('A1213pokl') is False, "1st example"
    assert checkio('bAse730onE4') is True, "2nd example"
    assert checkio('asasasasasasasaas') is False, "3rd example"
    assert checkio('QWERTYqwerty') is False, "4th example"
    assert checkio('123456123456') is False, "5th example"
    assert checkio('QwErTy911poqqqq') is True, "6th example"

link

https://stackoverflow.com/questions/25010167/e731-do-not-assign-a-lambda-expression-use-a-def

In my opinion this rule better be avoided and broken when it makes sense, use your judgement.