Skip to content

Instantly share code, notes, and snippets.

@lorinc
Last active September 29, 2016 23:32
Show Gist options
  • Save lorinc/c754321e8e3cfe77dc7d90b1051466ed to your computer and use it in GitHub Desktop.
Save lorinc/c754321e8e3cfe77dc7d90b1051466ed to your computer and use it in GitHub Desktop.
We asked this as a Java interview question and I HAD to show them the beauty of Python ;)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{{{[{}{}{}]()}()}[{}{}{}]}]\n",
"[{{{[]()}()}[]}]\n",
"[{{{()}()}}]\n",
"[{{{}}}]\n",
"[{{}}]\n",
"[{}]\n",
"[]\n",
"\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"import re\n",
"\n",
"# check this text for parenthesis error, neglect other chars\n",
"text = '[{a{b{\\n[\\t{ü}^C{}{}]()}()}[\\t{ü}^C{}{}]}]'\n",
"\n",
"r = '[\\[\\{\\(\\)\\}\\]]'\n",
"s = ''.join(re.findall(r, text))\n",
"ps = ['[]', '()', '{}']\n",
"\n",
"for _ in range(int(len(s)/2)):\n",
" print(s)\n",
" s = min([s.replace(t,'') for t in ps], key=len)\n",
"\n",
"assert len(s) == 0, 'Could not break down ' + s"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "NB-VENV Python3",
"language": "python",
"name": "nb-venv-py3"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment