Repository: srush/Transformer-Puzzles
Branch: main
Commit: 7178a69b363e
Files: 4
Total size: 115.6 KB
Directory structure:
gitextract_c6so92vp/
├── .gitignore
├── LICENSE
├── README.md
└── TransformerPuzzlers.ipynb
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2023 Sasha Rush
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# Transformer Puzzles
This notebook is a collection of short coding puzzles based on the internals of the Transformer. The puzzles are written in Python and can be done in this notebook. After completing these you will have a much better intutive sense of how a Transformer can compute certain logical operations.
These puzzles are based on [Thinking Like Transformers](https://arxiv.org/pdf/2106.06981.pdf) by Gail Weiss, Yoav Goldberg, Eran Yahav and derived from this [blog post](https://srush.github.io/raspy/).

## Goal
**Can we produce a Transformer that does basic elementary school addition?**
i.e. given a string "19492+23919" can we produce the correct output?
## Rules
Each exercise consists of a function with a argument `seq` and output `seq`. Like a transformer we cannot change length. Operations need to act on the entire sequence in parallel. There is a global `indices` which tells use the position in the sequence. If we want to do something different on certain positions we can use `where` like in Numpy or PyTorch. To run the seq we need to give it an initial input.
```python colab={"base_uri": "https://localhost:8080/", "height": 96} id="1b28dc98" outputId="f1ac1157-3db8-40c0-dbb2-7d9bad8943a0"
def even_vals(seq=tokens):
"Keep even positions, set odd positions to -1"
x = indices % 2
# Note that all operations broadcast so you can use scalars.
return where(x == 0, seq, -1)
seq = even_vals()
# Give the initial input tokens
seq.input([0,1,2,3,4])
```
The main operation you can use is "attention". You do this by defining a selector which forms a matrix based on `key` and `query`.
```python colab={"base_uri": "https://localhost:8080/", "height": 176} id="e2ee0ff8" outputId="a61ac19c-2550-4f3c-d653-50c323cdfd59"
before = key(indices) < query(indices)
before
```
We can combine selectors with logical operations.
```python colab={"base_uri": "https://localhost:8080/", "height": 201} id="c315ba6d" outputId="270d50fa-649c-438b-8606-d3d078478162"
before_or_same = before | (key(indices) == query(indices))
before_or_same
```
Once you have a selector, you can apply "attention" to sum over the grey positions. For example to compute cumulative such we run the following function.
```python colab={"base_uri": "https://localhost:8080/", "height": 326} id="e79c8c8b" outputId="44db7f90-502d-497c-c5ba-4062c09f0a9a"
def cumsum(seq=tokens):
return before_or_same.value(seq)
seq = cumsum()
seq.input([0, 1, 2, 3, 4])
```
Good luck!
================================================
FILE: TransformerPuzzlers.ipynb
================================================
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "f8117f57",
"metadata": {
"id": "f8117f57"
},
"source": [
"# Transformer Puzzles"
]
},
{
"cell_type": "markdown",
"id": "e9e822cb",
"metadata": {
"id": "e9e822cb"
},
"source": [
"This notebook is a collection of short coding puzzles based on the internals of the Transformer. The puzzles are written in Python and can be done in this notebook. After completing these you will have a much better intutive sense of how a Transformer can compute certain logical operations. \n",
"\n",
"These puzzles are based on [Thinking Like Transformers](https://arxiv.org/pdf/2106.06981.pdf) by Gail Weiss, Yoav Goldberg, Eran Yahav and derived from this [blog post](https://srush.github.io/raspy/)."
]
},
{
"cell_type": "markdown",
"id": "8e962052",
"metadata": {
"id": "8e962052"
},
"source": [
"## Goal\n",
"\n",
"**Can we produce a Transformer that does basic addition?**\n",
"\n",
"i.e. given a string \"19492+23919\" can we produce the correct output? "
]
},
{
"cell_type": "markdown",
"id": "d332140b",
"metadata": {
"id": "d332140b"
},
"source": [
"## Rules\n",
"\n",
"Each exercise consists of a function with a argument `seq` and output `seq`. Like a transformer we cannot change length. Operations need to act on the entire sequence in parallel. There is a global `indices` which tells use the position in the sequence. If we want to do something different on certain positions we can use `where` like in Numpy or PyTorch. To run the seq we need to give it an initial input. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c5c885a",
"metadata": {
"id": "6c5c885a"
},
"outputs": [],
"source": [
"%%capture\n",
"!pip install -qqq git+https://github.com/chalk-diagrams/chalk git+https://github.com/srush/RASPy "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51724e11",
"metadata": {
"id": "51724e11"
},
"outputs": [],
"source": [
"from IPython.display import display, HTML\n",
"from raspy import key, query, tokens, indices, where, draw\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b28dc98",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 96
},
"id": "1b28dc98",
"outputId": "f1ac1157-3db8-40c0-dbb2-7d9bad8943a0"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[0, -1, 2, -1, 4]"
],
"image/svg+xml": "\n"
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"def even_vals(seq=tokens):\n",
" \"Keep even positions, set odd positions to -1\"\n",
" x = indices % 2\n",
" # Note that all operations broadcast so you can use scalars.\n",
" return where(x == 0, seq, -1)\n",
"seq = even_vals()\n",
"\n",
"# Give the initial input tokens\n",
"seq.input([0,1,2,3,4])"
]
},
{
"cell_type": "markdown",
"id": "9dc23f88",
"metadata": {
"id": "9dc23f88"
},
"source": [
"The main operation you can use is \"attention\". You do this by defining a selector which forms a matrix based on `key` and `query`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e2ee0ff8",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 176
},
"id": "e2ee0ff8",
"outputId": "a61ac19c-2550-4f3c-d653-50c323cdfd59"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Selector Layer 0\n",
" 0 1 2 3 4\n",
" |h e l l o|\n",
" 0 1 2 3 4\n",
" |---------|\n",
"0 h 0 | 1 1 1 1\n",
"1 e 1 | 1 1 1\n",
"2 l 2 | 1 1\n",
"3 l 3 | 1\n",
"4 o 4 | "
],
"image/svg+xml": "\n"
},
"metadata": {},
"execution_count": 4
}
],
"source": [
"before = key(indices) < query(indices)\n",
"before"
]
},
{
"cell_type": "markdown",
"id": "a4de0a14",
"metadata": {
"id": "a4de0a14"
},
"source": [
"We can combine selectors with logical operations."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c315ba6d",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 201
},
"id": "c315ba6d",
"outputId": "270d50fa-649c-438b-8606-d3d078478162"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Selector Layer 0\n",
" 0 1 2 3 4\n",
" |h e l l o|\n",
" 0 1 2 3 4\n",
" 0 1 2 3 4\n",
" |---------|\n",
"0 h 0 | 1 1 1 1 1\n",
"1 e 1 | 1 1 1 1\n",
"2 l 2 | 1 1 1\n",
"3 l 3 | 1 1\n",
"4 o 4 | 1"
],
"image/svg+xml": "\n"
},
"metadata": {},
"execution_count": 5
}
],
"source": [
"before_or_same = before | (key(indices) == query(indices))\n",
"before_or_same"
]
},
{
"cell_type": "markdown",
"id": "00bc66a3",
"metadata": {
"id": "00bc66a3"
},
"source": [
"Once you have a selector, you can apply \"attention\" to sum over the grey positions. For example to compute cumulative such we run the following function. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e79c8c8b",
"metadata": {
"lines_to_next_cell": 1,
"colab": {
"base_uri": "https://localhost:8080/",
"height": 326
},
"id": "e79c8c8b",
"outputId": "44db7f90-502d-497c-c5ba-4062c09f0a9a"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[0, 1, 3, 6, 10]"
],
"image/svg+xml": "\n"
},
"metadata": {},
"execution_count": 6
}
],
"source": [
"def cumsum(seq=tokens):\n",
" return before_or_same.value(seq)\n",
"seq = cumsum()\n",
"seq.input([0, 1, 2, 3, 4])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ae57559",
"metadata": {
"cellView": "form",
"id": "9ae57559"
},
"outputs": [],
"source": [
"#@title Test Code (Collapse)\n",
"def atoi(seq=tokens):\n",
" return seq.map(lambda x: ord(x) - ord('0'))\n",
"\n",
"def test_output(user, spec, token_sets):\n",
" for ex_num, token_set in enumerate(token_sets): \n",
" out1 = user(*token_set[:-1])((token_set[-1]))\n",
" out2 = spec(*token_set)\n",
" print(f\"Example {ex_num}. Args:\", token_set, \"Expected:\", out2)\n",
" display(out1)\n",
" out1 = out1.toseq()\n",
" for i, o in enumerate(out2):\n",
" assert out1[i] == o, f\"Output: {out1} Expected: {out2}\"\n",
"\n",
" pups = [\n",
" \"2m78jPG\",\n",
" \"pn1e9TO\",\n",
" \"MQCIwzT\",\n",
" \"udLK6FS\",\n",
" \"ZNem5o3\",\n",
" \"DS2IZ6K\",\n",
" \"aydRUz8\",\n",
" \"MVUdQYK\",\n",
" \"kLvno0p\",\n",
" \"wScLiVz\",\n",
" \"Z0TII8i\",\n",
" \"F1SChho\",\n",
" \"9hRi2jN\",\n",
" \"lvzRF3W\",\n",
" \"fqHxOGI\",\n",
" \"1xeUYme\",\n",
" \"6tVqKyM\",\n",
" \"CCxZ6Wr\",\n",
" \"lMW0OPQ\",\n",
" \"wHVpHVG\",\n",
" \"Wj2PGRl\",\n",
" \"HlaTE8H\",\n",
" \"k5jALH0\",\n",
" \"3V37Hqr\",\n",
" \"Eq2uMTA\",\n",
" \"Vy9JShx\",\n",
" \"g9I2ZmK\",\n",
" \"Nu4RH7f\",\n",
" \"sWp0Dqd\",\n",
" \"bRKfspn\",\n",
" \"qawCMl5\",\n",
" \"2F6j2B4\",\n",
" \"fiJxCVA\",\n",
" \"pCAIlxD\",\n",
" \"zJx2skh\",\n",
" \"2Gdl1u7\",\n",
" \"aJJAY4c\",\n",
" \"ros6RLC\",\n",
" \"DKLBJh7\",\n",
" \"eyxH0Wc\",\n",
" \"rJEkEw4\"]\n",
" print(\"Success!\")\n",
" return HTML(\"\"\"\n",
" \n",
" \"\"\"%(random.sample(pups, 1)[0]))\n",
"SEQ = [2,1,3,2,4]\n",
"SEQ2 = [3, 4 ,3, -1, 2]"
]
},
{
"cell_type": "markdown",
"id": "57d753ac",
"metadata": {
"id": "57d753ac"
},
"source": [
"For each problem we will provide a Python specification. Your goal is to implement that specification with Transformers."
]
},
{
"cell_type": "markdown",
"id": "77441886",
"metadata": {
"id": "77441886"
},
"source": [
"### Challenge 0: Select the initial position\n",
"\n",
"Given a initial sequence compute a new sequence where all positions have the initial value. (1 line)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1da74d03",
"metadata": {
"id": "1da74d03"
},
"outputs": [],
"source": [
"def head_spec(seq):\n",
" return [seq[0] for _ in seq]\n",
"\n",
"def head(seq=tokens):\n",
" return (key(indices) == query(0)).value(seq)\n",
" \n",
"test_output(head, head_spec, [(SEQ,),(SEQ2,)])"
]
},
{
"cell_type": "markdown",
"id": "f565d61c",
"metadata": {
"id": "f565d61c"
},
"source": [
"### Challenge 1: Select a given index\n",
"\n",
"Produce a sequence where all the elements have the value at index `i`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10d1b909",
"metadata": {
"lines_to_next_cell": 1,
"id": "10d1b909"
},
"outputs": [],
"source": [
"def index_spec(i, seq):\n",
" return [seq[i] for _ in seq]\n",
"\n",
"def index(i, seq=tokens):\n",
" raise NotImplementedError\n",
"\n",
"test_output(index, index_spec, [(2, SEQ), (3, SEQ2), (1, SEQ)])"
]
},
{
"cell_type": "markdown",
"id": "a6c2b47d",
"metadata": {
"id": "a6c2b47d"
},
"source": [
"### Challenge 2: Shift\n",
"\n",
"Shift all of the tokens in a sequence to the right by `i` positions filling in the values with `default`. (1 line)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9b5db79",
"metadata": {
"lines_to_next_cell": 1,
"id": "d9b5db79"
},
"outputs": [],
"source": [
"def shift_spec(i, default=\"0\", seq=None):\n",
" return [default]*i + [s for j, s in enumerate(seq) if j < len(seq) - i]\n",
"\n",
"def shift(i, default=\"0\", seq=tokens):\n",
" raise NotImplementedError\n",
"\n",
"test_output(shift, shift_spec, [(2, 0, SEQ), (3, 0, SEQ2), (1, 0, SEQ)])"
]
},
{
"cell_type": "markdown",
"id": "3f87e538",
"metadata": {
"lines_to_next_cell": 2,
"id": "3f87e538"
},
"source": [
"### Challenge 3: Right Align\n",
"\n",
"Right align a padded sequence e.g. ralign().inputs('xyz___') = '000xyz'\" (3 layers) (2 lines)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a841d6f4",
"metadata": {
"lines_to_next_cell": 1,
"id": "a841d6f4"
},
"outputs": [],
"source": [
"def ralign_spec(ldefault=\"0\", seq=tokens):\n",
" last = None\n",
" for i in range(len(seq)-1, -1, -1):\n",
" if seq[i] == \"_\":\n",
" last = i\n",
" else:\n",
" break\n",
" if last == None:\n",
" return seq\n",
" return [ldefault] * (len(seq) - last) + seq[:last]\n",
"\n",
"def ralign(ldefault=\"0\", seq=tokens):\n",
" raise NotImplementedError\n",
"\n",
"test_output(ralign, ralign_spec, [(\"-\", list(\"xyzabc__\"),), (\"0\", list(\"xyz___\"),)])"
]
},
{
"cell_type": "markdown",
"id": "a178203a",
"metadata": {
"id": "a178203a"
},
"source": [
"### Challenge 4: Split\n",
"\n",
"Split a sequence on a value. Get the first or second part. Right align. (5 lines)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e69995ed",
"metadata": {
"lines_to_next_cell": 1,
"id": "e69995ed"
},
"outputs": [],
"source": [
"def split_spec(v, get_first_part, seq):\n",
" out = []\n",
" mid = False\n",
" blank = \"0\" if not get_first_part else \"_\"\n",
" for j, s in enumerate(seq):\n",
" if s == v:\n",
" out.append(blank)\n",
" mid = True\n",
" elif (get_first_part and not mid) or (not get_first_part and mid):\n",
" out.append(s)\n",
" else:\n",
" out.append(blank)\n",
" return ralign_spec(\"0\", seq=out)\n",
"\n",
"def split(v, get_first_part, seq=tokens):\n",
" raise NotImplementedError\n",
"\n",
"test_output(split, split_spec,\n",
" [(\"-\", 1, list(\"xyz-ax\"),),\n",
" (\"-\", 0, list(\"xyz-ax\"),),\n",
" (\"+\", 0, list(\"xy+z-ax\"),)]\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "f9d19ecd",
"metadata": {
"id": "f9d19ecd"
},
"source": [
"### Challenge 5: Minimum \n",
"\n",
"Compute the minimum value of the sequence. This one starts to get harder! (5 lines of code)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53b19ac8",
"metadata": {
"lines_to_next_cell": 1,
"id": "53b19ac8"
},
"outputs": [],
"source": [
"def minimum_spec(seq):\n",
" m = min(seq)\n",
" return [m for _ in seq]\n",
"\n",
"def minimum(seq=tokens):\n",
" raise NotImplementedError\n",
"\n",
"test_output(minimum, minimum_spec, [(SEQ,), (SEQ2,), ([2, 1, 1],)])"
]
},
{
"cell_type": "markdown",
"id": "6d2fe8ec",
"metadata": {
"id": "6d2fe8ec"
},
"source": [
"### Challenge 6: First Index\n",
"\n",
"Compute the first index that has token `token`. (1 line)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4a6a030",
"metadata": {
"lines_to_next_cell": 1,
"id": "a4a6a030"
},
"outputs": [],
"source": [
"def first_spec(token, seq):\n",
" first = None\n",
" for i, s in enumerate(seq):\n",
" if s == token and first is None:\n",
" first = i\n",
" return [first for _ in seq]\n",
"\n",
"def first(token, seq=tokens):\n",
" raise NotImplementedError\n",
"\n",
"test_output(first, first_spec, [(3, SEQ), (-1, SEQ2), ('l', list('hello'))])"
]
},
{
"cell_type": "markdown",
"id": "38236f00",
"metadata": {
"id": "38236f00"
},
"source": [
"### Challenge 7: Slide\n",
"\n",
"Replace special tokens \"<\" with the closest non \"<\" value to their right. (4 lines of code)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7781c97c",
"metadata": {
"id": "7781c97c"
},
"outputs": [],
"source": [
"def slide_spec(match, seq):\n",
" out = []\n",
" for i, s in enumerate(seq):\n",
" if s == \"<\":\n",
" for v in seq[i+1:]:\n",
" if v != \"<\":\n",
" out.append(v)\n",
" break\n",
" else:\n",
" out.append(s)\n",
" return out\n",
"\n",
"def slide(match=\"<\", seq=tokens):\n",
" raise NotImplementedError\n",
"\n",
"test_output(slide, slide_spec,\n",
" [(\"<\", list(\"1<<2\"),),\n",
" (\"<\", list(\"2<<<3\"),),\n",
" (\"<\", list(\"3<<<1<<3\"),)]\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "6be44511",
"metadata": {
"id": "6be44511"
},
"source": [
"### Final Challenge: Adder\n",
"\n",
"Now we will put everything together. Here are the steps. \n",
"\n",
"add().input(\"683+345\")\n",
"\n",
"0) Split into parts. Convert to ints. Add\n",
"\n",
"> \"683+345\" => [0, 0, 0, 9, 12, 8]\n",
"\n",
"1) Compute the carry terms. Three possibilities: 1 has carry, 0 no carry, < maybe has carry. \n",
"\n",
"> [0, 0, 0, 9, 12, 8] => \"00<100\"\n",
"\n",
"2) Slide the carry coefficients\n",
"\n",
"> \"00<100\" => 001100\"\n",
"\n",
"3) Complete the addition.\n",
"\n",
"Each of these is 1 line of code. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11b27d5b",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 96
},
"id": "11b27d5b",
"outputId": "b1224ef6-c889-40ab-f051-fd00e37ceb2a"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 3, 2, 1]"
],
"image/svg+xml": "\n"
},
"metadata": {},
"execution_count": 32
}
],
"source": [
"# The function atoi lets us convert from string to sequences of integers\n",
"atoi(tokens).input(\"1321\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3370d2c8",
"metadata": {
"id": "3370d2c8"
},
"outputs": [],
"source": [
"def add_spec(seq):\n",
" a, b = \"\".join(seq).split(\"+\")\n",
" c = int(a) + int(b)\n",
" out = f\"{c}\"\n",
" return list(map(int, list((\"0\" * (len(seq) - len(out))) + out)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "98a331c6",
"metadata": {
"id": "98a331c6"
},
"outputs": [],
"source": [
"def add(seq=tokens):\n",
" x = atoi(split(\"+\", True, seq)) \\\n",
" + atoi(split(\"+\", False, seq))\n",
" # 1) Check for carries \n",
" gets_carry = shift(-1, \"0\", where(x > 9, \"1\", where(x == 9, \"<\", \"0\")))\n",
" # 2) Slide carries to their columns - all in one parallel go! \n",
" gets_carry = atoi(slide(\"<\", gets_carry))\n",
" # 3) Add in carries, and remove overflow from original addition. \n",
" return (x + gets_carry) % 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f1f708c",
"metadata": {
"id": "7f1f708c"
},
"outputs": [],
"source": [
"test_output(add, add_spec,\n",
" [(list(\"1+2\"),),\n",
" (list(\"22+384\"),),\n",
" (list(\"3+10\"),)]\n",
" )"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "raspy",
"language": "python",
"name": "raspy"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}