{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1\n", "\n", "Given n, please return the nth Fibonacci number.\n", "\n", "請回傳第 n 個費波那西的值,費波那西的公式如下\n", "\n", " f(0) = 0\n", " f(1) = 1\n", " f(n) = f(n-1) + f(n-2)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' Python swap 不需要 tmp\\n 他的順序是:\\n 1. 讀出 b\\n 2. 讀出 a 和 b, 並把他們相加\\n 3. 把讀出來的 b 存進 a\\n 4. 把讀出來算完的 a+b 存進 b\\n \\n 另外這裡的 a, b 分別是第 n 個和第 n+1 個\\n n = 0 的時候迴圈不會執行 f_0 = 0 = a\\n'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fib(n):\n", " ### your code start\n", " a, b, = 0, 1\n", " while n > 0:\n", " a, b = b, a + b\n", " n -= 1\n", " return a\n", " ### your code end\n", " \n", "''' Python swap 不需要 tmp\n", " 他的順序是:\n", " 1. 讀出 b\n", " 2. 讀出 a 和 b, 並把他們相加\n", " 3. 把讀出來的 b 存進 a\n", " 4. 把讀出來算完的 a+b 存進 b\n", " \n", " 另外這裡的 a, b 分別是第 n 個和第 n+1 個\n", " n = 0 的時候迴圈不會執行 f_0 = 0 = a\n", "'''" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# test cell\n", "fib(5)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert fib(5) == 5\n", "assert fib(10) == 55" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2\n", "\n", "Given a number array, please return a new array whose elements are twice of origin's (Try to use `list comprehension`)\n", "\n", "給定一個 array,請回傳一個新 array,裡面的每個 elements 為原本的兩倍(請使用 list comprehension 試試看)\n", "\n", " input = [1, 4, 7, 3, 13]\n", " output = [2, 8, 14, 6, 26]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\" 這是個常用的寫法 這樣寫行數比較短 也可以直接將 List 傳進 function (和 lambda 很像)\\n 上面的 function 其實可以寫成\\n \\n def dup(input_list):\\n a = []\\n for(i in input_list):\\n a.append(i * 2)\\n \\n 假設你要排序第三題每個人的年齡 但不在乎是誰的年齡\\n 原本你可能這樣寫\\n \\n ages = []\\n for x in students:\\n ages.appends(x['age'])\\n ages.sort()\\n \\n 用 [] 的寫法則可以一行寫完\\n ages = sorted([x['age'] for x in students])\\n\"" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def dup(input_list):\n", " ### your code start\n", " return [i * 2 for i in input_list]\n", " ### your code end\n", " \n", "''' 這是個常用的寫法 這樣寫行數比較短 也可以直接將 List 傳進 function (和 lambda 很像)\n", " 上面的 function 其實可以寫成\n", " \n", " def dup(input_list):\n", " a = []\n", " for(i in input_list):\n", " a.append(i * 2)\n", " \n", " 假設你要排序第三題每個人的年齡 但不在乎是誰的年齡\n", " 原本你可能這樣寫\n", " \n", " ages = []\n", " for x in students:\n", " ages.appends(x['age'])\n", " ages.sort()\n", " \n", " 用 [] 的寫法則可以一行寫完\n", " ages = sorted([x['age'] for x in students])\n", "'''" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "# test cell" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert dup([]) == []\n", "assert dup([1, 2, 3]) == [2, 4, 6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3\n", "\n", "Given an array of students' info (Try to use `lambda`)\n", "You don't have to return array.\n", "\n", " input = [{\n", " 'name': Abby,\n", " 'age': 16,\n", " 'number': 1,\n", " }, {\n", " 'name': Kinna,\n", " 'age': 17,\n", " 'number': 2,\n", " },{\n", " 'name': Yuki,\n", " 'age': 15,\n", " 'number': 3,\n", " }]\n", " output = [{\n", " 'name': Yuki,\n", " 'age': 15,\n", " 'number': 3,\n", " }, {\n", " 'name': Abby,\n", " 'age': 16,\n", " 'number': 1,\n", " },{\n", " 'name': Kinna,\n", " 'age': 17,\n", " 'number': 2,\n", " }]\n" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\" 1. list.sort 會直接對 list 做更動,而 sorted 會回傳新的排序好的的 list\\n 下面兩段回有一樣的結果\\n \\n mylist.sort()\\n \\n 和\\n \\n mylist = sorted(mylist)\\n \\n 2. lambda x: x['age'] 其實就等於\\n \\n def myfunc(x):\\n return x['age']\\n\"" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def sort_student(students):\n", " ### your code start\n", " students.sort(key=lambda x: x['age'])\n", " ### your code end\n", " \n", "''' 1. list.sort 會直接對 list 做更動,而 sorted 會回傳新的排序好的的 list\n", " 下面兩段回有一樣的結果\n", " \n", " mylist.sort()\n", " \n", " 和\n", " \n", " mylist = sorted(mylist)\n", " \n", " 2. lambda x: x['age'] 其實就等於\n", " \n", " def myfunc(x):\n", " return x['age']\n", "'''" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "students = [{'name': 'Abby', 'age': 16, 'number': 1,}, {\n", " 'name': 'Kinna','age': 17, 'number': 2,}, {\n", " 'name': 'Yuki', 'age': 15, 'number': 3,}]\n", "\n", "students_result = [{'name': 'Yuki', 'age': 15, 'number': 3,}, {\n", " 'name': 'Abby', 'age': 16, 'number': 1,},{\n", " 'name': 'Kinna','age': 17, 'number': 2,}]\n", "\n", "sort_student(students)\n", "assert students == students_result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4\n", "\n", "Combine two array as an array of tuple (Try to use `zip`)\n", "\n", "將兩個 array 結合成 tuple 的資料格式\n", "\n", " input = ['a', 'b', 'c'], [1, 2, 3]\n", " output = [('a', 1), ('b', 2), ('c', 3)\n", "\n", "### zip 在自己 create 一個 dataframe 的時候會常用到 可以跑一下下面的 code 看一下 `zip(mylist)` 和 `zip(*mylist)` 的差別" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "def combine(a, b):\n", " ### your code start\n", " return list(zip(a, b))\n", " ### your code end" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert combine(['a', 'b', 'c'], [1, 2, 3]) == [('a', 1), ('b', 2), ('c', 3)]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('a', 1), ('b', 2), ('c', 3)]\n", "[('a', 'b', 'c'), (1, 2, 3)]\n" ] } ], "source": [ "# TO unzip list to two individual lists\n", "l = combine(['a', 'b', 'c'], [1, 2, 3])\n", "\n", "def upzip(l):\n", " return list(zip(*l))\n", "\n", "print(l)\n", "print(upzip(l))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Excercise 5\n", "\n", "implement `range` function. Return element by elment instead of `List` (Use `generator`)\n", "\n", "實作 range fumction,並回傳一個接一個的值 (使用 generator 來達到回傳單一值的效果)\n", "\n", " def range(start, end):\n", " ...\n", " \n", " for i in range(1, 5):\n", " print(i,)\n", " \n", " >>> 1,2,3,4,5\n", " " ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' generator 的執行可以想成這樣\\n for i in mygenerator 是 call mygenerator.__next__() 到沒 yield 為止\\n 每一個 __next__() 會執行到 yield 把值傳回來並暫停\\n \\n for i in my_range(2, 5): 可以想成這樣:\\n \\n 1. call my_range.next()\\n i = 2\\n return i,暫停\\n \\n 2. call my_range.next()\\n i += 1 # i = 3\\n return i,暫停\\n \\n 3. call my_range.next()\\n i += 1 # i = 4\\n return i,暫停\\n \\n 4. call my_range.next()\\n i += 1 # i = 5\\n 跳脫 while 迴圈\\n 到真正的 return\\n \\n 5. 迴圈結束\\n'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def my_range(start, end):\n", " ### your code start\n", " i = start\n", " while i < end:\n", " yield i\n", " i += 1\n", " return # 跟不寫是一樣的\n", " ### your code end\n", " \n", "''' generator 的執行可以想成這樣\n", " for i in mygenerator 是 call mygenerator.__next__() 到沒 yield 為止\n", " 每一個 __next__() 會執行到 yield 把值傳回來並暫停\n", " \n", " for i in my_range(2, 5): 可以想成這樣:\n", " \n", " 1. call my_range.next()\n", " i = 2\n", " return i,暫停\n", " \n", " 2. call my_range.next()\n", " i += 1 # i = 3\n", " return i,暫停\n", " \n", " 3. call my_range.next()\n", " i += 1 # i = 4\n", " return i,暫停\n", " \n", " 4. call my_range.next()\n", " i += 1 # i = 5\n", " 跳脫 while 迴圈\n", " 到真正的 return\n", " \n", " 5. 迴圈結束\n", "'''" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert my_range(10, 20).__next__() == 10\n", "assert [i for i in my_range(2, 5)] == [2, 3, 4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 6\n", "\n", "Add each element in two lists by `map`.\n", "\n", "將兩個 list 內的元素相加(使用 map function)\n", "\n", " input_1 = [1, 2, 3]\n", " input_2 = [1, 2, 4]\n", " \n", " merge(input_1, input_2) = [2, 4, 7]\n", " \n", "### map 是一個常用的對 list 做調整的 function\n", "\n", " new_list = list(map(lambda x: x+1, a))\n", " \n", "等於下面\n", " \n", " new_list = list(a) # 複製一份 a,為了有相同的長度\n", " for i in range(len(a)):\n", " new_list[i] = a[i] + 1\n", "\n", "---\n", "\n", " new_list = map(lambda x, y: x+y, a, b)\n", "\n", "等於下面\n", " \n", " new_list = list(a) # 複製一份 a,為了有相同的長度\n", " for i in range(len(a)):\n", " new_list[i] = a[i] + b[i]\n", " \n" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "def merge(a, b):\n", " ### your code start\n", " return list(map(lambda x, y: x+y, a, b))\n", " ### your code end" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert merge([1, 2, 3], [1, 2, 4]) == [2, 4, 7]\n", "assert merge([], []) == []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 7\n", "\n", "Add index to each elements in list. (Use `enumerate`)\n", "\n", "將輸入的 list 和本身的 index 做相加(使用 enumerate)\n", "\n", " input = [1, 2, 6, 4]\n", " output = [1, 3, 8, 7]\n", " \n", "That is,\n", "\n", " output[0] = input[0] + 0\n", " output[1] = input[1] + 1\n", " ...\n", " \n", "### `enumerate` 可以想成這樣的func\n", "\n", " def enumerate(mylist):\n", " for i in range(len(mylist)):\n", " yield i, mylist[i]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "def add_index(my_list):\n", " ### your code start\n", " return [i + e for i, e in enumerate(my_list)]\n", " ### your code end" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert add_index([1, 2, 3]) == [1, 3, 5]\n", "assert add_index([]) == []" ] }, { "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }