Created
November 7, 2019 13:06
-
-
Save smartm13/fa1807b3aa1d4e10df5d4118a0f017e1 to your computer and use it in GitHub Desktop.
Trying new python 3.8 Walrus operator in a leetcode problem to solve using one-liner
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'3.8.0 (default, Nov 6 2019, 21:49:08) \\n[GCC 7.3.0]'" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import sys\n", | |
"sys.version" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### welcoming walrus:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def expensive_operation(n):\n", | |
" return sum([i**i for i in n])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"289\n" | |
] | |
} | |
], | |
"source": [ | |
"if (r:=expensive_operation(range(5)))>30:\n", | |
" print(r)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**leetcode array transfromation problem statement**: [link](https://leetcode.com/problems/array-transformation/) \n", | |
"To increment/decrement element towards its neightbours till convergance of the array" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Simple solution:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def transform(arr):\n", | |
" maper=lambda t:t[1]+(-1*int(bool(t[1]>t[0])) if t[1]>t[2] else int(bool(t[1]<t[0] and t[1]!=t[2])))\n", | |
" return arr[:1]+list(map(maper,zip(arr[:],arr[1:],arr[2:])))+arr[-1:] " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#transform=lambda arr:arr[:1]+list(map(lambda t:t[1]+(-1*int(bool(t[1]>t[0])) if t[1]>t[2] else int(bool(t[1]<t[0] and t[1]!=t[2]))),zip(arr[:],arr[1:],arr[2:])))+arr[-1:]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"t=[0,10,30,20,10,20,10,5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"pass:t=[0, 10, 29, 20, 11, 19, 10, 5]\n", | |
"pass:t=[0, 10, 28, 20, 12, 18, 10, 5]\n", | |
"pass:t=[0, 10, 27, 20, 13, 17, 10, 5]\n", | |
"pass:t=[0, 10, 26, 20, 14, 16, 10, 5]\n", | |
"pass:t=[0, 10, 25, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 24, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 23, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 22, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 21, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 20, 20, 15, 15, 10, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"while t!=(t:=transform(t)):\n", | |
" print(f\"pass:{t=}\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### reducing to one liner:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"pass:t=[0, 10, 29, 20, 11, 19, 10, 5]\n", | |
"pass:t=[0, 10, 28, 20, 12, 18, 10, 5]\n", | |
"pass:t=[0, 10, 27, 20, 13, 17, 10, 5]\n", | |
"pass:t=[0, 10, 26, 20, 14, 16, 10, 5]\n", | |
"pass:t=[0, 10, 25, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 24, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 23, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 22, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 21, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 20, 20, 15, 15, 10, 5]\n", | |
"[0, 10, 20, 20, 15, 15, 10, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"#readable level 1:\n", | |
"t=[0,10,30,20,10,20,10,5]\n", | |
"def transform(arr):\n", | |
" maper=lambda t:t[1]+(-1*int(bool(t[1]>t[0])) if t[1]>t[2] else int(bool(t[1]<t[0] and t[1]!=t[2])))\n", | |
" return arr[:1]+list(map(maper,zip(arr[:],arr[1:],arr[2:])))+arr[-1:] \n", | |
"while t!=(t:=transform(t)):\n", | |
" print(f\"pass:{t=}\")\n", | |
"print(t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"pass:t=[0, 10, 29, 20, 11, 19, 10, 5]\n", | |
"pass:t=[0, 10, 28, 20, 12, 18, 10, 5]\n", | |
"pass:t=[0, 10, 27, 20, 13, 17, 10, 5]\n", | |
"pass:t=[0, 10, 26, 20, 14, 16, 10, 5]\n", | |
"pass:t=[0, 10, 25, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 24, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 23, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 22, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 21, 20, 15, 15, 10, 5]\n", | |
"pass:t=[0, 10, 20, 20, 15, 15, 10, 5]\n", | |
"[0, 10, 20, 20, 15, 15, 10, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"#readable level 0.5:\n", | |
"t=[0,10,30,20,10,20,10,5]\n", | |
"transform=lambda arr:arr[:1]+list(map(lambda t:t[1]+(-1*int(bool(t[1]>t[0])) if t[1]>t[2] else int(bool(t[1]<t[0] and t[1]!=t[2]))),zip(arr[:],arr[1:],arr[2:])))+arr[-1:]\n", | |
"while t!=(t:=transform(t)):\n", | |
" print(f\"pass:{t=}\")\n", | |
"print(t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 10, 20, 20, 15, 15, 10, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"#readable level 0:\n", | |
"t=[0,10,30,20,10,20,10,5]\n", | |
"while t!=(t:=(lambda arr:arr[:1]+list(map(lambda t:t[1]+(-1*int(bool(t[1]>t[0])) if t[1]>t[2] else int(bool(t[1]<t[0] and t[1]!=t[2]))),zip(arr[:],arr[1:],arr[2:])))+arr[-1:])(t)):\n", | |
" pass#print(f\"pass:{t=}\")\n", | |
"print(t)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# ** **drum rolls** **" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 10, 20, 20, 15, 15, 10, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"#readable level final: --the one-liner--\n", | |
"t=[0,10,30,20,10,20,10,5]\n", | |
"while t!=(t:=(lambda arr:arr[:1]+list(map(lambda t:t[1]+(-1*int(bool(t[1]>t[0])) if t[1]>t[2] else int(bool(t[1]<t[0] and t[1]!=t[2]))),zip(arr[:],arr[1:],arr[2:])))+arr[-1:])(t)):pass\n", | |
"print(t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "py38", | |
"language": "python", | |
"name": "py38" | |
}, | |
"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.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment