Skip to content

Instantly share code, notes, and snippets.

@jittat
Created March 7, 2019 07:53
Show Gist options
  • Save jittat/495c40f6923b14d6510c8a5b8bb976ec to your computer and use it in GitHub Desktop.
Save jittat/495c40f6923b14d6510c8a5b8bb976ec to your computer and use it in GitHub Desktop.
fun python example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def mymerge(lst1, lst2):\n",
" \"\"\"\n",
" >>> mymerge([1,2,5,6,10],[2,3,4,5,8,9,12])\n",
" [1, 2, 2, 3, 4, 5, 5, 6, 8, 9, 10, 12]\n",
" \"\"\"\n",
" if lst1 == []:\n",
" return lst2\n",
" if lst2 == []:\n",
" return lst1\n",
" if lst1[0] <= lst2[0]:\n",
" return [lst1[0]] + mymerge(lst1[1:], lst2)\n",
" else:\n",
" return [lst2[0]] + mymerge(lst1, lst2[1:])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mymerge([1,2,4,6,],[2,3,4,5,6,7,8])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def mergesort(lst):\n",
" n = len(lst)\n",
" if n <= 1:\n",
" return lst\n",
" left = lst[0:(n // 2)]\n",
" right = lst[(n // 2):]\n",
" sleft = mergesort(left)\n",
" sright = mergesort(right)\n",
" return mymerge(sleft, sright)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 8, 43, 436, 543, 543, 654, 6456, 435435]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mergesort([1,3,4,6,4,3,5,7,8,6,5,4,543,654,6456,43,543,5,436,435435])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def quicksort(lst):\n",
" if len(lst) <= 1:\n",
" return lst\n",
" #print(lst)\n",
" \n",
" pivot = lst[0]\n",
" \n",
" left = [x for x in lst if x < pivot]\n",
" mid = [x for x in lst if x == pivot]\n",
" right = [x for x in lst if x > pivot]\n",
" \n",
" sleft = quicksort(left)\n",
" sright = quicksort(right)\n",
" return sleft + mid + sright\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 10, 20, 30, 40, 50, 60]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"quicksort([1,3,2,5,6,4,10,20,60,50,40,30])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def square(x):\n",
" return x*x"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10000"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(100)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"f = square"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10000"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(100)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def toall(f,a,b,c):\n",
" return f(a), f(b), f(c)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 4, 9)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"toall(square, 1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 4, 9)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"toall(f,1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def apply_all(f, lst):\n",
" if lst == []:\n",
" return []\n",
" else:\n",
" return [f(lst[0])] + apply_all(f,lst[1:])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 9, 16, 25, 36, 100, 400, 900, 1600]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"apply_all(square,[1,2,3,4,5,6,10,20,30,40])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def plusone(x):\n",
" return x + 1"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def compose(f,g):\n",
" \"\"\"\n",
" compose(square,plusone) :: x -> (x+1)^2\n",
" compose(plusone,square) :: x -> x^2 + 1\n",
" \"\"\"\n",
" def h(x):\n",
" return f(g(x))\n",
" \n",
" return h"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"101"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xxx = compose(plusone, square)\n",
"xxx(10)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"101"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compose(plusone, square)(10)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def fuc(k):\n",
" def f(x):\n",
" return x + k\n",
" return f"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"g = compose(compose(fuc(1000),fuc(500)),square)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1600"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g(10)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def addone(x):\n",
" return x + 1"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.<lambda>(x)>"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lambda x: x + 1"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"101"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(lambda x: x + 1) (100)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"def fuc(k):\n",
" def f(x):\n",
" return x + k\n",
" return f\n",
"\n",
"def fuc(k):\n",
" return lambda x: x + k"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"g = fuc(10)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"110"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g(100)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(10)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 9, 16, 25]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(square, [1,2,3,4,5]))"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[False, True, False, True, False]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x: x % 2 == 0, [1,2,3,4,5]))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(lambda x: x % 2 == 0, [1,2,3,4,5]))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[21834, 324, 432, 4324, 32432, 432, 4324]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(lambda x: x > 100, \n",
" [21834,34,324,32,432,4324,32432,432,4324]))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"46"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"\n",
"def x(a,b):\n",
" return a+b\n",
"\n",
"reduce(lambda x,y: x+y, [1,1,2,3,4,5,6,5,4,5,10])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment