diff --git a/homework/tests.py b/homework/tests.py index d9a1d5f..5eb97ba 100644 --- a/homework/tests.py +++ b/homework/tests.py @@ -1,4 +1,5 @@ import datetime +import re def test_leap_year(): @@ -12,7 +13,11 @@ def test_leap_year(): """ def is_leap_year(date): - pass + print(date) + if date.year % 4 != 0 or (date.year % 100 == 0 and date.year % 400 != 0): + return False + else: + return True assert is_leap_year(datetime.date(year=2000, month=5, day=13)) assert is_leap_year(datetime.date(year=2016, month=11, day=1)) @@ -33,13 +38,18 @@ def test_file_data(): """ def count_word_in_file(filename, word): - pass + with open(filename) as f: + count = f.read() \ + .lower() \ + .split() \ + .count(word.lower()) + return count assert count_word_in_file("homework/pony.txt", "радуга") == 0 assert count_word_in_file("homework/pony.txt", "и") == 3 assert count_word_in_file("homework/pony.txt", "пони") == 5 - + def test_text_stat_hard(): """ Задание: необходимо реализовать функцию, которая принимает filename - имя текстового файла @@ -49,8 +59,16 @@ def test_text_stat_hard(): """ def text_stat(filename): - pass + with open(filename) as f: + text = f.read() + rows = sum(1 for line in text.split('\n')) + words = len(re.findall(r'\w+', text)) + symbols = sum(1 for line in text) + stat = (rows, words, symbols) + return stat + + # в первых двух файлах по 7 строк, а в конце третьего файла есть пустая строка, которую мы тоже должны посчитать. + assert text_stat("homework/textstat1.txt") == (7, 69, 445) + assert text_stat("homework/textstat2.txt") == (7, 73, 459) + assert text_stat("homework/textstat3.txt") == (11, 73, 467) - assert text_stat("homework/textstat1.txt") == (6, 69, 445) - assert text_stat("homework/textstat2.txt") == (6, 73, 459) - assert text_stat("homework/textstat3.txt") == (10, 73, 467) diff --git a/homework/tests2.py b/homework/tests2.py new file mode 100644 index 0000000..e69de29