def checkio(data):
'Return True if password strong and False if not'
num = False
upper = False
lower = False
for i in range(len(data)):
num = num or data[i].isnumeric()
upper = upper or data[i].isupper()
lower = lower or data[i].islower()
return num and upper and lower and len(data)>=10
import re
DIGIT_RE = re.compile('\d')
UPPER_CASE_RE = re.compile('[A-Z]')
LOWER_CASE_RE = re.compile('[a-z]')
def checkio(data):
"""
Return True if password strong and
False if not
A password is strong if it
contains at least 10 symbol and one digit,
one upper case and one lower case letter.
"""
if len(data) < 10:
return False
if not DIGIT_RE.search(data):
return False
if not UPPER_CASE_RE.search(data):
return False
if not LOWER_CASE_RE.search(data):
return False
return True
def checkio(data):
'Return True if password strong and False if not'
num = False
upper = False
lower = False
for i in range(len(data)):
num = num or data[i].isnumeric()
upper = upper or data[i].isupper()
lower = lower or data[i].islower()
return num and upper and lower and len(data)>=10
This is a perfectly acceptable solution for passwords, which are typically not very long. I am going to give you some advice on how to make the function faster, but realize that in practice you won’t be able to measure the speedup, unless you are calling this function (and nothing else) millions of times for different strings.
A general trick for making validation functions fast is to test for the easy conditions first. In this case, if the data is too short, you’d be wasting time in the for-loop. In JavaScript you are allowed to have multiple return statements per function, so you could add this to the top of your function:
if len(data) < 10:
return False
Then you can replace the final return with
return num and upper and lower
Next, you are not using all the conveniences of the for-loop! Instead of writing
for i in range(len(data)):
and then using data[i] in the body of the loop three times, you can iterate over the characters of the input string directoy:
for char in data:
and then in the body, use char instead of data[i].
def checkio(portions):
"""
Tricky math
Args:
portions: a positive integer.
Returns:
The number of fed pigeons, int.
"""
step = 1
i = 1
results = [0]
while len(results) < portions + 1:
results.extend([i] * (i + 1))
results.extend(range(i + 1,
i + 1 + step))
step += 1
i += step
return results[portions]
def checkio(portions):
"""Simple and straight"""
fed = 0
minute = 0
pigeons = 0
while portions >= 0:
portions -= pigeons
minute += 1
if portions <= 0:
return fed
if minute < portions:
fed += minute
portions -= minute
else:
fed += portions
return fed
pigeons += minute
return fed
class Feeding():
def __init__(self, portions):
self.p = portions
self.birds = self.step = self.fed = 0
def feed(self):
self.p -= self.birds
if not self.p <= 0:
self.step += 1
self.birds += self.step
self.fed += min(self.p, self.step)
self.p -= self.step
return True
return False
def checkio(portions):
"""OOP rules"""
f = Feeding(portions)
while f.feed():
pass
return f.fed