Created
April 17, 2021 19:05
-
-
Save norweeg/68933e4db198b722527ac49775c32f1c to your computer and use it in GitHub Desktop.
square of integers.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "square of integers.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMqPTa1Cyv6ksHsCqWoUkUX", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/norweeg/68933e4db198b722527ac49775c32f1c/square-of-integers.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_gbEjYPVJxaW" | |
}, | |
"source": [ | |
"from ipywidgets import interact, BoundedIntText\n", | |
"from IPython.display import Pretty\n", | |
"from itertools import chain, repeat, islice\n", | |
"\n", | |
"@interact(n=BoundedIntText(description=\"n = \", min=1, value=5))\n", | |
"def number_square(n):\n", | |
" rows = (\n", | |
" lambda: map(\n", | |
" lambda i: chain(\n", | |
" range(1,i), \n", | |
" repeat(i, ((2*n)-1)-(2*(i-1))), \n", | |
" reversed(range(1, i))\n", | |
" ), \n", | |
" range(1, n+1)\n", | |
" )\n", | |
" )\n", | |
" \n", | |
" as_str = lambda i: \" \".join(map(lambda s: str(s).ljust(len(str(n))), i))\n", | |
" mirrored = lambda i: islice(reversed(list(i)), 1, None)\n", | |
" square = chain(rows(), mirrored(rows()))\n", | |
"\n", | |
" return Pretty(\"\\n\".join(map(as_str, square)))" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "rX7wKDoq316L" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment