@@ -55,12 +55,13 @@ def gen_random(self):
55
55
56
56
assert StrAdd .sat (StrAdd .sol (** StrAdd .get_example ()))
57
57
58
+
58
59
@register
59
60
class StrSetLen (Problem ):
60
- """Find a string with a certain number of duplicate chars"""
61
+ """Find a string with `dups` duplicate chars"""
61
62
62
63
@staticmethod
63
- def sat (s : str , dups = 1000 ):
64
+ def sat (s : str , dups = 2021 ):
64
65
return len (set (s )) == len (s ) - dups
65
66
66
67
@staticmethod
@@ -73,9 +74,10 @@ def gen(self, target_num_problems):
73
74
return
74
75
self .add (dict (dups = dups ))
75
76
77
+
76
78
@register
77
79
class StrMul (Problem ):
78
- """Solve string multiplication problem """
80
+ """Find a string which when repeated `n` times gives `target` """
79
81
80
82
@staticmethod
81
83
def sat (s : str , target = 'foofoofoofoo' , n = 2 ):
@@ -96,7 +98,7 @@ def gen_random(self):
96
98
97
99
@register
98
100
class StrMul2 (Problem ):
99
- """Solve string multiplication problem """
101
+ """Find `n` such that `s` repeated `n` times gives `target` """
100
102
101
103
@staticmethod
102
104
def sat (n : int , target = 'foofoofoofoo' , s = 'foofoo' ):
@@ -117,7 +119,7 @@ def gen_random(self):
117
119
118
120
@register
119
121
class StrLen (Problem ):
120
- """Solve string length problem """
122
+ """Find a string of length `n` """
121
123
122
124
@staticmethod
123
125
def sat (s : str , n = 1000 ):
@@ -134,7 +136,7 @@ def gen_random(self):
134
136
135
137
@register
136
138
class StrAt (Problem ):
137
- """Solve str[i] problem """
139
+ """Find the index of `target` in string `s` """
138
140
139
141
@staticmethod
140
142
def sat (i : int , s = "cat" , target = "a" ):
@@ -152,7 +154,7 @@ def gen_random(self):
152
154
153
155
@register
154
156
class StrNegAt (Problem ):
155
- """Solve str[-i] problem """
157
+ """Find the index of `target` in `s` using a negative index. """
156
158
157
159
@staticmethod
158
160
def sat (i : int , s = "cat" , target = "a" ):
@@ -170,7 +172,7 @@ def gen_random(self):
170
172
171
173
@register
172
174
class StrSlice (Problem ):
173
- """Solve string slice problem """
175
+ """Find the three slice indices that give the specific `target` in string `s` """
174
176
175
177
@staticmethod
176
178
def sat (inds : List [int ], s = "hello world" , target = "do" ):
@@ -199,7 +201,7 @@ def gen_random(self):
199
201
200
202
@register
201
203
class StrIndex (Problem ):
202
- """Solve str. index problem """
204
+ """Find a string whose *first* index in `big_str` is `index` """
203
205
204
206
@staticmethod
205
207
def sat (s : str , big_str = "foobar" , index = 2 ):
@@ -215,9 +217,10 @@ def gen_random(self):
215
217
index = big_str .index (big_str [i :])
216
218
self .add (dict (big_str = big_str , index = index ))
217
219
220
+
218
221
@register
219
222
class StrIndex2 (Problem ):
220
- """Solve str. index problem """
223
+ """Find a string whose *first* index of `sub_str` is `index` """
221
224
222
225
@staticmethod
223
226
def sat (big_str : str , sub_str = "foobar" , index = 2 ):
@@ -228,7 +231,7 @@ def sol(sub_str, index):
228
231
i = ord ('A' )
229
232
while chr (i ) in sub_str :
230
233
i += 1
231
- return chr (i )* index + sub_str
234
+ return chr (i ) * index + sub_str
232
235
233
236
def gen_random (self ):
234
237
sub_str = self .random .pseudo_word (max_len = 50 )
@@ -238,18 +241,17 @@ def gen_random(self):
238
241
239
242
@register
240
243
class StrIn (Problem ):
241
- """Solve str in problem """
244
+ """Find a string of length `length` that is in both strings `a` and `b` """
242
245
243
246
@staticmethod
244
247
def sat (s : str , a = "hello" , b = "yellow" , length = 4 ):
245
248
return len (s ) == length and s in a and s in b
246
249
247
250
@staticmethod
248
251
def sol (a , b , length ):
249
- for i in range (len (a )- length + 1 ):
250
- if a [i :i + length ] in b :
251
- return a [i :i + length ]
252
-
252
+ for i in range (len (a ) - length + 1 ):
253
+ if a [i :i + length ] in b :
254
+ return a [i :i + length ]
253
255
254
256
def gen_random (self ):
255
257
sub_str = self .random .pseudo_word ()
@@ -261,22 +263,205 @@ def gen_random(self):
261
263
262
264
@register
263
265
class StrIn2 (Problem ):
264
- """Solve str in problem """
266
+ """Find a list of >= `count` distinct strings that are all contained in `s` """
265
267
266
268
@staticmethod
267
269
def sat (substrings : List [str ], s = "hello" , count = 15 ):
268
270
return len (substrings ) == len (set (substrings )) >= count and all (sub in s for sub in substrings )
269
271
270
272
@staticmethod
271
273
def sol (s , count ):
272
- return ["" ] + sorted ({s [j :i ] for i in range (len (s )+ 1 ) for j in range (i )})
274
+ return ["" ] + sorted ({s [j :i ] for i in range (len (s ) + 1 ) for j in range (i )})
273
275
274
276
def gen_random (self ):
275
277
s = self .random .pseudo_word (max_len = 50 )
276
278
count = len (self .sol (s , None ))
277
279
self .add (dict (s = s , count = count ))
278
280
279
281
282
+ ########################################
283
+ # List problems
284
+ ########################################
285
+
286
+
287
+ @register
288
+ class ListSetLen (Problem ):
289
+ """Find a list with a certain number of duplicate items"""
290
+
291
+ @staticmethod
292
+ def sat (li : List [int ], dups = 42155 ):
293
+ return len (set (li )) == len (li ) - dups
294
+
295
+ @staticmethod
296
+ def sol (dups ):
297
+ return [1 ] * (dups + 1 )
298
+
299
+ def gen_random (self ):
300
+ self .add (dict (dups = self .random .randrange (10 ** 5 )))
301
+
302
+
303
+ @register
304
+ class ListMul (Problem ):
305
+ """Find a list that when multiplied n times gives the target list"""
306
+
307
+ @staticmethod
308
+ def sat (li : List [int ], target = [17 , 9 , - 1 , 17 , 9 , - 1 ], n = 2 ):
309
+ return li * n == target
310
+
311
+ @staticmethod
312
+ def sol (target , n ):
313
+ if n == 0 :
314
+ return []
315
+ return target [:len (target ) // n ]
316
+
317
+ def gen_random (self ):
318
+ li = [self .random .randrange (- 10 ** 5 , 10 ** 5 ) for _ in
319
+ range (self .random .randrange (1 , 10 ))] * self .random .randint (1 , 3 )
320
+ n = self .random .randrange (10 )
321
+ target = li * n
322
+ self .add (dict (target = target , n = n ))
323
+
324
+
325
+ @register
326
+ class ListLen (Problem ):
327
+ """Find a list of a given length n"""
328
+
329
+ @staticmethod
330
+ def sat (li : List [int ], n = 85012 ):
331
+ return len (li ) == n
332
+
333
+ @staticmethod
334
+ def sol (n ):
335
+ return [1 ] * n
336
+
337
+ def gen_random (self ):
338
+ n = self .random .randrange (self .random .choice ([10 , 100 , 1000 , 10000 ]))
339
+ self .add (dict (n = n ))
340
+
341
+
342
+ @register
343
+ class ListAt (Problem ):
344
+ """Find the index of an item in a list. Any such index is fine."""
345
+
346
+ @staticmethod
347
+ def sat (i : int , li = [17 , 31 , 91 , 18 , 42 , 1 , 9 ], target = 18 ):
348
+ return li [i ] == target
349
+
350
+ @staticmethod
351
+ def sol (li , target ):
352
+ return li .index (target )
353
+
354
+ def gen_random (self ):
355
+ li = [self .random .randrange (- 10 ** 2 , 10 ** 2 ) for _ in range (self .random .randrange (1 , 20 ))]
356
+ target = self .random .choice (li )
357
+ self .add (dict (li = li , target = target ))
358
+
359
+
360
+ @register
361
+ class ListNegAt (Problem ):
362
+ """Find the index of an item in a list using negative indexing."""
363
+
364
+ @staticmethod
365
+ def sat (i : int , li = [17 , 31 , 91 , 18 , 42 , 1 , 9 ], target = 91 ):
366
+ return li [i ] == target and i < 0
367
+
368
+ @staticmethod
369
+ def sol (li , target ):
370
+ return li .index (target ) - len (li )
371
+
372
+ def gen_random (self ):
373
+ li = [self .random .randrange (- 10 ** 2 , 10 ** 2 ) for _ in range (self .random .randrange (1 , 20 ))]
374
+ target = self .random .choice (li )
375
+ self .add (dict (li = li , target = target ))
376
+
377
+
378
+ @register
379
+ class ListSlice (Problem ):
380
+ """Find three slice indices to achieve a given list slice"""
381
+
382
+ @staticmethod
383
+ def sat (inds : List [int ], li = [42 , 18 , 21 , 103 , - 2 , 11 ], target = [- 2 , 21 , 42 ]):
384
+ i , j , k = inds
385
+ return li [i :j :k ] == target
386
+
387
+ @staticmethod
388
+ def sol (li , target ):
389
+ from itertools import product
390
+ for i , j , k in product (range (- len (li ) - 1 , len (li ) + 1 ), repeat = 3 ):
391
+ try :
392
+ if li [i :j :k ] == target :
393
+ return [i , j , k ]
394
+ except (IndexError , ValueError ):
395
+ pass
396
+
397
+ def gen_random (self ):
398
+ li = [self .random .randrange (- 10 ** 2 , 10 ** 2 ) for _ in range (self .random .randrange (1 , 20 ))]
399
+ i , j , k = [self .random .randrange (- len (li ) - 1 , len (li ) + 1 ) for _ in range (3 )]
400
+ try :
401
+ target = li [i :j :k ]
402
+ if (target != [] and target != li ) or self .random .randrange (50 ) == 0 :
403
+ self .add (dict (li = li , target = target ))
404
+ except (IndexError , ValueError ):
405
+ pass
406
+
407
+
408
+
409
+ @register
410
+ class ListIndex (Problem ):
411
+ """Find the item whose first index in `li` is `index`"""
412
+
413
+ @staticmethod
414
+ def sat (item : int , li = [17 , 2 , 3 , 9 , 11 , 11 ], index = 4 ):
415
+ return li .index (item ) == index
416
+
417
+ @staticmethod
418
+ def sol (li , index ):
419
+ return li [index ]
420
+
421
+ def gen_random (self ):
422
+ li = [self .random .randrange (- 10 ** 2 , 10 ** 2 ) for _ in range (self .random .randrange (1 , 20 ))]
423
+ i = self .random .randrange (len (li ))
424
+ index = li .index (li [i ])
425
+ self .add (dict (li = li , index = index ))
426
+
427
+ @register
428
+ class ListIndex2 (Problem ):
429
+ """Find a list that contains `i` first at index `index`"""
430
+
431
+ @staticmethod
432
+ def sat (li : List [int ], i = 29 , index = 10412 ):
433
+ return li .index (i ) == index
434
+
435
+ @staticmethod
436
+ def sol (i , index ):
437
+ return [i - 1 ] * index + [i ]
438
+
439
+ def gen_random (self ):
440
+ i = self .random .randrange (- 10 ** 5 , 10 ** 5 )
441
+ index = self .random .randrange (10 ** 5 )
442
+ self .add (dict (i = i , index = index ))
443
+
444
+
445
+ @register
446
+ class ListIn (Problem ):
447
+ """Find an item that is in both lists `a` and `b`"""
448
+
449
+ @staticmethod
450
+ def sat (s : str , a = ['cat' , 'dot' , 'bird' ], b = ['tree' , 'fly' , 'dot' ]):
451
+ return s in a and s in b
452
+
453
+ @staticmethod
454
+ def sol (a , b ):
455
+ return next (s for s in b if s in a )
456
+
457
+ def gen_random (self ):
458
+ a = [self .random .pseudo_word () for _ in range (self .random .randrange (1 , 100 ))]
459
+ b = [self .random .pseudo_word () for _ in range (self .random .randrange (1 , 100 ))]
460
+ b .insert (self .random .randrange (len (b )), self .random .choice (a ))
461
+ self .add (dict (a = a , b = b ))
462
+
463
+
464
+
280
465
########################################
281
466
# int problems
282
467
########################################
@@ -401,7 +586,7 @@ def gen_random(self):
401
586
402
587
@register
403
588
class IntDiv2 (Problem ):
404
- """Solve division problem """
589
+ """Find `n` that when divided by `b` is `a` """
405
590
406
591
@staticmethod
407
592
def sat (n : int , a = 345346363 , b = 10 ):
@@ -420,7 +605,7 @@ def gen_random(self):
420
605
421
606
422
607
@register
423
- class SquareRoot (Problem ):
608
+ class IntSquareRoot (Problem ):
424
609
"""Compute square root of number.
425
610
The target has a round (integer) square root."""
426
611
@@ -439,7 +624,7 @@ def gen_random(self):
439
624
440
625
441
626
@register
442
- class NegSquareRoot (Problem ):
627
+ class IntNegSquareRoot (Problem ):
443
628
"""Compute negative square root of number.
444
629
The target has a round (integer) square root."""
445
630
@@ -458,7 +643,7 @@ def gen_random(self):
458
643
459
644
460
645
@register
461
- class SquareRootFloat (Problem ):
646
+ class FloatSquareRoot (Problem ):
462
647
"""Compute square root of number.
463
648
The target might not have a round solution.
464
649
Accuracy of third decimal digit is required."""
@@ -477,7 +662,7 @@ def gen_random(self):
477
662
478
663
479
664
@register
480
- class NegSquareRootFloat (Problem ):
665
+ class FloatNegSquareRoot (Problem ):
481
666
"""Compute (negative) square root of number.
482
667
The target might not have a round solution.
483
668
Accuracy of third decimal digit is required."""
0 commit comments