Last active
May 12, 2017 13:04
-
-
Save pembeci/16bcbd8c73549c9af6a3e4e3f2bdf7d0 to your computer and use it in GitHub Desktop.
CENG 2002 PL Concepts - Functional Programming - Lecture 1
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
b = 8 | |
def do_something(a): | |
global b | |
b += 1 | |
return a + b | |
print(do_something(40)) | |
print("b is now", b) | |
print(do_something(40)) | |
print(do_something(40)) | |
import time | |
def make_counter(): | |
print("IN make_counter") | |
cnt = 0 | |
time.sleep(2) | |
def count(): | |
nonlocal cnt | |
print("IN count") | |
cnt += 1 | |
print("COUNT=", cnt) | |
print("RETURNING FROM make_counter") | |
return count | |
c1 = make_counter() | |
c2 = make_counter() | |
c3 = make_counter() | |
c1() | |
c1() | |
c1() | |
c2() | |
c3() | |
c2() | |
c1() | |
def make_counter2(start_val=0, incr=1): | |
cnt = start_val | |
def count(): | |
message = "COUNT=" | |
nonlocal cnt | |
print("IN count") | |
cnt += incr | |
print(message, cnt) | |
message = "NEW MESG=" | |
return count | |
c4 = make_counter2() # 1, 2, 3, 4, ... | |
c5 = make_counter2(100,5) # 105, 110, 115, 120, ... | |
c6 = make_counter2(20) # 21, 22, 23, 24, ... | |
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
def memoize(f): | |
memo = {} | |
def helper(x): | |
if x not in memo: | |
memo[x] = f(x) | |
return memo[x] | |
return helper | |
def fib(n): | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) | |
# fib = memoize(fib) | |
# print(fib(40)) |
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Functional Programming / Paradigm" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Functions as first class values" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"\n", | |
"def double(x):\n", | |
" return 2*x + 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"25" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"double(12)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"function" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(double)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"int" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = 5\n", | |
"b = a\n", | |
"b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"new_variable = double" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"25" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"new_variable(12)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def triple(x):\n", | |
" return 3 * x\n", | |
"\n", | |
"def square(x):\n", | |
" return x*x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"my_funcs = [double, triple, square, triple]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<function __main__.triple>" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"my_funcs[1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"21" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"my_funcs[1](7)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"11\n", | |
"15\n", | |
"25\n", | |
"15\n" | |
] | |
} | |
], | |
"source": [ | |
"for fn in my_funcs:\n", | |
" print fn(5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def cat_talk():\n", | |
" print \"Meaw\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"cat = { \"type\": \"animal\", \"max_age\": 15, \"talk\": cat_talk}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Meaw\n" | |
] | |
} | |
], | |
"source": [ | |
"cat[\"talk\"]()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"100" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"new_variable = square\n", | |
"new_variable(10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Higher Order Functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def apply_twice(f,x):\n", | |
" return f(f(x))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"81" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"apply_twice(square, 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"90" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"apply_twice(triple, 10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# apply_twice(len,\"abcd\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def add_to_string(word):\n", | |
" return \"[\" + word + \"]\"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'[cat]'" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"add_to_string(\"cat\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'[[cat]]'" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"apply_twice(add_to_string, \"cat\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def f(x):\n", | |
" c = 5\n", | |
" d = 2\n", | |
" def g(y):\n", | |
" return y*10\n", | |
" return c+d+g(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"27" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"f(2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def add_fn_factory(x):\n", | |
" x = x*2\n", | |
" def add(y):\n", | |
" return x+y\n", | |
" return add" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"f = add_fn_factory(7)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"24" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"f(10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"34" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"add_fn_factory(7)(20)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"add5 = add_fn_factory(5)\n", | |
"add10 = add_fn_factory(10)\n", | |
"add100 = add_fn_factory(100)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(13, 23, 203)\n" | |
] | |
} | |
], | |
"source": [ | |
"print (add5(3), add10(3), add100(3))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Some common higher order functions\n", | |
"\n", | |
"### map\n", | |
"\n", | |
"`map` is used to transform one list (collection) of values into another list by applying a function to every element in a list." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 4, 9, 16, 25, 36, 49, 64, 81]" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nums = [1,2,3,4,5,6,7,8,9]\n", | |
"new_nums = []\n", | |
"for num in nums:\n", | |
" new_nums.append(square(num))\n", | |
"new_nums " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 4, 9, 16, 25, 36, 49, 64, 81]" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(square,nums)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[5, 3, 2, 10]" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(len,[\"abcde\", \"def\", \"gh\", \"asdsfsdvdf\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Izzeet pembec\\xc4\\xb0'" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"izZEet PEMBECİ\".capitalize()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# -*- coding: utf8 -*-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def tr_capitalize(word):\n", | |
" first_letter = word[0]\n", | |
" others = word[1:]\n", | |
" if first_letter == \"i\":\n", | |
" return \"İ\" + others.lower()\n", | |
" elif first_letter == \"ı\": \n", | |
" return \"I\" + others.lower()\n", | |
" else:\n", | |
" return first_letter.upper() + others.lower()\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ali can kayaturk\n" | |
] | |
} | |
], | |
"source": [ | |
"print tr_capitalize(\"Ali can KAYATURK\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Ali Can Kayaturk'" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\" \".join(map(tr_capitalize,\"Ali can KAYATURK\".split()))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"words = \"Hello world. This is an example. \".split()\n", | |
"\n", | |
"def first3(word):\n", | |
" return word[:3]\n", | |
"\n", | |
"map(first3,words)\n", | |
"\n", | |
"def reverse(word):\n", | |
" return word[:3].upper()\n", | |
"\n", | |
"map(reverse,words)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lambda expressions are a syntactic sugar in Python so you can define anonymous functions easily." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Hel', 'wor', 'Thi', 'is', 'an', 'exa']" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(lambda w: w[:3], words)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(lambda word: word[:3].upper(), words)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"nums1 = [3,8,9,11,45]\n", | |
"nums2 = [5,4,4,28,12]\n", | |
"\n", | |
"# nums3 = [8,12,13,39,57]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[15, 32, 36, 308, 540]" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(lambda a,b:a*b, nums1, nums2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 2, 0, 0, 2, 5]" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(lambda a,b: a % b, [34,56,12,30,50,40], range(2,8))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['EEE', 'XXXX', 'AAAAA', 'MMMMMM']" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(lambda a,b: a * b, \"EXAM\", range(3,7))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"E\n", | |
"X\n", | |
"A\n", | |
"M\n" | |
] | |
} | |
], | |
"source": [ | |
"for c in \"EXAM\":\n", | |
" print c" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['E*', 'X+', 'A-', 'M/']" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(lambda a,b: a+b, \"EXAM\", [\"*\", \"+\", \"-\", \"/\"])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### filter" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"nums = [1,2,3,4,42,5,6,7,8,9,11,13,24,26]\n", | |
"\n", | |
"def is_odd(num):\n", | |
" return num%2 == 1\n", | |
"\n", | |
"def is_even(num):\n", | |
" return num%2 == 0\n", | |
"\n", | |
"def is_even_and_large(num):\n", | |
" return num%2 == 0 and num > 15" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[True,\n", | |
" False,\n", | |
" True,\n", | |
" False,\n", | |
" False,\n", | |
" True,\n", | |
" False,\n", | |
" True,\n", | |
" False,\n", | |
" True,\n", | |
" True,\n", | |
" True,\n", | |
" False,\n", | |
" False]" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"map(is_odd ,nums)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 3, 5, 7, 9, 11, 13]" | |
] | |
}, | |
"execution_count": 51, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"filter(is_odd ,nums)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[2, 4, 42, 6, 8, 24, 26]" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"filter(is_even ,nums)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[42, 24, 26]" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"filter(is_even_and_large ,nums)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[11, 13]" | |
] | |
}, | |
"execution_count": 54, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"filter(lambda n: n>=10 and n<=15 ,nums)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"students = [ \n", | |
" {\"name\": \"Ali\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"M\"}, \n", | |
" {\"name\": \"Veli\", \"year\": 1, \"gpa\": 3.1, \"gender\": \"M\"},\n", | |
" {\"name\": \"Kaya\", \"year\": 3, \"gpa\": 2.1, \"gender\": \"M\"},\n", | |
" {\"name\": \"Oya\", \"year\": 2, \"gpa\": 2.5, \"gender\": \"F\"},\n", | |
" {\"name\": \"Zeynep\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"F\"},\n", | |
" {\"name\": \"Dilek\", \"year\": 2, \"gpa\": 3.5, \"gender\": \"F\"}, \n", | |
" ]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ali: 2.8\n", | |
"Veli: 3.1\n", | |
"Kaya: 2.1\n", | |
"Oya: 2.5\n", | |
"Zeynep: 2.8\n", | |
"Dilek: 3.5\n" | |
] | |
} | |
], | |
"source": [ | |
"print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,students))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Hello\\nworld.\\nThis\\nis\\nan\\nexample.'" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"\\n\".join(words)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello\n", | |
"world.\n", | |
"This\n", | |
"is\n", | |
"an\n", | |
"example.\n" | |
] | |
} | |
], | |
"source": [ | |
"print \"\\n\".join(words)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"honor_students = filter(lambda student: student[\"gpa\"]>=3.0, students)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n", | |
" {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2}]" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"honor_students" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Veli: 3.1\n", | |
"Dilek: 3.5\n" | |
] | |
} | |
], | |
"source": [ | |
"print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,honor_students))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sorted([3,7,8,2,1,9])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"values = [3,7,8,2,1,9]\n", | |
"values.sort()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"values" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"words.sort()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Hello', 'This', 'an', 'example.', 'is', 'world.']" | |
] | |
}, | |
"execution_count": 66, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"words" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"students.sort()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n", | |
" {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n", | |
" {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n", | |
" {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n", | |
" {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n", | |
" {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1}]" | |
] | |
}, | |
"execution_count": 68, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"students" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"students.sort(key=lambda student: student[\"gpa\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"students.sort(key=lambda student: student[\"gpa\"], reverse=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n", | |
" {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n", | |
" {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n", | |
" {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n", | |
" {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n", | |
" {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]" | |
] | |
}, | |
"execution_count": 71, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"students" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n", | |
" {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n", | |
" {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n", | |
" {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n", | |
" {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n", | |
" {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]" | |
] | |
}, | |
"execution_count": 72, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sorted(students, key=lambda student: student[\"name\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n", | |
" {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n", | |
" {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n", | |
" {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n", | |
" {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n", | |
" {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]" | |
] | |
}, | |
"execution_count": 73, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sorted(students, key=lambda student: student[\"year\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n", | |
" {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n", | |
" {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n", | |
" {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n", | |
" {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n", | |
" {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]" | |
] | |
}, | |
"execution_count": 74, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sorted(students, key=lambda student: (student[\"year\"], -student[\"gpa\"]))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### reduce" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def add2(partial,next_val):\n", | |
" print \"Partial=\", partial, \" Next=\", next_val\n", | |
" return partial+next_val" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Partial= 7 Next= 3\n", | |
"Partial= 10 Next= 5\n", | |
"Partial= 15 Next= 2\n", | |
"Partial= 17 Next= 8\n", | |
"Partial= 25 Next= 3\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"28" | |
] | |
}, | |
"execution_count": 76, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"reduce(add2, [7,3,5,2,8,3])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Partial= 0 Next= 7\n", | |
"Partial= 7 Next= 3\n", | |
"Partial= 10 Next= 5\n", | |
"Partial= 15 Next= 2\n", | |
"Partial= 17 Next= 8\n", | |
"Partial= 25 Next= 3\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"28" | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"reduce(add2, [7,3,5,2,8,3], 0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"210" | |
] | |
}, | |
"execution_count": 78, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"reduce(lambda prev,new_val: prev*new_val, [7,3,5,2])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def mult2(partial,next_val):\n", | |
" result = partial*next_val\n", | |
" print \"Partial=\", partial, \" Next=\", next_val, \" Result=\", result\n", | |
" return result" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Partial= 7 Next= 3 Result= 21\n", | |
"Partial= 21 Next= 5 Result= 105\n", | |
"Partial= 105 Next= 2 Result= 210\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"210" | |
] | |
}, | |
"execution_count": 80, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"reduce(mult2, [7,3,5,2])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def larger(partial,next_val):\n", | |
" if partial>next_val:\n", | |
" result = partial\n", | |
" else:\n", | |
" result = next_val\n", | |
" print \"Partial=\", partial, \"\\t\\tNext=\", next_val, \"\\t\\tResult=\", result\n", | |
" return result" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Partial= 4 \t\tNext= 7 \t\tResult= 7\n", | |
"Partial= 7 \t\tNext= 5 \t\tResult= 7\n", | |
"Partial= 7 \t\tNext= 9 \t\tResult= 9\n", | |
"Partial= 9 \t\tNext= 3 \t\tResult= 9\n", | |
"Partial= 9 \t\tNext= 9 \t\tResult= 9\n", | |
"Partial= 9 \t\tNext= 5 \t\tResult= 9\n", | |
"Partial= 9 \t\tNext= 11 \t\tResult= 11\n", | |
"Partial= 11 \t\tNext= 8 \t\tResult= 11\n", | |
"Partial= 11 \t\tNext= 15 \t\tResult= 15\n", | |
"Partial= 15 \t\tNext= 3 \t\tResult= 15\n", | |
"Partial= 15 \t\tNext= 10 \t\tResult= 15\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"15" | |
] | |
}, | |
"execution_count": 82, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"reduce(larger, [4,7,5,9,3,9,5,11,8,15,3,10])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def add_if_even(partial,next_val):\n", | |
" if next_val%2 == 0:\n", | |
" result = partial + [next_val]\n", | |
" else:\n", | |
" result = partial\n", | |
" print \"Partial=\", partial, \"\\tNext=\", next_val, \"\\tResult=\", result\n", | |
" return result" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Partial= [] \tNext= 4 \tResult= [4]\n", | |
"Partial= [4] \tNext= 7 \tResult= [4]\n", | |
"Partial= [4] \tNext= 5 \tResult= [4]\n", | |
"Partial= [4] \tNext= 9 \tResult= [4]\n", | |
"Partial= [4] \tNext= 3 \tResult= [4]\n", | |
"Partial= [4] \tNext= 9 \tResult= [4]\n", | |
"Partial= [4] \tNext= 5 \tResult= [4]\n", | |
"Partial= [4] \tNext= 11 \tResult= [4]\n", | |
"Partial= [4] \tNext= 8 \tResult= [4, 8]\n", | |
"Partial= [4, 8] \tNext= 15 \tResult= [4, 8]\n", | |
"Partial= [4, 8] \tNext= 3 \tResult= [4, 8]\n", | |
"Partial= [4, 8] \tNext= 10 \tResult= [4, 8, 10]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[4, 8, 10]" | |
] | |
}, | |
"execution_count": 84, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"reduce(add_if_even, [4,7,5,9,3,9,5,11,8,15,3,10], [])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def partition(prev_result,next_val):\n", | |
" print \"Prev=\", prev_result\n", | |
" if next_val%2 == 0:\n", | |
" prev_result[\"even\"].append(next_val)\n", | |
" else:\n", | |
" prev_result[\"odd\"].append(next_val)\n", | |
" print \"Next=\", next_val, \"\\tNew=\", prev_result\n", | |
" return prev_result" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Prev= {'even': [], 'odd': []}\n", | |
"Next= 4 \tNew= {'even': [4], 'odd': []}\n", | |
"Prev= {'even': [4], 'odd': []}\n", | |
"Next= 6 \tNew= {'even': [4, 6], 'odd': []}\n", | |
"Prev= {'even': [4, 6], 'odd': []}\n", | |
"Next= 8 \tNew= {'even': [4, 6, 8], 'odd': []}\n", | |
"Prev= {'even': [4, 6, 8], 'odd': []}\n", | |
"Next= 3 \tNew= {'even': [4, 6, 8], 'odd': [3]}\n", | |
"Prev= {'even': [4, 6, 8], 'odd': [3]}\n", | |
"Next= 5 \tNew= {'even': [4, 6, 8], 'odd': [3, 5]}\n", | |
"Prev= {'even': [4, 6, 8], 'odd': [3, 5]}\n", | |
"Next= 6 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n", | |
"Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n", | |
"Next= 7 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n", | |
"Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n", | |
"Next= 4 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n", | |
"Prev= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n", | |
"Next= 11 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}" | |
] | |
}, | |
"execution_count": 86, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"reduce(partition, [4,6,8,3,5,6,7,4,11], {\"even\":[], \"odd\": []})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'\\nvar teams = [\\n { name: \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\\n { name: \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\\n { name: \"Karab\\xc3\\xbck\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\\n { name: \"Mu\\xc4\\x9flaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\\n { name: \"UlaG\\xc3\\xbcc\\xc3\\xbc\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\\n { name: \"K\\xc3\\xb6tekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\\n { name: \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\\n]\\nteams.map(function(t) {\\n t.points = 2*t.wins+t.draws;\\n t.played=t.wins+t.draws+t.losses\\n})\\nteams\\n .filter(function(t) { return t.played >= 5;})\\n .sort(function (t1,t2) {\\n if (t1.points != t2.points) return t2.points - t1.points;\\n else if (t1.played != t2.played) return t1.played - t2.played;\\n else return t2.name.length - t1.name.length;\\n })\\n .forEach(function(t) {\\n t.flag = true; // will this affect teams[4] output below?\\n console.log(t.name + \": \" + t.points + \"-\" + t.played);\\n })\\nconsole.log(teams[4]);\\n'" | |
] | |
}, | |
"execution_count": 87, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"\"\"\n", | |
"var teams = [\n", | |
" { name: \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n", | |
" { name: \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n", | |
" { name: \"Karabük\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n", | |
" { name: \"Muğlaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n", | |
" { name: \"UlaGücü\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n", | |
" { name: \"Kötekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n", | |
" { name: \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n", | |
"]\n", | |
"teams.map(function(t) {\n", | |
" t.points = 2*t.wins+t.draws;\n", | |
" t.played=t.wins+t.draws+t.losses\n", | |
"})\n", | |
"teams\n", | |
" .filter(function(t) { return t.played >= 5;})\n", | |
" .sort(function (t1,t2) {\n", | |
" if (t1.points != t2.points) return t2.points - t1.points;\n", | |
" else if (t1.played != t2.played) return t1.played - t2.played;\n", | |
" else return t2.name.length - t1.name.length;\n", | |
" })\n", | |
" .forEach(function(t) {\n", | |
" t.flag = true; // will this affect teams[4] output below?\n", | |
" console.log(t.name + \": \" + t.points + \"-\" + t.played);\n", | |
" })\n", | |
"console.log(teams[4]);\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"teams = [\n", | |
" { \"name\": \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n", | |
" { \"name\": \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n", | |
" { \"name\": \"Karabuk\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n", | |
" { \"name\": \"Muglaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n", | |
" { \"name\": \"UlaGucu\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n", | |
" { \"name\": \"Kotekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n", | |
" { \"name\": \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" Muglaspor 22 8\n", | |
" Karabuk 12 5\n", | |
" HataySporrr 11 6\n", | |
" HataySpor 11 6\n", | |
" Kotekli 10 8\n" | |
] | |
} | |
], | |
"source": [ | |
"def add_extra_stats(team):\n", | |
" team[\"points\"] = team[\"wins\"] * 3 + team[\"draws\"] * 1\n", | |
" team[\"games\"] = team[\"wins\"] + team[\"draws\"] + team[\"losses\"]\n", | |
" return team\n", | |
"teams = map(add_extra_stats, teams)\n", | |
"teams = filter(lambda t: t[\"games\"] >= 5, teams)\n", | |
"teams.sort(key=lambda t: (t[\"points\"], t[\"games\"], len(t[\"name\"])), reverse=True)\n", | |
"\n", | |
"for team in teams:\n", | |
" print \"%15s %4d %4d\" % (team[\"name\"], team[\"points\"], team[\"games\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'draws': 1,\n", | |
" 'games': 8,\n", | |
" 'losses': 0,\n", | |
" 'name': 'Muglaspor',\n", | |
" 'points': 22,\n", | |
" 'wins': 7},\n", | |
" {'draws': 0,\n", | |
" 'games': 5,\n", | |
" 'losses': 1,\n", | |
" 'name': 'Karabuk',\n", | |
" 'points': 12,\n", | |
" 'wins': 4},\n", | |
" {'draws': 2,\n", | |
" 'games': 6,\n", | |
" 'losses': 1,\n", | |
" 'name': 'HataySporrr',\n", | |
" 'points': 11,\n", | |
" 'wins': 3},\n", | |
" {'draws': 2,\n", | |
" 'games': 6,\n", | |
" 'losses': 1,\n", | |
" 'name': 'HataySpor',\n", | |
" 'points': 11,\n", | |
" 'wins': 3},\n", | |
" {'draws': 7,\n", | |
" 'games': 8,\n", | |
" 'losses': 0,\n", | |
" 'name': 'Kotekli',\n", | |
" 'points': 10,\n", | |
" 'wins': 1}]" | |
] | |
}, | |
"execution_count": 90, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"teams" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"XHelloY\n", | |
"<<Summer>>\n", | |
"[EXAM]\n", | |
"<<EXAM>>\n", | |
"*EXAM*\n", | |
"EXAM??EXAM\n", | |
"EM\n" | |
] | |
} | |
], | |
"source": [ | |
"def marker_fact(b, e=None):\n", | |
" if e == None: e=b\n", | |
" \n", | |
" def marker(w):\n", | |
" return b + w + e\n", | |
" \n", | |
" return marker\n", | |
"\n", | |
"print marker_fact(\"X\", \"Y\")(\"Hello\")\n", | |
"markers = [marker_fact(\"[\", \"]\"), marker_fact(\"<<\", \">>\"), \n", | |
" marker_fact(\"*\")]\n", | |
"print markers[1](\"Summer\")\n", | |
"\n", | |
"def reverse_marker(w):\n", | |
" return w + \"??\" + w\n", | |
"\n", | |
"markers.append(reverse_marker)\n", | |
"markers.append(lambda w: w[0]+w[-1]) \n", | |
"for marker in markers:\n", | |
" print marker(\"EXAM\")\n", | |
" \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[<function __main__.marker>,\n", | |
" <function __main__.marker>,\n", | |
" <function __main__.marker>,\n", | |
" <function __main__.reverse_marker>,\n", | |
" <function __main__.<lambda>>]" | |
] | |
}, | |
"execution_count": 92, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"markers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'*ABCDEFG*'" | |
] | |
}, | |
"execution_count": 98, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"markers[2](\"ABCDEFG\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['*What*', '+the+', '|hell|']\n" | |
] | |
} | |
], | |
"source": [ | |
"new_markers = map(marker_fact, [\"*\", \"+\", \"|\"])\n", | |
"print map(lambda f,w: f(w), new_markers, [\"What\", \"the\", \"hell\"])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": true | |
}, | |
"source": [ | |
"### List Comprehensions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[6, 16, 12, 2, 10, 18, 24, 90, 30, 8]" | |
] | |
}, | |
"execution_count": 101, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nums = [3,8,6,1,5,9,12,45,15,4]\n", | |
"nums2 = map(lambda n: n*2, nums)\n", | |
"nums2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[6, 16, 12, 2, 10, 18, 24, 90, 30, 8]" | |
] | |
}, | |
"execution_count": 102, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nums3 = [2*num for num in nums]\n", | |
"nums3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[24, 90, 30]" | |
] | |
}, | |
"execution_count": 103, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nums4 = [2*num for num in nums if num>10]\n", | |
"nums4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[24, 90, 30]" | |
] | |
}, | |
"execution_count": 104, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nums5 =map(lambda n:n*2,filter(lambda n:n>10, nums))\n", | |
"nums5" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1, 'A'),\n", | |
" (1, 'B'),\n", | |
" (1, 'C'),\n", | |
" (1, 'D'),\n", | |
" (2, 'A'),\n", | |
" (2, 'B'),\n", | |
" (2, 'C'),\n", | |
" (2, 'D'),\n", | |
" (3, 'A'),\n", | |
" (3, 'B'),\n", | |
" (3, 'C'),\n", | |
" (3, 'D')]" | |
] | |
}, | |
"execution_count": 105, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[(a,b) for a in [1,2,3] for b in [\"A\", \"B\", \"C\", \"D\"]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 109, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"+\n", | |
"+\n", | |
"++\n", | |
"+\n", | |
"++\n", | |
"+++\n", | |
"+\n", | |
"++\n", | |
"+++\n", | |
"++++\n", | |
"+\n", | |
"++\n", | |
"+++\n", | |
"++++\n", | |
"+++++\n", | |
"+\n", | |
"++\n", | |
"+++\n", | |
"++++\n", | |
"+++++\n", | |
"++++++\n", | |
"+\n", | |
"++\n", | |
"+++\n", | |
"++++\n", | |
"+++++\n", | |
"++++++\n", | |
"+++++++\n", | |
"+\n", | |
"++\n", | |
"+++\n", | |
"++++\n", | |
"+++++\n", | |
"++++++\n", | |
"+++++++\n", | |
"++++++++\n" | |
] | |
} | |
], | |
"source": [ | |
"print \"\\n\".join([b * \"+\" for a in range(1,10) for b in range(1,a)])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[102, 3, 104, 5, 106, 7, 108, 9]" | |
] | |
}, | |
"execution_count": 111, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[(w+100 if w%2==0 else w) for w in [2,3,4,5,6,7,8,9] ]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment