-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.py
63 lines (53 loc) · 2.02 KB
/
word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re
class Word:
def __init__(self, word=''):
self.word = word
self.length = self.get_length()
self.unique_letters = self.get_unique_letters()
self.vowels_count = self.get_vowels_count()
self.consonants_count = self.get_consonants_count()
self.most_frequent_letters, self.most_frequent_letters_count = self.get_most_frequent_letters()
def validate(self) -> bool:
if re.search('^[a-zA-Z]+$', self.word):
return True
else:
return False
def get_length(self) -> int:
return len(self.word)
def get_unique_letters(self) -> dict:
letters = {}
for character in self.word.lower():
if character in letters:
letters[character] = letters[character] + 1
else:
letters[character] = 1
return letters
def get_vowels_count(self) -> int:
return len(self.get_vowels())
def get_consonants_count(self) -> int:
return len(self.get_consonants())
def get_vowels(self) -> str:
vowel_chars = ['a', 'e', 'i', 'o', 'u']
vowels = ''
for character in self.word.lower():
if character in vowel_chars:
vowels += character
return vowels
def get_consonants(self) -> str:
vowel_chars = ['a', 'e', 'i', 'o', 'u']
consonants = ''
for character in self.word.lower():
if character not in vowel_chars:
consonants += character
return consonants
def get_most_frequent_letters(self) -> tuple:
highest_count = 0
unique_letters = self.get_unique_letters()
most_frequent_letters = []
for key in unique_letters:
if unique_letters[key] > highest_count:
highest_count = unique_letters[key]
most_frequent_letters = [key]
elif unique_letters[key] == highest_count:
most_frequent_letters.append(key)
return most_frequent_letters, highest_count