-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path040_challenge_1_exercise.py
More file actions
72 lines (59 loc) · 1.69 KB
/
040_challenge_1_exercise.py
File metadata and controls
72 lines (59 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
66
67
68
69
70
71
72
# Video alternative: https://youtu.be/qDWyR0XpJtQ&t=1295s
from lib.helpers import check_that_these_are_equal
# Now it's your turn.
# Note that the exercise will be (a little) less challenging
# than the example.
# Write a function that takes a list of words and returns a
# report of all the words that are longer than 10 characters
# but don't contain a hyphen. If those words are longer than
# 15 characters, then they should be shortened to 15
# characters and have an ellipsis (...) added to the end.
# For example, if the input is:
# [
# 'hello',
# 'nonbiological',
# 'Kay',
# 'this-is-a-long-word',
# 'antidisestablishmentarianism'
# ]
# then the output should be:
# "These words are quite long: nonbiological, antidisestablis..."
# @TASK: Complete this exercise.
print("")
print("Function: report_long_words")
def report_long_words(words):
reported_words = []
for word in words:
if '-' in word: continue
if len(word) > 10 and len(word) <= 15:
reported_words.append(word)
elif len(word) > 15:
reported_words.append(word[0:15]+ '...')
return 'These words are quite long: ' + ', '.join(reported_words)
check_that_these_are_equal(
report_long_words([
'hello',
'nonbiological',
'Kay',
'this-is-a-long-word',
'antidisestablishmentarianism'
]),
"These words are quite long: nonbiological, antidisestablis..."
)
check_that_these_are_equal(
report_long_words([
'cat',
'dog',
'rhinosaurus',
'rhinosaurus-rex',
'frog'
]),
"These words are quite long: rhinosaurus"
)
check_that_these_are_equal(
report_long_words([
'cat'
]),
"These words are quite long: "
)
# Once you're done, move on to 041_challenge_2_example.py