Skip to content

Commit 2937be6

Browse files
committed
Added trivial inverse problems (and renamed file)
1 parent e353612 commit 2937be6

File tree

3 files changed

+209
-26
lines changed

3 files changed

+209
-26
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
111111
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
112112
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
113113

114-
115-
### Datasheet
116114
See the [datasheet](DATASHEET.md) for our dataset.
117115

118116
## Trademarks

templates/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
from . import ICPC
88
from . import IMO
99
from . import puzzles
10-
from . import trivial
10+
from . import trivial_inverse

templates/trivial.py renamed to templates/trivial_inverse.py

Lines changed: 208 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ def gen_random(self):
5555

5656
assert StrAdd.sat(StrAdd.sol(**StrAdd.get_example()))
5757

58+
5859
@register
5960
class StrSetLen(Problem):
60-
"""Find a string with a certain number of duplicate chars"""
61+
"""Find a string with `dups` duplicate chars"""
6162

6263
@staticmethod
63-
def sat(s: str, dups=1000):
64+
def sat(s: str, dups=2021):
6465
return len(set(s)) == len(s) - dups
6566

6667
@staticmethod
@@ -73,9 +74,10 @@ def gen(self, target_num_problems):
7374
return
7475
self.add(dict(dups=dups))
7576

77+
7678
@register
7779
class StrMul(Problem):
78-
"""Solve string multiplication problem"""
80+
"""Find a string which when repeated `n` times gives `target`"""
7981

8082
@staticmethod
8183
def sat(s: str, target='foofoofoofoo', n=2):
@@ -96,7 +98,7 @@ def gen_random(self):
9698

9799
@register
98100
class StrMul2(Problem):
99-
"""Solve string multiplication problem"""
101+
"""Find `n` such that `s` repeated `n` times gives `target`"""
100102

101103
@staticmethod
102104
def sat(n: int, target='foofoofoofoo', s='foofoo'):
@@ -117,7 +119,7 @@ def gen_random(self):
117119

118120
@register
119121
class StrLen(Problem):
120-
"""Solve string length problem"""
122+
"""Find a string of length `n`"""
121123

122124
@staticmethod
123125
def sat(s: str, n=1000):
@@ -134,7 +136,7 @@ def gen_random(self):
134136

135137
@register
136138
class StrAt(Problem):
137-
"""Solve str[i] problem"""
139+
"""Find the index of `target` in string `s`"""
138140

139141
@staticmethod
140142
def sat(i: int, s="cat", target="a"):
@@ -152,7 +154,7 @@ def gen_random(self):
152154

153155
@register
154156
class StrNegAt(Problem):
155-
"""Solve str[-i] problem"""
157+
"""Find the index of `target` in `s` using a negative index."""
156158

157159
@staticmethod
158160
def sat(i: int, s="cat", target="a"):
@@ -170,7 +172,7 @@ def gen_random(self):
170172

171173
@register
172174
class StrSlice(Problem):
173-
"""Solve string slice problem"""
175+
"""Find the three slice indices that give the specific `target` in string `s`"""
174176

175177
@staticmethod
176178
def sat(inds: List[int], s="hello world", target="do"):
@@ -199,7 +201,7 @@ def gen_random(self):
199201

200202
@register
201203
class StrIndex(Problem):
202-
"""Solve str.index problem"""
204+
"""Find a string whose *first* index in `big_str` is `index`"""
203205

204206
@staticmethod
205207
def sat(s: str, big_str="foobar", index=2):
@@ -215,9 +217,10 @@ def gen_random(self):
215217
index = big_str.index(big_str[i:])
216218
self.add(dict(big_str=big_str, index=index))
217219

220+
218221
@register
219222
class StrIndex2(Problem):
220-
"""Solve str.index problem"""
223+
"""Find a string whose *first* index of `sub_str` is `index`"""
221224

222225
@staticmethod
223226
def sat(big_str: str, sub_str="foobar", index=2):
@@ -228,7 +231,7 @@ def sol(sub_str, index):
228231
i = ord('A')
229232
while chr(i) in sub_str:
230233
i += 1
231-
return chr(i)*index + sub_str
234+
return chr(i) * index + sub_str
232235

233236
def gen_random(self):
234237
sub_str = self.random.pseudo_word(max_len=50)
@@ -238,18 +241,17 @@ def gen_random(self):
238241

239242
@register
240243
class StrIn(Problem):
241-
"""Solve str in problem"""
244+
"""Find a string of length `length` that is in both strings `a` and `b`"""
242245

243246
@staticmethod
244247
def sat(s: str, a="hello", b="yellow", length=4):
245248
return len(s) == length and s in a and s in b
246249

247250
@staticmethod
248251
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]
253255

254256
def gen_random(self):
255257
sub_str = self.random.pseudo_word()
@@ -261,22 +263,205 @@ def gen_random(self):
261263

262264
@register
263265
class StrIn2(Problem):
264-
"""Solve str in problem"""
266+
"""Find a list of >= `count` distinct strings that are all contained in `s`"""
265267

266268
@staticmethod
267269
def sat(substrings: List[str], s="hello", count=15):
268270
return len(substrings) == len(set(substrings)) >= count and all(sub in s for sub in substrings)
269271

270272
@staticmethod
271273
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)})
273275

274276
def gen_random(self):
275277
s = self.random.pseudo_word(max_len=50)
276278
count = len(self.sol(s, None))
277279
self.add(dict(s=s, count=count))
278280

279281

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+
280465
########################################
281466
# int problems
282467
########################################
@@ -401,7 +586,7 @@ def gen_random(self):
401586

402587
@register
403588
class IntDiv2(Problem):
404-
"""Solve division problem"""
589+
"""Find `n` that when divided by `b` is `a`"""
405590

406591
@staticmethod
407592
def sat(n: int, a=345346363, b=10):
@@ -420,7 +605,7 @@ def gen_random(self):
420605

421606

422607
@register
423-
class SquareRoot(Problem):
608+
class IntSquareRoot(Problem):
424609
"""Compute square root of number.
425610
The target has a round (integer) square root."""
426611

@@ -439,7 +624,7 @@ def gen_random(self):
439624

440625

441626
@register
442-
class NegSquareRoot(Problem):
627+
class IntNegSquareRoot(Problem):
443628
"""Compute negative square root of number.
444629
The target has a round (integer) square root."""
445630

@@ -458,7 +643,7 @@ def gen_random(self):
458643

459644

460645
@register
461-
class SquareRootFloat(Problem):
646+
class FloatSquareRoot(Problem):
462647
"""Compute square root of number.
463648
The target might not have a round solution.
464649
Accuracy of third decimal digit is required."""
@@ -477,7 +662,7 @@ def gen_random(self):
477662

478663

479664
@register
480-
class NegSquareRootFloat(Problem):
665+
class FloatNegSquareRoot(Problem):
481666
"""Compute (negative) square root of number.
482667
The target might not have a round solution.
483668
Accuracy of third decimal digit is required."""

0 commit comments

Comments
 (0)