Skip to content

Instantly share code, notes, and snippets.

@alchzh
Last active September 15, 2020 20:26
Show Gist options
  • Save alchzh/85ab9e3d340f44718b35946b7632dfe6 to your computer and use it in GitHub Desktop.
Save alchzh/85ab9e3d340f44718b35946b7632dfe6 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<module 'golfing' from '/home/jovyan/golfing.py'>"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import golfing\n",
"import importlib\n",
"\n",
"importlib.reload(golfing)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"colab_type": "code",
"id": "ouTekh8-rIva",
"outputId": "5b7a9a5e-3087-43e9-b91e-17b75f441d48"
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "43e77e14ed6b4cfebb49edc2f48d13a9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='<h2><code>finalQuizScore</code>: <b>141</b> characters</h2>')"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "09280367f56e45b5a3f3bac29d6c949c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(VBox(children=(HTML(value='<style>.golf-input { font-family: monospace; height: 200px; }</style…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%golf finalQuizScore -t float -s ','\n",
"finalQuizScore=lambda a,b,c,d,e:(\"Y\",\"Sorry, y\")[(f:=900-int(7/2*b+3*a+c+d+e/2))>100]+f\"ou would need a {f}% on the final exam to get an A-.\"\n",
"\n",
"# --INPUT--\n",
"# 90, 90, 90, 90, 90\n",
"# 100, 100, 100, 100, 100\n",
"# 80, 80, 80, 80, 80\n",
"# 81, 83, 85, 87, 90.5\n",
"# 100, 100, 50, 50, 98"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8c0edcf931714c7ead1d3a33d472e4a6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='<h2><code>addQuotes</code>: <b>74</b> characters</h2>')"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b79cd78b9df445d1a222ec6b073b62af",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(VBox(children=(HTML(value='<style>.golf-input { font-family: monospace; height: 200px; }</style…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%golf addQuotes\n",
"addQuotes=lambda s:\"\".join(((s==1)^(s:='`'<c<'{'))*'\"'+c for c in s)+'\"'*s\n",
"\n",
"\n",
"# --INPUT--\n",
"# [[one, two, three]]\n",
"# this is a test\n",
"# test\n",
"# this \"one\" \"contains' \"quotations\n",
"# Hello SpeciaL!\n",
"# NO CHANGE"
]
}
],
"metadata": {
"colab": {
"name": "Code Golf.ipynb",
"provenance": [],
"toc_visible": true
},
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
import ipywidgets as widgets
from IPython.display import display
from IPython.core import magic_arguments
from IPython.core.magic import cell_magic, magics_class, Magics
from inspect import signature
def test_golf(f, default_input="", arg_count=1, separator=None, by_line=False, converter=str):
stylesheet = widgets.HTML(value="<style>.golf-input { font-family: monospace; height: 200px; }</style>")
input_area = widgets.Textarea(value=default_input)
input_area.add_class("golf-input")
button = widgets.Button(description='Test')
output = widgets.Output()
def run_line(line):
args = line.strip().split(separator, arg_count-1)
print(f(*(converter(arg) for arg in args)))
def run(b):
output.clear_output()
with output:
input = input_area.value
if by_line:
for line in input.split("\n"):
run_line(line)
else:
run_line(input)
button.on_click(run)
box = widgets.HBox([widgets.VBox([stylesheet, input_area]), widgets.VBox([button, output])])
display(box)
run(button)
@magics_class
class GolfMagics(Magics):
@magic_arguments.magic_arguments()
@magic_arguments.argument(
'function', metavar='function', type=str,
help='the name of the golf function'
)
@magic_arguments.argument(
'-s', '--separator', default="None",
help="""Separator for function parameters."""
)
@magic_arguments.argument(
'-w', '--whole', action="store_false",
help="""Whether to process whole input at once."""
)
@magic_arguments.argument(
'-t', '--type', default="str",
help="""What type to convert parameters to."""
)
@cell_magic
def golf(self, line="", cell=None):
idx = cell.find("# --INPUT--\n")
if idx == -1:
code = cell
default_input = ""
else:
code = cell[:idx]
default_input = "\n".join(line.lstrip("# ") for line in cell[idx+12:].strip().split("\n"))
self.shell.ex(code)
args = magic_arguments.parse_argstring(self.golf, line)
f = self.shell.user_ns[args.function]
display(widgets.HTML(value=f"<h2><code>{args.function}</code>: <b>{len(code.strip())}</b> characters</h2>"))
test_golf(
f,
default_input=default_input,
separator=self.shell.ev(args.separator),
by_line=args.whole,
converter=self.shell.ev(args.type),
arg_count=len(signature(f).parameters)
)
ip = get_ipython()
ip.register_magics(GolfMagics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment