-
Notifications
You must be signed in to change notification settings - Fork 1
/
genNotebooks.py
223 lines (196 loc) · 7.91 KB
/
genNotebooks.py
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
from bs4 import BeautifulSoup, SoupStrainer, NavigableString
from urllib.parse import urljoin
import textwrap
import requests
import re
import json
import requests_cache
requests_cache.install_cache( "dev_cache", allowable_methods=("GET", "POST") );
baseurl = "https://codingbat.com/java";
response = requests.get( baseurl );
"""
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Warmup-1 > sleepIn\n",
"\n",
"```\n",
"The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in.\n",
"\n",
"\n",
"sleepIn(false, false) → true\n",
"sleepIn(true, false) → false\n",
"sleepIn(false, true) → true\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bool sleepIn(bool weekday, bool vacation)\n",
"{\n",
" return true;\n",
"}\n",
"\n",
"// tests:\n",
"printTest( \"sleepIn(false, false) → true\", sleepIn(false, false), true );\n",
"printTest( \"sleepIn(true, false) → false\", sleepIn(true, false), false );\n",
"printTest( \"sleepIn(false, true) → true\", sleepIn(false, true), true );\n",
"printTest( \"sleepIn(true, true) → true\", sleepIn(true, true), true );"
]
}
"""
nbStub = \
"""
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\\n",
"#include <iomanip>\\n",
"#include <vector>\\n",
"\\n",
"using namespace std;"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"namespace testns { template<class A> void printTest( string str, A run, A expected )\\n",
"{\\n",
" if( run == expected )\\n",
" cout << boolalpha << \\"\\\\033[1;32m\\"\\n",
" << \\"Expected: \\" << setw(60) << left << str << \\" Run: \\" << setw(20) << left << run << \\"\\\\t\\\\tOK\\"\\n",
" << \\"\\\\033[0m\\" << endl;\\n",
" else\\n",
" cerr << boolalpha << \\"\\\\033[1;31m\\"\\n",
" << \\"Expected: \\" << setw(60) << left << str << \\" Run: \\" << setw(20) << left << run << \\"\\\\t\\\\tX\\"\\n",
" << \\"\\\\033[0m\\" << endl;\\n",
"} } using namespace testns;\\n",
"void printTest( string str, string run, const char* expected ){ printTest( str, \\"\\\\\\"\\"+run+\\"\\\\\\"\\", \\"\\\\\\"\\"+string( expected )+\\"\\\\\\"\\" ); }\\n",
"template<class A> void printTest( string str, vector<A> run, vector<A> expected )\\n",
"{\\n",
" string runStr = \\"[\\"; for( A a : run ) runStr += to_string( a ) + \\", \\";\\n",
" if( runStr.length() > 1 ) runStr = runStr.substr(0, runStr.length()-2); runStr += \\"]\\";\\n",
" string expStr = \\"[\\"; for( A a : expected ) expStr += to_string( a ) + \\", \\";\\n",
" if( expStr.length() > 1 ) expStr = expStr.substr(0, expStr.length()-2); expStr += \\"]\\";\\n",
" printTest( str, runStr, expStr );\\n",
"}"
]
},
CODINGBAT_PROBLEMS_HERE
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
""";
i = 0;
for link in BeautifulSoup(response.text, parse_only=SoupStrainer('a'), features="html.parser"):
if link.has_attr('href') and str.startswith( link[ "href" ], "/java/" ):
print(link['href'])
out = "";
resp = requests.get( urljoin( baseurl, link[ "href" ] ) );
for problem in BeautifulSoup( resp.text, parse_only=SoupStrainer( "a" ), features="html.parser" ):
if problem.has_attr( "href" ) and str.startswith( problem[ "href" ], "/prob/" ):
print( "\t", problem["href"] );
r = requests.get( urljoin( baseurl, problem[ "href" ] ) );
bs = BeautifulSoup( r.text, features="html.parser" );
problemInfo = [];
el = bs.select_one( ".go" ).parent.previousSibling;
while el != None:
if not isinstance(el, NavigableString) and el.has_attr( "class" ) and "minh" in el[ "class" ]: problemInfo.insert( 0, "" );
problemInfo.insert( 0, textwrap.fill( el.string or el.getText(), 80 ) );
el = el.previousSibling;
problemStatement = "\n".join( problemInfo ).replace( "\n\n", "\n" );
if problemStatement.count( "'" ) % 2 == 1: problemStatement = problemStatement.replace( "'", "ʼ" );
templateCode = bs.select_one( "#ace_div" ).string;
dummyRets = {
"int": "0",
"boolean": "false",
"String": "\"\"",
"int[]": "new int[]{}",
"String[]": "new String[]{}",
"List": "null"
}
retType = ( re.search( r"public (.*?) \w+\(", templateCode ) or re.search( r"^(.*?) \w+\(", templateCode )).group( 1 );
# retType = dummyRets[ retType ];
if retType == "int": dummyRet = "0";
elif retType == "boolean": dummyRet = "false";
elif retType == "String": dummyRet = "\"\"";
elif m:=re.match( r"(.*)\[\]", retType ): dummyRet = "new " + m.group() + "{}";
elif retType == "List": dummyRet = "null";
elif m:=re.match( r"List<(.*)>", retType ): dummyRet = "new List<>()"
else:
print( "\t\t\tUNHANDLED RET TYPE", retType );
dummyRet = "null";
dummySubmittableJava = templateCode.replace( " \n}", " return " + dummyRet + ";\n}" );
testsResp = requests.post( "https://codingbat.com/run", data={ "id": problem["href"][len("/prob/"):], "code": dummySubmittableJava } );
tests = [ test.select_one( "td" ).string for test in BeautifulSoup( testsResp.text, parse_only=SoupStrainer( "tr" ), features="html.parser" ) if test.select_one( "td" ) ];
celljson = {
"cell_type": "markdown",
"metadata": {}
}
problemTitle = bs.select_one( "span.h2" ).string \
+ bs.select_one( "span.h2" ).parent.nextSibling.string \
+ bs.select_one( "span.h2" ).parent.nextSibling.nextSibling.string;
problemMDSource = f"# {problemTitle}";
celljson[ "source" ] = [ line + "\n" for line in problemMDSource.split( "\n" ) ];
celljson[ "source" ][-1] = celljson[ "source" ][-1][:-1];
out += json.dumps( celljson ) + ",\n";
celljson = {
"cell_type": "code",
"execution_count": None,
"metadata": {},
"outputs": [],
};
cppSource = f"/*\n{problemStatement}\n*/\n\n"
dummyCpp = dummySubmittableJava \
.replace( "public ", "" ) \
.replace( "boolean", "bool" ) \
.replace( "Integer", "int" ) \
.replace( "List", "vector" );
dummyCpp = re.sub( r"(?<!\w)String", "string", dummyCpp );
dummyCpp = dummyCpp.replace( "return new int[]{};", "return vector<int>{};" );
dummyCpp = dummyCpp.replace( "return new vector<>();", "return {}" );
# dummyCpp = dummyCpp.replace( "return null;", "return NULL;" );
dummyCpp = re.sub( r"(\w+)\[\]", "vector<\\1>", dummyCpp );
dummyCpp = re.sub( r"^vector<(.*?)>", "std::vector<\\1>", dummyCpp, flags=re.M );
cppSource += dummyCpp;
cppSource += "\n\n// tests:\n";
for test in tests:
if not " → " in test: continue;
testCall = test.split( " → " )[0].replace("[", "{").replace("]", "}");
testRet = test.split( " → " )[1].replace("[", "{").replace("]", "}");
test = test.replace( "\"", "\\\"" );
cppSource += f"printTest( \"{test}\", {testCall}, {testRet} );\n";
celljson[ "source" ] = [ line + "\n" for line in cppSource.split( "\n" )[:-1] ];
celljson[ "source" ][-1] = celljson[ "source" ][-1][:-1];
out += json.dumps( celljson ) + ",\n";
f = open( f"cpp-codingbat-{str(i+1).zfill(2)}-{link['href'][len('/java/'):]}.ipynb", "w" );
f.write( nbStub.replace( "CODINGBAT_PROBLEMS_HERE", out[:-2] ) );
i += 1;