forked from Kaggle/learntools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex6.py
More file actions
211 lines (181 loc) · 6.39 KB
/
Copy pathex6.py
File metadata and controls
211 lines (181 loc) · 6.39 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import itertools
from learntools.core import *
from learntools.core.richtext import *
CS = CodeSolution
import learntools.python.solns.word_search as word_search_module
word_search = word_search_module.word_search
import learntools.python.solns.multi_word_search as mws_module
multi_word_search = mws_module.multi_word_search
import learntools.python.solns.diamond as d_module
diamond = d_module.diamond
import learntools.python.solns.roulette_analysis as rou_module
roulette_gt = rou_module.conditional_roulette_probs
class ZA(EqualityCheckProblem):
_var = 'length'
_default_values = [-1]
_expected = 0
_solution = ("The empty string has length zero. Note that the empty "
"string is also the only string that Python considers as False"
" when converting to boolean.")
class ZB(EqualityCheckProblem):
_var = 'length'
_default_values = [-1]
_expected = 7
_solution = ("Keep in mind Python includes spaces (and punctuation) when"
" counting string length.")
class ZC(EqualityCheckProblem):
_var = 'length'
_default_values = [-1]
_expected = 7
_solution = ("Even though we use different syntax to create it, the string"
" `c` is identical to `b`. In particular, note that the backslash"
" is not part of the string, so it doesn't contribute to its length.")
class ZD(EqualityCheckProblem):
_var = 'length'
_default_values = [-1]
_expected = 3
_solution = ("The fact that this string was created using triple-quote syntax"
" doesn't make any difference in terms of its content or length. This"
" string is exactly the same as `'hey'`.")
class ZE(EqualityCheckProblem):
_var = 'length'
_default_values = [-1]
_expected = 1
_solution = ("The newline character is just a single character! (Even though"
" we represent it to Python using a combination of two characters.)")
LightningLen = MultipartProblem(
ZA,ZB,ZC,ZD,ZE,
)
class ZipValidator(FunctionProblem):
_var = 'is_valid_zip'
_hint = ("Try looking up `help(str.isdigit)`")
_solution = CS(
"""def is_valid_zip(zip_str):
return len(zip_str) == 5 and zip_str.isdigit()""")
_test_cases = [
('12345', True),
('1234x', False),
('1234', False),
('00000', True),
('', False),
('?', False),
]
class WordSearch(FunctionProblem):
_var = 'word_search'
_solution = CS.load(word_search_module.__file__)
_hint = ("Some methods that may be useful here: `str.split()`, `str.strip()`,"
" `str.lower()`."
)
_test_docs_a = [
"The Learn Python Challenge Casino",
"They bought a car, and a horse",
"Casinoville?",
]
_test_docs_b = _test_docs_a + ["He bought a casino. That's crazy."]
_test_inputs = [
(_test_docs_a, 'casino'),
(_test_docs_a, 'ear'),
(_test_docs_a, 'car'),
(_test_docs_b, 'crazy'),
(_test_docs_b, 'bought'),
([], 'spam'),
]
_test_cases = [
(args, word_search(*args))
for args in _test_inputs
]
class MultiWordSearch(FunctionProblem):
_var = 'multi_word_search'
_solution = CS.load(mws_module.__file__)
_test_docs_a = [
"The Learn Python Challenge Casino",
"They bought a car",
"Casinoville?",
]
_test_docs_b = _test_docs_a + ["He bought a casino. That's crazy."]
_test_keywords = [
[],
['casino'],
['casino', 'ear'],
['casino', 'ear', 'bought'],
]
_test_cases = [
(args, multi_word_search(*args))
for args in itertools.product(
[_test_docs_a, _test_docs_b, []],
_test_keywords,
)
]
class DiamondArt(FunctionProblem):
_bonus = True
_var = 'diamond'
_solution = CS.load(d_module.__file__)
_hint = ("`str` has a few methods that help with the problem of padding"
" a string to a certain size: two that might help here are"
" `str.rjust()` or `str.center()`"
)
_test_heights = [2, 4, 0, 6]
_test_cases = [
(h, diamond(h))
for h in _test_heights
]
def check(self, fn):
for args, expected in self._test_cases:
orig_args = args
# Wrap in tuple if necessary
if not isinstance(args, tuple):
args = args,
try:
actual = fn(*args)
except Exception as e:
actual = e
assert actual is not None, ("Got a return value of `None`"
" given height = {}."
" Did you forget the return statement?").format(args[0])
orig_actual = actual
# Ignore spaces to the right of the diamond itself for purposes
# of comparison.
# Also, okay, fine, allow final newline in their answer. I guess.
if len(actual) > 0 and actual[-1] == '\n':
actual = actual[:-1]
normalize = lambda di: '\n'.join(line.rstrip() for line in di.split('\n'))
anorm = normalize(actual)
enorm = normalize(expected)
actual_shown = (
repr(orig_actual) if (orig_actual + ' ').isspace()
else actual
)
assert anorm == enorm, (
"Expected diamond looking something like...\n\n"
"```\n{}\n```\n for height={}, but instead got this thing...\n\n"
"```\n{}\n```\n").format(
expected,
args[0],
actual_shown,
)
class RouletteAnalyzer(FunctionProblem):
_bonus = True
_var = 'conditional_roulette_probs'
_solution = CS.load(rou_module.__file__)
_test_inputs = [
[1, 3, 1, 5, 1],
[1, 1, 1, 1,],
[1, 2, 1],
[5, 1, 3, 1, 2, 1 , 3, 3, 5, 1, 2],
]
_test_cases = [
(args, roulette_gt(args))
for args in _test_inputs
]
qvars = bind_exercises(globals(), [
LightningLen,
ZipValidator,
WordSearch,
MultiWordSearch,
DiamondArt,
RouletteAnalyzer
],
tutorial_id=112,
start=0,
)
__all__ = list(qvars)