forked from mikeizbicki/functional-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixme.py
More file actions
98 lines (80 loc) · 2.05 KB
/
Fixme.py
File metadata and controls
98 lines (80 loc) · 2.05 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
'''
Your solution for each function should be only a single line long.
In particular, you may not use any loops when implementing these functions;
instead, you must use either the map and filter functions
or list comprehensions.
'''
def evens(n):
'''
Returns a list of even numbers from 0 to n inclusive.
>>> evens(10)
[0, 2, 4, 6, 8, 10]
>>> evens(11)
[0, 2, 4, 6, 8, 10]
>>> evens(0)
[0]
>>> evens(1)
[0]
>>> evens(-1)
[]
'''
return [x for x in range(0, n+1) if x % 2 == 0]
def threes(n):
'''
Returns a list of all numbers from 0 to n inclusive that
contain the digit 3.
>>> threes(2)
[]
>>> threes(3)
[3]
>>> threes(10)
[3]
>>> threes(20)
[3, 13]
>>> threes(50)
[3, 13, 23, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43]
'''
return [x for x in range(0, n+1) if '3' in str(x)]
def small_words(text):
'''
Returns a list of all words in the input text that
are less than 4 characters long.
HINT:
Recall that text.split() converts the text variable into a list of words.
>>> small_words('this is a simple test case')
['this', 'is', 'a', 'test', 'case']
>>> small_words('really enormous words')
[]
>>> small_words('')
[]
>>> small_words('a big word is bad')
['a', 'big', 'word', 'is', 'bad']
'''
return [word for word in text.split() if len(word) <= 4]
def squares(n):
'''
Returns a list of all square number between 1 and n inclusive.
Recall that the nth square number is defined to be n*n.
>>> squares(1)
[1]
>>> squares(2)
[1, 4]
>>> squares(3)
[1, 4, 9]
>>> squares(10)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
'''
return [x*x for x in range(1, n+1)]
def lengths(strings):
'''
Given a list of strings, returns a list of the lengths
of the corresponding strings.
>>> lengths([])
[]
>>> lengths(['test'])
[4]
>>> lengths(['this','is','a','test'])
[4, 2, 1, 4]
'''
return [len(x) for x in strings]