From 0d6659dc9bbf17281c32ba2fe6ec43258092f479 Mon Sep 17 00:00:00 2001 From: Anton Peretrukhin Date: Tue, 26 Jun 2018 09:15:24 +0500 Subject: [PATCH 1/3] HomeWork, lesson 2 --- homework/tests.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/homework/tests.py b/homework/tests.py index d4926c7..41ee5e3 100644 --- a/homework/tests.py +++ b/homework/tests.py @@ -12,7 +12,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,8 +37,14 @@ def test_file_data(): """ def count_word_in_file(filename, word): - pass + with open(filename) as f: + count = f.read() \ + .lower() \ + .split() \ + .count(word) + 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 + From 0e5e24192786d5d03be9d9b9a32348ecc8141d55 Mon Sep 17 00:00:00 2001 From: Anton Peretrukhin Date: Tue, 26 Jun 2018 09:17:23 +0500 Subject: [PATCH 2/3] HomeWork, lesson 2 --- homework/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/tests.py b/homework/tests.py index 41ee5e3..7c56357 100644 --- a/homework/tests.py +++ b/homework/tests.py @@ -41,7 +41,7 @@ def count_word_in_file(filename, word): count = f.read() \ .lower() \ .split() \ - .count(word) + .count(word.lower()) return count assert count_word_in_file("homework/pony.txt", "радуга") == 0 From 45eee41aef5c5c03cb5e89de7dfb4a8e0b43dec8 Mon Sep 17 00:00:00 2001 From: Anton Peretrukhin Date: Sun, 8 Jul 2018 11:39:17 +0500 Subject: [PATCH 3/3] HM-2: aperetrukhin --- homework/tests.py | 22 ++++++++++++++++++++++ homework/tests2.py | 0 homework/textstat1.txt | 7 +++++++ homework/textstat2.txt | 7 +++++++ homework/textstat3.txt | 10 ++++++++++ 5 files changed, 46 insertions(+) create mode 100644 homework/tests2.py create mode 100644 homework/textstat1.txt create mode 100644 homework/textstat2.txt create mode 100644 homework/textstat3.txt diff --git a/homework/tests.py b/homework/tests.py index 7c56357..cf7bb88 100644 --- a/homework/tests.py +++ b/homework/tests.py @@ -1,4 +1,5 @@ import datetime +import re def test_leap_year(): @@ -48,3 +49,24 @@ def count_word_in_file(filename, word): assert count_word_in_file("homework/pony.txt", "и") == 3 assert count_word_in_file("homework/pony.txt", "пони") == 5 +def test_text_stat_hard(): + """ + Задание: необходимо реализовать функцию, которая принимает filename - имя текстового файла + и возвращает кортедж: (количество строк, количество слов, количество символов) в тексте. + Текст может содержать любые символы латинского алфавита, цифры, пробелы и знаки пунктуации. + Словом считается число-буквенная последовательность (например elephant, el3phant или 449). + """ + + def text_stat(filename): + 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) diff --git a/homework/tests2.py b/homework/tests2.py new file mode 100644 index 0000000..e69de29 diff --git a/homework/textstat1.txt b/homework/textstat1.txt new file mode 100644 index 0000000..82807a5 --- /dev/null +++ b/homework/textstat1.txt @@ -0,0 +1,7 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit, +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris +nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in +reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla +pariatur. Excepteur sint occaecat cupidatat non proident, sunt in +culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/homework/textstat2.txt b/homework/textstat2.txt new file mode 100644 index 0000000..d08533a --- /dev/null +++ b/homework/textstat2.txt @@ -0,0 +1,7 @@ +Lo2rem ipsum dolor sit amet, consectetur adipiscing elit, +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut 44 enim ad minim veniam, quis nostrud exercitation ullamco laboris +nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in +repreh55enderit in voluptate velit esse cillum dolore eu fugiat nulla +pariatur. Excepteur sint occaecat cupidatat non proident, sunt in +culpa 6qui6 7 7 7 officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/homework/textstat3.txt b/homework/textstat3.txt new file mode 100644 index 0000000..2042453 --- /dev/null +++ b/homework/textstat3.txt @@ -0,0 +1,10 @@ + +Lo2rem.ipsum;dolor sit amet, consectetur adipiscing elit, + +sed do "eiusmod tempor" incididunt ut labore et dolore magna aliqua. +Ut 44 enim ad minim veniam, quis nostrud exercitation ullamco laboris + +nisi ut aliquip ex 'ea' commodo consequat. Duis aute irure dolor in +repreh55enderit in voluptate velit esse cillum dolore eu fugiat nulla +pariatur. Excepteur sint-occaecat cupidatat non proident, sunt in +culpa 6qui6 7 7 7 officia deserunt mollit anim id est laborum.