def is_acceptable_password(password: str) -> bool:
    # TODO: Split into smaller funcs
    """
    Checks if given password string meets required conditions:
        - length > 6;
        - contains at least one digit, but cannot consist of just digits (except condition 3);
        - if password length > 9 then second condition is skipped;
        - should not contain the word "password" in any case;
        - should contain 3 different letters or digits regardless of length
    :param password: Password string to analyze
    :return: True if password is acceptable, False otherwise
    """
    check_type(str, password)

    blacklist_word = "password"
    if len(password) <= 6:
        return False
    if blacklist_word in password.lower():
        return False

    full_alphabet = string.ascii_lowercase + string.digits
    if len(set(password.lower()) & set(full_alphabet)) < 3:
        return False

    if len(password) > 9:
        return True

    if password.isdigit():
        return False

    if not set(string.digits) & set(password):
        return False

    return True
Exemple #2
0
def is_all_upper(text: str) -> bool:
    """
    Checks if all characters in text are uppercase
    :param text: A string to analyse
    :return: True if all characters are uppercase, False otherwise
    """
    check_type(str, text)
    return text.isupper()
Exemple #3
0
def all_the_same(items: list) -> bool:
    """
    Checks if all items in list are same
    :param items: a list of items to compare
    :return: True if all items are same, False if not
    """
    check_type(list, items)
    items_set = set(items)
    return True if len(items_set) <= 1 else False
def validate_iterable(items: Iterable):
    """
    Validates iterable for is_ascending func
    :param items: Iterable of items
    :return: None if all items are valid, raises Assertion
    """
    if not isinstance(items, Iterable):
        raise TypeError("Given object is not Iterable")
    for item in items:
        check_type(int, item)
def validate_files_list(files: list) -> None:
    """
    Checks that all items in files list belong to 'str' type
    :param files: A list of files to validate
    :return: "Ok!" if list is correct, otherwise raises TypeError
    """
    check_type(list, files)
    if not files:
        raise TypeError("List is empty")
    for item in files:
        check_type(str, item)
    return
def sun_angle(time: str) -> typing.Union[int, float, str]:
    """
    Calculates the sun angle using a time string
    :param time: a string of time in format HH:MM (ex: 12:00)
    :return: Sun angle if time of the day, otherwise "I don't see the sun!"
    """
    check_type(str, time)
    check_time_format(time)

    hour, minute = list(map(int, time.split(':')))
    check_time_values(hour, minute)

    angle_of_the_sun = 15 * hour + minute / 4 - 90
    return angle_of_the_sun if 0 <= angle_of_the_sun <= 180 else "I don't see the sun!"
def are_isometric(first_string: str, second_string: str) -> bool:
    """
    Checks whether two given strings are isometric
    :param first_string: First string to compare
    :param second_string: Second string to compare
    :return: True if strings are isometric, False if strings are not isometric or have different length
    """
    check_type(str, first_string)
    check_type(str, second_string)

    if len(first_string) != len(second_string):
        return False

    letters = [letter for letter, _ in set(zip(first_string, second_string))]
    return len(letters) == len(set(letters))
Exemple #8
0
def is_acceptable_password(password: str) -> bool:
    """
    Сhecks if given password length is >6 and it contains at least one digit
    :param password: Password string to analyze
    :return: True if password is acceptable, False otherwise
    """
    check_type(str, password)

    if len(password) <= 6:
        return False

    if not set(string.digits) & set(password):
        return False

    return True
Exemple #9
0
def multiply_digits(number: int) -> int:
    """
    Calculates the product of all digits in given number
    :param number: An int number
    :return: int product of digits in number, excluding zeroes
    """
    check_type(int, number)
    assert 0 < number < 1000000

    digits = [int(digit) for digit in str(number) if int(digit)]
    product = 1

    for digit in digits:
        product *= digit

    return product
Exemple #10
0
def words_order(text: str, words: list) -> bool:
    """
    Checks if words in list appear in same order as in text
    :param text: str text to analyze
    :param words: list of words to search in text
    :return: True if order same or len(words) == 1, False otherwise
    """
    check_type(str, text)
    check_type(list, words)

    if not text or not words:
        return False

    words_from_text = [word for word in text.split() if word in words]
    unique_words_from_text = list(dict.fromkeys(words_from_text))
    result = unique_words_from_text == words
    return result
def morse_decoder(code: str) -> str:
    """
    Decodes given morse code
    :param code: a string of morse code
    :return: decoded string
    """
    check_type(str, code)

    encoded_words = code.split("   ")
    decoded_words = []

    for encoded_word in encoded_words:
        decoded_word = get_decoded_word(encoded_word)
        decoded_words.append(decoded_word)
    decoded_string = " ".join(decoded_words)

    decoded_string = decoded_string.capitalize()

    return decoded_string
Exemple #12
0
def is_acceptable_password(password: str) -> bool:
    """
    Сhecks if given password length is >6 and it contains at least one digit
    :param password: Password string to analyze
    :return: True if password is acceptable, False otherwise
    """
    check_type(str, password)

    length_is_acceptable = False
    has_digit = False

    if len(password) > 6:
        length_is_acceptable = True

    for character in password:
        if character.isdigit():
            has_digit = True

    return length_is_acceptable and has_digit
def validate_tree(tree: List[List[str]]) -> None:
    """
    Validates type of given list of tree
    :param tree: List of lists of strings, representing fathers and sons
    :return: None if list is valid, raises TypeError otherwise
    """
    check_contents_of_tree(tree)
    check_type(list, tree)
    for pair in tree:
        check_type(list, pair)
        for name in pair:
            check_type(str, name)
def test_list_and_dict():
    with pytest.raises(TypeError):
        check_type(list, {"key": "value"})
def test_two_ints():
    assert check_type(int, 1) is None
def test_integer_and_float():
    with pytest.raises(TypeError):
        check_type(int, 3.2)
def test_int_and_rounded_float():
    assert check_type(int, round(1.3)) is None
def test_integer_and_string():
    with pytest.raises(TypeError):
        check_type(int, "d")
def test_no_arguments():
    with pytest.raises(TypeError):
        check_type()
def test_one_argument():
    with pytest.raises(TypeError):
        check_type(1)
def test_string_hypothesis(s):
    assert check_type(str, s) is None