Add files via upload

This commit is contained in:
Peter Norvig
2022-10-29 13:12:24 -07:00
committed by GitHub
parent c4856ab03f
commit d873ed007e

View File

@@ -164,7 +164,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Finally, here's a third version. It has a very straightforward approach: generate all possible output strings for the source, and then check if the target its one of the possible outputs. To generate output strings, scan left-to-right, and for every character consider both the possibility of typing the character and of replacing it by backspace, maintaining a set of all possible outputs at each step.\n", "Finally, below is a third version. It has a very straightforward approach: generate all possible output strings for the source, and then check if the target its one of the possible outputs. To generate output strings, scan left-to-right, and for every character consider both the possibility of typing the character and of replacing it by backspace, maintaining a set of all possible outputs at each step.\n",
"\n", "\n",
"If this version of the program gives the same results as a more complicated version, that gives us added confidence that all the assumptions made by the complicated version are valid, because this version does not make those assumptions." "If this version of the program gives the same results as a more complicated version, that gives us added confidence that all the assumptions made by the complicated version are valid, because this version does not make those assumptions."
] ]
@@ -182,7 +182,7 @@
" return target in possible_outputs(source)\n", " return target in possible_outputs(source)\n",
"\n", "\n",
"def possible_outputs(source: str) -> set:\n", "def possible_outputs(source: str) -> set:\n",
" \"\"\"All outputs that can be generated by \"\"\"\n", " \"\"\"All outputs that can be generated by replacing characters in `source` with `backspace`.\"\"\"\n",
" outputs = {''}\n", " outputs = {''}\n",
" for c in source:\n", " for c in source:\n",
" outputs = union({out + c, out[:-1]} for out in outputs)\n", " outputs = union({out + c, out[:-1]} for out in outputs)\n",