forked from microsoft/PythonProgrammingPuzzles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy.py
More file actions
410 lines (309 loc) · 12.7 KB
/
study.py
File metadata and controls
410 lines (309 loc) · 12.7 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
Puzzles used in our user study (the user study didn't have docstrings), see [Programming Puzzles](https://arxiv.org/abs/2106.05784).
"""
from puzzle_generator import PuzzleGenerator, Tags
from typing import List
# See https://github.com/microsoft/PythonProgrammingPuzzles/wiki/How-to-add-a-puzzle to learn about adding puzzles
class Study_1(PuzzleGenerator):
@staticmethod
def sat(s: str):
"""Find a string with 1000 'o's but no two adjacent 'o's."""
return s.count('o') == 1000 and s.count('oo') == 0
@staticmethod
def sol():
return ('h' + 'o') * 1000
class Study_2(PuzzleGenerator):
@staticmethod
def sat(s: str):
"""Find a string with 1000 'o's, 100 pairs of adjacent 'o's and 801 copies of 'ho'."""
return s.count('o') == 1000 and s.count('oo') == 100 and s.count('ho') == 801
@staticmethod
def sol():
return 'ho' * (800 + 1) + 'o' * (100 * 2 - 1)
class Study_3(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""Find a permutation of [0, 1, ..., 998] such that the ith element is *not* i, for all i=0, 1, ..., 998."""
return sorted(li) == list(range(999)) and all(li[i] != i for i in range(len(li)))
@staticmethod
def sol():
return [((i + 1) % 999) for i in range(999)]
class Study_4(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""Find a list of length 10 where the fourth element occurs exactly twice."""
return len(li) == 10 and li.count(li[3]) == 2
@staticmethod
def sol():
return list(range(10 // 2)) * 2
class Study_5(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""Find a list integers such that the integer i occurs i times, for i = 0, 1, 2, ..., 9."""
return all([li.count(i) == i for i in range(10)])
@staticmethod
def sol():
return [i for i in range(10) for j in range(i)]
class Study_6(PuzzleGenerator):
@staticmethod
def sat(i: int):
"""Find an integer greater than 10^10 which is 4 mod 123."""
return i % 123 == 4 and i > 10 ** 10
@staticmethod
def sol():
return 4 + 10 ** 10 + 123 - 10 ** 10 % 123
class Study_7(PuzzleGenerator):
@staticmethod
def sat(s: str):
"""Find a three-digit pattern that occurs more than 8 times in the decimal representation of 8^2888."""
return str(8 ** 2888).count(s) > 8 and len(s) == 3
@staticmethod
def sol():
s = str(8 ** 2888)
return max({s[i: i + 3] for i in range(len(s) - 2)}, key=lambda t: s.count(t))
class Study_8(PuzzleGenerator):
@staticmethod
def sat(ls: List[str]):
"""Find a list of more than 1235 strings such that the 1234th string is a proper substring of the 1235th."""
return ls[1234] in ls[1235] and ls[1234] != ls[1235]
@staticmethod
def sol():
return [''] * 1235 + ['a']
class Study_9(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""
Find a way to rearrange the letters in the pangram "The quick brown fox jumps over the lazy dog" to get
the pangram "The five boxing wizards jump quickly". The answer should be represented as a list of index
mappings.
"""
return ["The quick brown fox jumps over the lazy dog"[i] for i in li] == list(
"The five boxing wizards jump quickly")
@staticmethod
def sol():
return ['The quick brown fox jumps over the lazy dog'.index(t)
for t in 'The five boxing wizards jump quickly']
class Study_10(PuzzleGenerator):
@staticmethod
def sat(s: str):
"""Find a palindrome of length greater than 11 in the decimal representation of 8^1818."""
return s in str(8 ** 1818) and s == s[::-1] and len(s) > 11
@staticmethod
def sol():
s = str(8 ** 1818)
return next(s[i: i + le]
for le in range(12, len(s) + 1)
for i in range(len(s) - le + 1)
if s[i: i + le] == s[i: i + le][::-1]
)
class Study_11(PuzzleGenerator):
@staticmethod
def sat(ls: List[str]):
"""
Find a list of strings whose length (viewed as a string) is equal to the lexicographically largest element
and is equal to the lexicographically smallest element.
"""
return min(ls) == max(ls) == str(len(ls))
@staticmethod
def sol():
return ['1']
class Study_12(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""Find a list of 1,000 integers where every two adjacent integers sum to 9, and where the first
integer plus 4 is 9."""
return all(i + j == 9 for i, j in zip([4] + li, li)) and len(li) == 1000
@staticmethod
def sol():
return [9 - 4, 4] * (1000 // 2)
class Study_13(PuzzleGenerator):
@staticmethod
def sat(x: float):
"""Find a real number which, when you subtract 3.1415, has a decimal representation starting with 123.456."""
return str(x - 3.1415).startswith("123.456")
@staticmethod
def sol():
return 123.456 + 3.1415
class Study_14(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""Find a list of integers such that the sum of the first i integers is i, for i=0, 1, 2, ..., 19."""
return all([sum(li[:i]) == i for i in range(20)])
@staticmethod
def sol():
return [1] * 20
class Study_15(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""Find a list of integers such that the sum of the first i integers is 2^i -1, for i = 0, 1, 2, ..., 19."""
return all(sum(li[:i]) == 2 ** i - 1 for i in range(20))
@staticmethod
def sol():
return [(2 ** i) for i in range(20)]
class Study_16(PuzzleGenerator):
@staticmethod
def sat(s: str):
"""Find a real number such that when you add the length of its decimal representation to it, you get 4.5.
Your answer should be the string form of the number in its decimal representation."""
return float(s) + len(s) == 4.5
@staticmethod
def sol():
return str(4.5 - len(str(4.5)))
class Study_17(PuzzleGenerator):
@staticmethod
def sat(i: int):
"""Find a number whose decimal representation is *a longer string* when you add 1,000 to it than when you add 1,001."""
return len(str(i + 1000)) > len(str(i + 1001))
@staticmethod
def sol():
return -1001
class Study_18(PuzzleGenerator):
@staticmethod
def sat(ls: List[str]):
"""
Find a list of strings that when you combine them in all pairwise combinations gives the six strings:
'berlin', 'berger', 'linber', 'linger', 'gerber', 'gerlin'
"""
return [s + t for s in ls for t in ls if s != t] == 'berlin berger linber linger gerber gerlin'.split()
@staticmethod
def sol():
seen = set()
ans = []
for s in 'berlin berger linber linger gerber gerlin'.split():
t = s[:3]
if t not in seen:
ans.append(t)
seen.add(t)
return ans
class Study_19(PuzzleGenerator):
"""
9/15/2021 Updated to take a list rather than a set because it was the only puzzle in the repo with Set argument.
"""
@staticmethod
def sat(li: List[int]):
"""
Find a list of integers whose pairwise sums make the set {0, 1, 2, 3, 4, 5, 6, 17, 18, 19, 20, 34}.
That is find L such that, { i + j | i, j in L } = {0, 1, 2, 3, 4, 5, 6, 17, 18, 19, 20, 34}.
"""
return {i + j for i in li for j in li} == {0, 1, 2, 3, 4, 5, 6, 17, 18, 19, 20, 34}
@staticmethod
def sol():
return [0, 1, 2, 3, 17]
class Study_20(PuzzleGenerator):
"""A more interesting version of this puzzle with a length constraint is ShortIntegerPath in graphs.py"""
@staticmethod
def sat(li: List[int]):
"""
Find a list of integers, starting with 0 and ending with 128, such that each integer either differs from
the previous one by one or is thrice the previous one.
"""
return all(j in {i - 1, i + 1, 3 * i} for i, j in zip([0] + li, li + [128]))
@staticmethod
def sol():
return [1, 3, 4, 12, 13, 14, 42, 126, 127]
class Study_21(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""
Find a list integers containing exactly three distinct values, such that no integer repeats
twice consecutively among the first eleven entries. (So the list needs to have length greater than ten.)
"""
return all([li[i] != li[i + 1] for i in range(10)]) and len(set(li)) == 3
@staticmethod
def sol():
return list(range(3)) * 10
class Study_22(PuzzleGenerator):
@staticmethod
def sat(s: str):
"""
Find a string s containing exactly five distinct characters which also contains as a substring every other
character of s (e.g., if the string s were 'parrotfish' every other character would be 'profs').
"""
return s[::2] in s and len(set(s)) == 5
@staticmethod
def sol():
return """abacadaeaaaaaaaaaa"""
class Study_23(PuzzleGenerator):
@staticmethod
def sat(ls: List[str]):
"""
Find a list of characters which are aligned at the same indices of the three strings 'dee', 'doo', and 'dah!'.
"""
return tuple(ls) in zip('dee', 'doo', 'dah!')
@staticmethod
def sol():
return list(next(zip('dee', 'doo', 'dah!')))
class Study_24(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""Find a list of integers with exactly three occurrences of seventeen and at least two occurrences of three."""
return li.count(17) == 3 and li.count(3) >= 2
@staticmethod
def sol():
return [17] * 3 + [3] * 2
class Study_25(PuzzleGenerator):
@staticmethod
def sat(s: str):
"""Find a permutation of the string 'Permute me true' which is a palindrome."""
return sorted(s) == sorted('Permute me true') and s == s[::-1]
@staticmethod
def sol():
s = sorted('Permute me true'[1:])[::2]
return "".join(s + ['P'] + s[::-1])
class Study_26(PuzzleGenerator):
@staticmethod
def sat(ls: List[str]):
"""Divide the decimal representation of 8^88 up into strings of length eight."""
return "".join(ls) == str(8 ** 88) and all(len(s) == 8 for s in ls)
@staticmethod
def sol():
return [str(8 ** 88)[i:i + 8] for i in range(0, len(str(8 ** 88)), 8)]
class Study_27(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""
Consider a digraph where each node has exactly one outgoing edge. For each edge (u, v), call u the parent and
v the child. Then find such a digraph where the grandchildren of the first and second nodes differ but they
share the same great-grandchildren. Represented this digraph by the list of children indices.
"""
return li[li[0]] != li[li[1]] and li[li[li[0]]] == li[li[li[1]]]
@staticmethod
def sol():
return [1, 2, 3, 3]
class Study_28(PuzzleGenerator):
"""9/15/2021: updated to a list since sets were removed from puzzle formats"""
@staticmethod
def sat(li: List[int]):
"""Find a list of one hundred integers between 0 and 999 which all differ by at least ten from one another."""
return all(i in range(1000) and abs(i - j) >= 10 for i in li for j in li if i != j) and len(set(li)) == 100
@staticmethod
def sol():
return list(range(0, 1000, 10))
class Study_29(PuzzleGenerator):
"""9/15/2021: updated to a list since sets were removed from puzzle formats"""
@staticmethod
def sat(l: List[int]):
"""
Find a list of more than 995 distinct integers between 0 and 999, inclusive, such that each pair of integers
have squares that differ by at least 10.
"""
return all(i in range(1000) and abs(i * i - j * j) >= 10 for i in l for j in l if i != j) and len(set(l)) > 995
@staticmethod
def sol():
return [0, 4] + list(range(6, 1000))
class Study_30(PuzzleGenerator):
@staticmethod
def sat(li: List[int]):
"""
Define f(n) to be the residue of 123 times n mod 1000. Find a list of integers such that the first twenty one
are between 0 and 999, inclusive, and are strictly increasing in terms of f(n).
"""
return all([123 * li[i] % 1000 < 123 * li[i + 1] % 1000 and li[i] in range(1000) for i in range(20)])
@staticmethod
def sol():
return sorted(range(1000), key=lambda n: 123 * n % 1000)[:21]
@staticmethod
def sol_surprisingly_short():
return list(range(1000))[::8][::-1]
if __name__ == "__main__":
PuzzleGenerator.debug_problems()