Skip to content

Added swap variables notebook #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 248 additions & 0 deletions notebooks/how_to_swap_variables.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "CE6ac5aIsb5K"
},
"source": [
"# How to Swap Variables Source Code\n",
"by Atharva Aher for the [How to Swap Variables](https://therenegadecoder.com/code/how-to-swap-variables-in-python/) article"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yitmP7ONsq6K"
},
"source": [
"## Solutions"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DdrKy6u5sxw6"
},
"source": [
"Here, we'll look at all the solutions from the original article."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JNcqYrx9sWS5",
"outputId": "bd09165c-5b43-4ce7-a2b9-8bf4e8145b0f"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"palette_1: barta\n",
"palette_2: foie\n"
]
}
],
"source": [
"# Swap Elements with a Temporary Variable\n",
"palette_1 = \"foie\"\n",
"palette_2 = \"barta\"\n",
"\n",
"temp = palette_1\n",
"palette_1 = palette_2\n",
"palette_2 =temp\n",
"\n",
"print('palette_1:', palette_1)\n",
"print('palette_2:', palette_2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mvS-Z2UYtE-T",
"outputId": "7ba8b187-69f5-4fd2-8723-589a2c54f197"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"palette_1: barta\n",
"palette_2: foie\n"
]
}
],
"source": [
"# Swap Elements with Iterable Unpacking\n",
"palette_1 = \"foie\"\n",
"palette_2 = \"barta\"\n",
"\n",
"palette_1, palette_2 = palette_2, palette_1\n",
"\n",
"print('palette_1:', palette_1)\n",
"print('palette_2:', palette_2)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1B57ydaftMrB",
"outputId": "c39a9e31-ffd9-43c6-8cdf-df45234da7ff"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"palette_1: barta\n",
"palette_2: foie\n"
]
}
],
"source": [
"palette_1 = \"foie\"\n",
"palette_2 = \"barta\"\n",
"\n",
"temp = palette_2, palette_1 # creates a tuple from the two palettes\n",
"palette_1, palette_2 = temp # unpacks the tuple into the two palettes\n",
"\n",
"print('palette_1:', palette_1)\n",
"print('palette_2:', palette_2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ECfsN4ZTtSES"
},
"source": [
"## Performance"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NtKAq18ptWwK"
},
"source": [
"Here, we'll look at the performance code from the original article."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "IGEB5JsctTzL"
},
"outputs": [],
"source": [
"# Import performance testing library\n",
"import timeit\n",
"\n",
"# Generate test strings\n",
"setup = \"\"\"\n",
"palette_1 = \"foie\"\n",
"palette_2 = \"barta\"\n",
"\"\"\"\n",
"temp = \"\"\"\n",
"_ = palette_1\n",
"palette_1 = palette_2\n",
"palette_2 = _\n",
"\"\"\"\n",
"unpack = \"\"\"\n",
"palette_1, palette_2 = palette_2, palette_1\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "c5F_PEXDttQz",
"outputId": "e1d5ecf7-3d73-4d0c-9079-04eb4b97b979"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.018495057999984965"
]
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"min(timeit.repeat(setup=setup, stmt=temp))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "zhNyN7w3twLb",
"outputId": "1a63233c-e2bf-4d90-8edc-d98d2917f87f"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.016565159000037966"
]
},
"metadata": {},
"execution_count": 12
}
],
"source": [
"min(timeit.repeat(setup=setup, stmt=unpack))"
]
}
],
"metadata": {
"colab": {
"name": "how-to-write-a-loop.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading