forked from learnpythonru/basic_exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_challenges.py
More file actions
65 lines (43 loc) · 1.69 KB
/
string_challenges.py
File metadata and controls
65 lines (43 loc) · 1.69 KB
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
64
65
def get_last_letter(word):
return word[-1]
# Вывести последнюю букву в слове
word = "Архангельск"
print(get_last_letter(word))
def get_count_letter(word, letter="а"):
return word.count(letter)
# Вывести количество букв "а" в слове
word = "Архангельск"
print(get_count_letter(word))
def get_count_vowel(word):
count_vowel = 0
vowels = ["а", "е", "ё", "ы", "я", "у", "и", "о", "э", "ю"]
for vowel in vowels:
count_vowel += get_count_letter(word, vowel)
return count_vowel
# Вывести количество гласных букв в слове
word = "Архангельск"
print(get_count_vowel(word.lower()))
def get_words_sentence(sentence):
return sentence.split()
# Вывести количество слов в предложении
sentence = "Мы приехали в гости"
print(len(get_words_sentence(sentence)))
def get_first_letters(sentence):
text = ""
for word in get_words_sentence(sentence):
text += word[0] + "\n"
return text
# Вывести первую букву каждого слова на отдельной строке
sentence = "Мы приехали в гости"
print(get_first_letters(sentence))
def get_middle_len_word(sentence):
middle_len = 0
count_len = 0
list_words = get_words_sentence(sentence)
for word in list_words:
count_len += len(word)
middle_len = count_len / len(list_words)
return str(middle_len)
# Вывести усреднённую длину слова в предложении
sentence = "Мы приехали в гости"
print(get_middle_len_word(sentence))