{ "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": null, "metadata": {}, "outputs": [], "source": [ "def fib(n):\n", " ### your code start\n", " return \n", " ### your code end" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# test cell\n", "fib(5)" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "def dup(input_list):\n", " ### your code start\n", " return \n", " ### your code end\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# test cell" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "def sort_student(students):\n", " ### your code start\n", " return \n", " ### your code end\n" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "def combine(a, b):\n", " ### your code start\n", " return \n", " ### your code end" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [] }, { "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": null, "metadata": {}, "outputs": [], "source": [ "def my_range(start, end):\n", " ### your code start\n", " return # 跟不寫是一樣的\n", " ### your code end\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "def merge(a, b):\n", " ### your code start\n", " return \n", " ### your code end" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "def add_index(my_list):\n", " ### your code start\n", " return \n", " ### your code end" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert add_index([1, 2, 3]) == [1, 3, 5]\n", "assert add_index([]) == []" ] } ], "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 }