Skip to content

Refactoring the syntax using list comprehension #7749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions strings/detecting_english_programmatically.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand All @@ -20,24 +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 = []
for symbol in message:
if symbol in LETTERS_AND_SPACE:
letters_only.append(symbol)
return "".join(letters_only)
return "".join(symbol for symbol in message if symbol in LETTERS_AND_SPACE)


def is_english(
Expand Down