Skip to content
This repository was archived by the owner on Oct 9, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Student/Ruohan/lesson01/.cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pure Function\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def m(n: int) -> int:\n",
" return 2**n-1\n",
"\n",
"## This result depends only on the parameter, n. \n",
"## There are no changes to global variables and the function doesn't update any mutable data structures."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A procedural approach!\n",
"\n",
"- Functions generally consist of multiple statements\n",
" - Assignments\n",
" - If-statements\n",
" - While loops\n",
" - Etc."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the barely functional calculator!\n",
"Enter an integer: 10\n",
"Enter an operator (+, -, *, or /): *\n",
"Enter an integer: 30\n",
"The result is: 300\n"
]
}
],
"source": [
"OPERATORS = '+', '-', '*', '/'\n",
"\n",
"\n",
"def p_main():\n",
" \n",
" \"\"\"The main flow.\"\"\"\n",
"\n",
" print('Welcome to the barely functional calculator!')\n",
" number1 = p_get_number()\n",
" operator = p_get_operator()\n",
" number2 = p_get_number()\n",
" result = p_calculate(number1, operator, number2)\n",
" print('The result is: %s' % result)\n",
"\n",
"\n",
"def p_get_number():\n",
" \n",
" \"\"\"Reads an integer from the standard input and returns it.\n",
" If a non-integer value is entered, a warning is printed,\n",
" and a new value is read.\"\"\"\n",
" \n",
" while True:\n",
" s = input('Enter an integer: ')\n",
" try:\n",
" return int(s)\n",
" except ValueError:\n",
" print('That is not an integer!')\n",
" \n",
"\n",
"def p_get_operator():\n",
" \n",
" \"\"\"Reads an operator from the standard input and returns it.\n",
" Valid operators are: +, -, *, and /. If an invalid operator\n",
" is entered, a warning is printed, and a new value is read.\"\"\" \n",
" \n",
" while True:\n",
" s = input('Enter an operator (+, -, *, or /): ')\n",
" if s in OPERATORS:\n",
" return s\n",
" print('That is not an operator!')\n",
" \n",
" \n",
"def p_calculate(number1, operator, number2):\n",
" \n",
" \"\"\"Performs a calculation with two numbers and an operator,\n",
" and returns the result.\"\"\"\n",
" \n",
" if operator == '+':\n",
" return number1 + number2\n",
" if operator == '-':\n",
" return number1 - number2\n",
" if operator == '*':\n",
" return number1 * number2\n",
" if operator == '/':\n",
" return number1 / number2\n",
" raise Exception('Invalid operator!')\n",
"\n",
" \n",
"p_main()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A functional approach!\n",
"\n",
"- Functions consist of only one expression\n",
"- How can we validate input? (One of the many things we will learn later!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"OPERATORS = '+', '-', '*', '/'\n",
"\n",
"\n",
"def f_get_number():\n",
" return int(input('Enter an interger: '))\n",
"\n",
"\n",
"def f_get_operator():\n",
" return input('Enter an operator:')\n",
"\n",
"\n",
"def f_calculate(num1, num2, operator) :\n",
" return num1 + num2 if operator == '+'\\\n",
" else num1 - num2 elif operator == '-'\\\n",
" else num1 * num2 elif operator == '*'\\\n",
" else num1 / num2 elif operator == '/'\\\n",
" else None\n",
" \n",
"\n",
"\n",
"def f_main():\n",
" return f_calculate(f_get_number(), f_get_number(), f_get_operator())\n",
" \n",
"\n",
"print('The result is: %s' % f_main())"
]
},
{
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list(range(10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for x in range(10):\n",
" print (x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## MAP"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"items = [1, 2, 3, 4, 5]\n",
"squared = list(map(lambda x: x**2, items))\n",
"print(squared)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Filter\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number_list = range(-10, 10)\n",
"less_than_zero = list(filter(lambda x: x < 0, number_list))\n",
"print(less_than_zero)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number_list = range(-10, 10)\n",
"less_than_zero = list(filter(lambda x: x > 0, number_list))\n",
"print(less_than_zero)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l = [2,3,4,5,6]\n",
"list(filter())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reduce"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from functools import reduce\n",
"product = reduce((lambda x, y: x * y), [1, 2, 3])\n",
"print(product)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## List Comprehension\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"multiples = [i for i in range(30) if i % 3 == 0]\n",
"print(multiples)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"squared = [x**2 for x in range(10)]\n",
"print(squared)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dict Comprehension\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mcase = {'a': 5, 'b': 3, 'A': 7, 'Z': 6}\n",
"{v: k for k, v in mcase.items()}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set Comprehension"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"squared = {x**2 for x in [0,1,1,2]}\n",
"print(squared)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## In the previous set example, can you explain the output?"
]
},
{
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file not shown.
Binary file not shown.
Loading