Skip to content

Commit 0de81e3

Browse files
committed
Improved the formatting of several problem descriptions.
Changed seed behavior: moved _secret_seed into problems.py and made it so changing the description does not change the seed.
1 parent 9cb2c08 commit 0de81e3

File tree

5 files changed

+63
-76
lines changed

5 files changed

+63
-76
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python Programming Puzzles
1+
# Python Programming Puzzles (P3)
22

33
This repo contains a dataset of python programming puzzles which can be used to teach and evaluate
44
an AI's programming proficiency. We hope this dataset with **grow rapidly**, and it is already diverse in
@@ -46,6 +46,10 @@ def sat(moves: List[List[int]]):
4646

4747
```
4848

49+
For more information on the motivation and how programming puzzles can help AI learn to program, see
50+
the paper:
51+
*Programming Puzzles*, by Tal Schuster, Ashwin Kalyan, Alex Polozov, and Adam Tauman Kalai. 2021 (Link to be added shortly)
52+
4953
# [Click here to browse the puzzles](/problems/README.md)
5054

5155
The problems in this repo are based on:

problems/README.md

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,11 +1694,10 @@ def sol(capacities=[8, 5, 3], init=[8, 0, 0], goal=[4, 4, 0]):
16941694
([classic_puzzles](#classic_puzzles) 21/22)
16951695

16961696
**Description:**
1697-
Find a substitution of digits for characters to make the numbers add up, like this:
1697+
Find a substitution of digits for characters to make the numbers add up in a sum like this:
16981698
SEND + MORE = MONEY
1699-
9567 + 1085 = 10652
17001699

1701-
The first digit in any cannot be 0.
1700+
The first digit in any number cannot be 0.
17021701
See [Wikipedia article](https://en.wikipedia.org/wiki/Verbal_arithmetic)
17031702

17041703
**Problem:**
@@ -1787,9 +1786,22 @@ def sol(words=['SEND', 'MORE', 'MONEY']):
17871786
**Description:**
17881787
[Sliding puzzle](https://en.wikipedia.org/wiki/15_puzzle)
17891788

1790-
Classic example of A* search. NP-hard but the puzzles can all be solved with A* and an efficient representation
1789+
The 3-, 8-, and 15-sliding puzzles are classic examples of A* search. In this puzzle, you are given a board like:
1790+
1 2 5
1791+
3 4 0
1792+
6 7 8
17911793

1792-
3-, 8-, and 15-sliding puzzles
1794+
and your goal is to transform it to:
1795+
0 1 2
1796+
3 4 5
1797+
6 7 8
1798+
1799+
by a sequence of swaps with the 0 square (0 indicates blank). The starting configuration is given by a 2d list of
1800+
lists and the answer is represented by a list of integers indicating which number you swap with 0. In the above
1801+
example, the answer would be `[1, 2, 5]`
1802+
1803+
1804+
The problem is NP-hard but the puzzles can all be solved with A* and an efficient representation.
17931805

17941806
**Problem:**
17951807

@@ -1948,21 +1960,16 @@ def sol(word="antidisestablishmentarianism", max_len=10):
19481960
([codeforces](#codeforces) 3/24)
19491961

19501962
**Description:**
1951-
Find a minimal list of corner locations for a×a tiles that covers [0, m] × [0, n]
1952-
and does not double-cover squares.
1963+
Find a minimal list of corner locations for a×a tiles that covers [0, m] × [0, n] and does not double-cover
1964+
squares.
19531965

19541966
Sample Input:
1955-
19561967
m = 10
1957-
19581968
n = 9
1959-
19601969
a = 5
1961-
19621970
target = 4
19631971

19641972
Sample Output:
1965-
19661973
[[0, 0], [0, 5], [5, 0], [5, 5]]
19671974

19681975
Inspired by [Codeforces Problem 1 A](https://codeforces.com/problemset/problem/1/A)
@@ -1989,8 +1996,8 @@ def sol(m=10, n=9, a=5, target=4):
19891996
([codeforces](#codeforces) 4/24)
19901997

19911998
**Description:**
1992-
Given a list of lists of triples of integers, return True for each list with a total of at least 2 and False
1993-
for each other list.
1999+
Given a list of lists of triples of integers, return True for each list with a total of at least 2 and False for
2000+
each other list.
19942001

19952002
Inspired by [Codeforces Problem 231 A](https://codeforces.com/problemset/problem/231/A)
19962003

@@ -2042,8 +2049,8 @@ def sol(scores=[100, 95, 80, 70, 65, 9, 9, 9, 4, 2, 1], k=6):
20422049
([codeforces](#codeforces) 6/24)
20432050

20442051
**Description:**
2045-
Tile an m x n checkerboard with 2 x 1 tiles. The solution is a list of fourtuples [i1, j1, i2, j2]
2046-
with i2 == i1 and j2 == j1 + 1 or i2 == i1 + 1 and j2 == j1 with no overlap.
2052+
Tile an m x n checkerboard with 2 x 1 tiles. The solution is a list of fourtuples [i1, j1, i2, j2] with i2 == i1
2053+
and j2 == j1 + 1 or i2 == i1 + 1 and j2 == j1 with no overlap.
20472054

20482055
Inspired by [Codeforces Problem 50 A](https://codeforces.com/problemset/problem/50/A)
20492056

@@ -2079,17 +2086,14 @@ def sol(m=10, n=5, target=50):
20792086

20802087
**Description:**
20812088
This straightforward problem is a little harder than the Codeforces one.
2082-
Given a sequence of operations "++x",
2083-
"x++", "--x", "x--", and a target value, find initial value so that the final value is the target value.
2089+
Given a sequence of operations "++x", "x++", "--x", "x--", and a target value, find initial value so that the
2090+
final value is the target value.
20842091

20852092
Sample Input:
2086-
20872093
ops = ["x++", "--x", "--x"]
2088-
20892094
target = 12
20902095

20912096
Sample Output:
2092-
20932097
13
20942098

20952099
Inspired by [Codeforces Problem 282 A](https://codeforces.com/problemset/problem/282/A)
@@ -2157,13 +2161,9 @@ def sol(s="aaAab", t="aAaaB"):
21572161
We are given a 5x5 bimatrix with a single 1 like:
21582162

21592163
0 0 0 0 0
2160-
21612164
0 0 0 0 1
2162-
21632165
0 0 0 0 0
2164-
21652166
0 0 0 0 0
2166-
21672167
0 0 0 0 0
21682168

21692169
Find a (minimal) sequence of row and column swaps to move the 1 to the center. A move is a string
@@ -2270,8 +2270,8 @@ def sol(word="konjac"):
22702270
([codeforces](#codeforces) 12/24)
22712271

22722272
**Description:**
2273-
You are given a string consisting of a's, b's and c's, find any longest substring containing no
2274-
repeated consecutive characters.
2273+
You are given a string consisting of a's, b's and c's, find any longest substring containing no repeated
2274+
consecutive characters.
22752275

22762276
Sample Input:
22772277
`"abbbc"`
@@ -2310,13 +2310,10 @@ You are given a string consisting of 0's and 1's. Find an index after which the
23102310
all 0's or all 1's.
23112311

23122312
Sample Input:
2313-
23142313
s = 0000111111100000, k = 5
23152314

23162315
Sample Output:
2317-
23182316
4
2319-
23202317
(or 5 or 6 or 11)
23212318

23222319
Inspired by [Codeforces Problem 96 A](https://codeforces.com/problemset/problem/96/A)
@@ -2390,13 +2387,10 @@ def sol(n=7):
23902387
Shortest Combination Lock Path
23912388

23922389
Given a starting a final lock position, find the (minimal) intermediate states, where each transition
2393-
involves increasing or decreasing a single digit (mod 10)
2394-
e.g.
2390+
involves increasing or decreasing a single digit (mod 10), e.g.
23952391

23962392
start = "012"
2397-
23982393
combo = "329"
2399-
24002394
output: ['112', '212', '312', '322', '321', '320']
24012395

24022396
Inspired by [Codeforces Problem 540 A](https://codeforces.com/problemset/problem/540/A)
@@ -4926,8 +4920,6 @@ The problem is *unsolved* in the sense that no known polynomial-time algorithm h
49264920

49274921
We include McCurley's discrete log challenge from
49284922
[Weber D., Denny T. (1998) "The solution of McCurley's discrete log challenge."](https://link.springer.com/content/pdf/10.1007/BFb0055747.pdf)
4929-
whose answer is
4930-
`n = 325923617918270562238615985978623709128341338833721058543950813521768156295091638348030637920237175638117352442299234041658748471079911977497864301995972638266781162575370644813703762423329783129621567127479417280687495231463348812`
49314923

49324924
**Problem:**
49334925

templates/classic_puzzles.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,12 +1022,11 @@ def random_reachable(capacities: List[int], init: List[int]):
10221022

10231023

10241024
@register
1025-
class VerbalArithmetic(Problem):
1026-
"""Find a substitution of digits for characters to make the numbers add up, like this:
1025+
class VerbalArithmetic(Problem): # updated because the answer was given away in the docstring! OMG
1026+
"""Find a substitution of digits for characters to make the numbers add up in a sum like this:
10271027
SEND + MORE = MONEY
1028-
9567 + 1085 = 10652
10291028
1030-
The first digit in any cannot be 0.
1029+
The first digit in any number cannot be 0.
10311030
See [Wikipedia article](https://en.wikipedia.org/wiki/Verbal_arithmetic)
10321031
"""
10331032

@@ -1125,9 +1124,22 @@ def gen_random(self):
11251124
class SlidingPuzzle(Problem):
11261125
"""[Sliding puzzle](https://en.wikipedia.org/wiki/15_puzzle)
11271126
1128-
Classic example of A* search. NP-hard but the puzzles can all be solved with A* and an efficient representation
1127+
The 3-, 8-, and 15-sliding puzzles are classic examples of A* search. In this puzzle, you are given a board like:
1128+
1 2 5
1129+
3 4 0
1130+
6 7 8
11291131
1130-
3-, 8-, and 15-sliding puzzles
1132+
and your goal is to transform it to:
1133+
0 1 2
1134+
3 4 5
1135+
6 7 8
1136+
1137+
by a sequence of swaps with the 0 square (0 indicates blank). The starting configuration is given by a 2d list of
1138+
lists and the answer is represented by a list of integers indicating which number you swap with 0. In the above
1139+
example, the answer would be `[1, 2, 5]`
1140+
1141+
1142+
The problem is NP-hard but the puzzles can all be solved with A* and an efficient representation.
11311143
"""
11321144

11331145
@staticmethod
@@ -1230,6 +1242,5 @@ def gen_random(self):
12301242

12311243

12321244
if __name__ == "__main__":
1233-
# SlidingPuzzle.sol(**SlidingPuzzle.get_example())
12341245
for problem in get_problems(globals()):
12351246
problem.test()

templates/codeforces.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,16 @@ def gen_random(self):
5858

5959
@register
6060
class SquareTiles(Problem):
61-
"""Find a minimal list of corner locations for a×a tiles that covers [0, m] × [0, n]
62-
and does not double-cover squares.
61+
"""Find a minimal list of corner locations for a×a tiles that covers [0, m] × [0, n] and does not double-cover
62+
squares.
6363
6464
Sample Input:
65-
6665
m = 10
67-
6866
n = 9
69-
7067
a = 5
71-
7268
target = 4
7369
7470
Sample Output:
75-
7671
[[0, 0], [0, 5], [5, 0], [5, 5]]
7772
7873
Inspired by [Codeforces Problem 1 A](https://codeforces.com/problemset/problem/1/A)
@@ -99,8 +94,8 @@ def gen_random(self):
9994
@register
10095
class EasyTwos(Problem):
10196
"""
102-
Given a list of lists of triples of integers, return True for each list with a total of at least 2 and False
103-
for each other list.
97+
Given a list of lists of triples of integers, return True for each list with a total of at least 2 and False for
98+
each other list.
10499
105100
Inspired by [Codeforces Problem 231 A](https://codeforces.com/problemset/problem/231/A)"""
106101

@@ -181,8 +176,8 @@ def gen_random(self):
181176

182177
@register
183178
class DominoTile(Problem):
184-
"""Tile an m x n checkerboard with 2 x 1 tiles. The solution is a list of fourtuples [i1, j1, i2, j2]
185-
with i2 == i1 and j2 == j1 + 1 or i2 == i1 + 1 and j2 == j1 with no overlap.
179+
"""Tile an m x n checkerboard with 2 x 1 tiles. The solution is a list of fourtuples [i1, j1, i2, j2] with i2 == i1
180+
and j2 == j1 + 1 or i2 == i1 + 1 and j2 == j1 with no overlap.
186181
187182
Inspired by [Codeforces Problem 50 A](https://codeforces.com/problemset/problem/50/A)
188183
"""
@@ -215,17 +210,14 @@ def gen_random(self):
215210
@register
216211
class IncDec(Problem):
217212
"""This straightforward problem is a little harder than the Codeforces one.
218-
Given a sequence of operations "++x",
219-
"x++", "--x", "x--", and a target value, find initial value so that the final value is the target value.
213+
Given a sequence of operations "++x", "x++", "--x", "x--", and a target value, find initial value so that the
214+
final value is the target value.
220215
221216
Sample Input:
222-
223217
ops = ["x++", "--x", "--x"]
224-
225218
target = 12
226219
227220
Sample Output:
228-
229221
13
230222
231223
Inspired by [Codeforces Problem 282 A](https://codeforces.com/problemset/problem/282/A)
@@ -295,13 +287,9 @@ class SlidingOne(Problem):
295287
"""We are given a 5x5 bimatrix with a single 1 like:
296288
297289
0 0 0 0 0
298-
299290
0 0 0 0 1
300-
301291
0 0 0 0 0
302-
303292
0 0 0 0 0
304-
305293
0 0 0 0 0
306294
307295
Find a (minimal) sequence of row and column swaps to move the 1 to the center. A move is a string
@@ -404,8 +392,8 @@ def gen_random(self):
404392

405393
@register
406394
class LongestSubsetString(Problem):
407-
"""You are given a string consisting of a's, b's and c's, find any longest substring containing no
408-
repeated consecutive characters.
395+
"""You are given a string consisting of a's, b's and c's, find any longest substring containing no repeated
396+
consecutive characters.
409397
410398
Sample Input:
411399
`"abbbc"`
@@ -442,13 +430,10 @@ class FindHomogeneousSubstring(Problem):
442430
all 0's or all 1's.
443431
444432
Sample Input:
445-
446433
s = 0000111111100000, k = 5
447434
448435
Sample Output:
449-
450436
4
451-
452437
(or 5 or 6 or 11)
453438
454439
Inspired by [Codeforces Problem 96 A](https://codeforces.com/problemset/problem/96/A)
@@ -516,13 +501,10 @@ class CombinationLock(Problem):
516501
"""Shortest Combination Lock Path
517502
518503
Given a starting a final lock position, find the (minimal) intermediate states, where each transition
519-
involves increasing or decreasing a single digit (mod 10)
520-
e.g.
504+
involves increasing or decreasing a single digit (mod 10), e.g.
521505
522506
start = "012"
523-
524507
combo = "329"
525-
526508
output: ['112', '212', '312', '322', '321', '320']
527509
528510
Inspired by [Codeforces Problem 540 A](https://codeforces.com/problemset/problem/540/A)

templates/number_theory.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def gen_random(self):
332332

333333

334334
@register
335-
class DiscreteLog(Problem):
335+
class DiscreteLog(Problem): # updated because the answer was given away in the docstring! OMG
336336
"""Discrete Log
337337
338338
The discrete logarithm problem is (given `g`, `t`, and `p`) to find n such that:
@@ -348,8 +348,6 @@ class DiscreteLog(Problem):
348348
349349
We include McCurley's discrete log challenge from
350350
[Weber D., Denny T. (1998) "The solution of McCurley's discrete log challenge."](https://link.springer.com/content/pdf/10.1007/BFb0055747.pdf)
351-
whose answer is
352-
`n = 325923617918270562238615985978623709128341338833721058543950813521768156295091638348030637920237175638117352442299234041658748471079911977497864301995972638266781162575370644813703762423329783129621567127479417280687495231463348812`
353351
"""
354352

355353
@staticmethod

0 commit comments

Comments
 (0)