Skip to content

Commit de3271e

Browse files
SwayamSahucclauss
andauthoredOct 27, 2022
Refactoring the syntax using list comprehension (TheAlgorithms#7749)
* Refactoring the syntax using list comprehension * Update detecting_english_programmatically.py * Update detecting_english_programmatically.py Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 61eedc1 commit de3271e

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed
 

‎strings/detecting_english_programmatically.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
2+
from string import ascii_letters
23

3-
UPPERLETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
4-
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + " \t\n"
4+
LETTERS_AND_SPACE = ascii_letters + " \t\n"
55

66

77
def load_dictionary() -> dict[str, None]:
@@ -20,24 +20,12 @@ def get_english_count(message: str) -> float:
2020
message = message.upper()
2121
message = remove_non_letters(message)
2222
possible_words = message.split()
23-
24-
if possible_words == []:
25-
return 0.0
26-
27-
matches = 0
28-
for word in possible_words:
29-
if word in ENGLISH_WORDS:
30-
matches += 1
31-
23+
matches = len([word for word in possible_words if word in ENGLISH_WORDS])
3224
return float(matches) / len(possible_words)
3325

3426

3527
def remove_non_letters(message: str) -> str:
36-
letters_only = []
37-
for symbol in message:
38-
if symbol in LETTERS_AND_SPACE:
39-
letters_only.append(symbol)
40-
return "".join(letters_only)
28+
return "".join(symbol for symbol in message if symbol in LETTERS_AND_SPACE)
4129

4230

4331
def is_english(

0 commit comments

Comments
 (0)
Please sign in to comment.