From 0d7ed148db5e8fa7558ee09abf1c85c6f21e45ef Mon Sep 17 00:00:00 2001 From: SwayamSahu <91021799+SwayamSahu@users.noreply.github.com> Date: Fri, 28 Oct 2022 00:20:23 +0530 Subject: [PATCH 1/3] Refactoring the syntax using list comprehension --- strings/detecting_english_programmatically.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/strings/detecting_english_programmatically.py b/strings/detecting_english_programmatically.py index aa18db21027a..a59a9b205ccb 100644 --- a/strings/detecting_english_programmatically.py +++ b/strings/detecting_english_programmatically.py @@ -33,10 +33,7 @@ def get_english_count(message: str) -> float: def remove_non_letters(message: str) -> str: - letters_only = [] - for symbol in message: - if symbol in LETTERS_AND_SPACE: - letters_only.append(symbol) + letters_only = [symbol for symbol in message if symbol in LETTERS_AND_SPACE] return "".join(letters_only) From a10eaf0ea017f5b9b57dba087c4c824c9913e116 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 27 Oct 2022 22:50:04 +0200 Subject: [PATCH 2/3] Update detecting_english_programmatically.py --- strings/detecting_english_programmatically.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/strings/detecting_english_programmatically.py b/strings/detecting_english_programmatically.py index a59a9b205ccb..94f0b887ba13 100644 --- a/strings/detecting_english_programmatically.py +++ b/strings/detecting_english_programmatically.py @@ -1,7 +1,7 @@ import os +from string import ascii_letters -UPPERLETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" -LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + " \t\n" +LETTERS_AND_SPACE = ascii_letters + " \t\n" def load_dictionary() -> dict[str, None]: @@ -20,21 +20,12 @@ def get_english_count(message: str) -> float: message = message.upper() message = remove_non_letters(message) possible_words = message.split() - - if possible_words == []: - return 0.0 - - matches = 0 - for word in possible_words: - if word in ENGLISH_WORDS: - matches += 1 - + matches = len(word for word in possible_words if word in ENGLISH_WORDS) return float(matches) / len(possible_words) def remove_non_letters(message: str) -> str: - letters_only = [symbol for symbol in message if symbol in LETTERS_AND_SPACE] - return "".join(letters_only) + return "".join(symbol for symbol in message if symbol in LETTERS_AND_SPACE) def is_english( From ca6793cb9f4e50281ba1d7f49e72ddc298792987 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 27 Oct 2022 22:53:08 +0200 Subject: [PATCH 3/3] Update detecting_english_programmatically.py --- strings/detecting_english_programmatically.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/detecting_english_programmatically.py b/strings/detecting_english_programmatically.py index 94f0b887ba13..b9000101beb4 100644 --- a/strings/detecting_english_programmatically.py +++ b/strings/detecting_english_programmatically.py @@ -20,7 +20,7 @@ def get_english_count(message: str) -> float: message = message.upper() message = remove_non_letters(message) possible_words = message.split() - matches = len(word for word in possible_words if word in ENGLISH_WORDS) + matches = len([word for word in possible_words if word in ENGLISH_WORDS]) return float(matches) / len(possible_words)