{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1. Fibonacci Sequence\n", "在课程里,讨论过如果去找到第N个Fibonacci number。在这里,我们来试着求一下它的Closed-form解。 \n", "\n", "Fibonacci数列为 1,1,2,3,5,8,13,21,.... 也就第一个数为1,第二个数为1,以此类推...\n", "我们用f(n)来数列里的第n个数,比如n=3时 f(3)=2。\n", "\n", "下面,来证明一下fibonacci数列的closed-form, 如下:\n", "\n", "$f(n)=\\frac{1}{\\sqrt{5}}(\\frac{1+\\sqrt{5}}{2})^n-\\frac{1}{\\sqrt{5}}(\\frac{1-\\sqrt{5}}{2})^n$\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "// your proof is here ....\n", "\n", "可参考\n", "http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibformproof.html\n", "\n", "\n", "### 具体证明如下:\n", "首先根据fibonacci数列的定义,我们可以得到如下的公式:\n", "\n", "$\\begin{pmatrix} f_{n+1} \\\\ f_n \\end{pmatrix}=\\begin{pmatrix}\n", "1&1\\\\1&0 \\end{pmatrix}^n \\begin{pmatrix} f_1 \\\\ f_0 \\end{pmatrix}$\n", "\n", "令$A=\\begin{pmatrix} 1&1\\\\ 1&0 \\end{pmatrix}$,根据 $|\\lambda E-A|=0 $,我们可以求出$A$的两个特征值为\n", "\n", "$\\lambda_1=\\frac{1-\\sqrt{5}}{2},\\lambda_2=\\frac{1+\\sqrt{5}}{2}$\n", "\n", "进一步得到特征向量为\n", "\n", "$\\alpha_1=\\begin{pmatrix} 1\\\\ \\frac{1+\\sqrt{5}}{2} \\end{pmatrix},\n", " \\alpha_2=\\begin{pmatrix} 1\\\\ \\frac{-1+\\sqrt{5}}{2} \\end{pmatrix}$\n", " \n", " 从而得到\n", " \n", "$P=\\begin{pmatrix} 1 & 1 \\\\ \\frac{1+\\sqrt{5}}{2} & \\frac{1-\\sqrt{5}}{2} \\end{pmatrix},\n", "P^{-1}=\\begin{pmatrix} \\frac{1-\\sqrt{5}}{2} & 1 \\\\ \\frac{1+\\sqrt{5}}{2} & -1 \\end{pmatrix} $\n", "\n", "因为 $(PAP^{-1})^n=PA(P^{-1}P)A...(P^{-1}P)AP^{-1}=PA^nP^{-1}$,所以\n", "$A^n=P^{-1}(PAP^{-1})^nP=P^{-1}\\begin{pmatrix} (\\frac{1-\\sqrt{5}}{2})^n & 0 \\\\ 0& (\\frac{1+\\sqrt{5}}{2})^n \\end{pmatrix} P $\n", "\n", "从而得出\n", "$f(n)=\\frac{1}{\\sqrt{5}}(\\frac{1+\\sqrt{5}}{2})^n-\\frac{1}{\\sqrt{5}}(\\frac{1-\\sqrt{5}}{2})^n$\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 证明如下:\n", "$\\begin{pmatrix} f_{n+1} \\\\ f_n \\end{pmatrix}=\\begin{pmatrix}\n", "1&1\\\\1&0 \\end{pmatrix}^n \\begin{pmatrix} f_1 \\\\ f_0 \\end{pmatrix}$\n", "\n", "令$A=\\begin{pmatrix} 1&1\\\\ 1&0 \\end{pmatrix}$,根据 $|\\lambda E-A|=0 $,我们可以求出$A$的两个特征值为\n", "\n", "$\\lambda_1=\\frac{1-\\sqrt{5}}{2},\\lambda_2=\\frac{1+\\sqrt{5}}{2}$\n", "\n", "进一步得到特征向量为\n", "\n", "$\\alpha_1=\\begin{pmatrix} 1\\\\ \\frac{1+\\sqrt{5}}{2} \\end{pmatrix},\n", " \\alpha_2=\\begin{pmatrix} 1\\\\ \\frac{-1+\\sqrt{5}}{2} \\end{pmatrix}$\n", " \n", " 从而得到\n", " \n", "$P=\\begin{pmatrix} 1 & 1 \\\\ \\frac{1+\\sqrt{5}}{2} & \\frac{1-\\sqrt{5}}{2} \\end{pmatrix},\n", "P^{-1}=\\begin{pmatrix} \\frac{1-\\sqrt{5}}{2} & 1 \\\\ \\frac{1+\\sqrt{5}}{2} & -1 \\end{pmatrix} $\n", "\n", "因为 $(PAP^{-1})^n=PA(P^{-1}P)A...(P^{-1}P)AP^{-1}=PA^nP^{-1}$,所以\n", "$A^n=P^{-1}(PAP^{-1})^nP=P^{-1}\\begin{pmatrix} (\\frac{1-\\sqrt{5}}{2})^n & 0 \\\\ 0& (\\frac{1+\\sqrt{5}}{2})^n \\end{pmatrix} P $\n", "\n", "从而得出\n", "$f(n)=\\frac{1}{\\sqrt{5}}(\\frac{1+\\sqrt{5}}{2})^n-\\frac{1}{\\sqrt{5}}(\\frac{1-\\sqrt{5}}{2})^n$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem2. Algorithmic Complexity\n", "对于下面的复杂度,从小大排一下顺序:\n", "\n", "$O(N), O(N^2), O(2^N), O(N\\log N), O(N!), O(1), O(\\log N), O(3^N), O(N^2\\log N), O(N^{2.1})$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "// your answer....\n", "\n", "\n", "$O(1) --> O(\\log N)-->O(N)-->O(N\\log N)-->O(N^2)-->O(N^2logN)-->O(N^{2.1})-->O(2^N)-->O(3^N)-->O(N!)$\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3 Dynamic Programming Problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Edit Distance (编辑距离)\n", "编辑距离用来计算两个字符串之间的最短距离,这里涉及到三个不通过的操作,add, delete和replace. 每一个操作我们假定需要1各单位的cost. \n", "\n", "例子: \"apple\", \"appl\" 之间的编辑距离为1 (需要1个删除的操作)\n", "\"machine\", \"macaide\" dist = 2\n", "\"mach\", \"aaach\" dist=2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# 基于动态规划的解法\n", "def levenshtein_dp(s, t):\n", " m, n = len(s), len(t)\n", " table = [[0] * (n) for _ in range(m)]\n", "\n", " for i in range(n):\n", " if s[0] == t[i]:\n", " table[0][i] = i - 0\n", " elif i != 0:\n", " table[0][i] = table[0][i - 1] + 1\n", " else:\n", " table[0][i] = 1\n", "\n", " for i in range(m):\n", " if s[i] == t[0]:\n", " table[i][0] = i - 0\n", " elif i != 0:\n", " table[i][0] = table[i - 1][0] + 1\n", " else:\n", " table[i][0] = 1\n", "\n", " print(table)\n", " for i in range(1, m):\n", " for j in range(1, n):\n", " table[i][j] = min(1 + table[i - 1][j], 1 + table[i][j - 1], int(s[i] != t[j]) + table[i - 1][j - 1])\n", "\n", " print(table)\n", " return table[-1][-1]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入字符串1:appl\n", "输入字符串2:apple\n", "[[0, 1, 2, 3, 4], [1, 0, 0, 0, 0], [2, 0, 0, 0, 0], [3, 0, 0, 0, 0]]\n", "[[0, 1, 2, 3, 4], [1, 0, 1, 2, 3], [2, 1, 0, 1, 2], [3, 2, 1, 0, 1]]\n", "1\n" ] } ], "source": [ "str1 = input(\"输入字符串1:\")\n", "str2 = input(\"输入字符串2:\")\n", "print(levenshtein_dp(str1, str2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4 非技术问题\n", "本题目的目的是想再深入了解背景,之后课程的内容也会根据感兴趣的点来做适当会调整。 \n", "\n", "\n", "Q1: 之前或者现在,做过哪些AI项目/NLP项目?可以适当说一下采用的解决方案,如果目前还没有想出合适的解决方案,也可以说明一下大致的想法。 请列举几个点。\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Q2: 未来想往哪个行业发展? 或者想做哪方面的项目? 请列举几个点。\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Q3: 参加训练营,最想获得的是什么?可以列举几个点。\n", "\n", "\n" ] }, { "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.8" } }, "nbformat": 4, "nbformat_minor": 2 }