diff --git a/04_Coursera_3.ipynb b/04_Coursera_3.ipynb new file mode 100644 index 0000000..0751f3f --- /dev/null +++ b/04_Coursera_3.ipynb @@ -0,0 +1,3643 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "using Plots" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2)" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r = 5\n", + "x0, y0 = 1,2" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "trialcircle (generic function with 1 method)" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function trialcircle(r,x0,y0)\n", + " t = range(0,stop=2π,length=200)\n", + " #x,y = map(cos,t), map(sin,t)\n", + " x,y = r*cos.(t), r*sin.(t)\n", + " x,y = x0 .+ x, y0 .+ y\n", + " plot(x,y)\n", + "end\n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\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", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "trialcircle(r,x0,y0)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "trialcircle! (generic function with 1 method)" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function trialcircle!(r,x0,y0)\n", + " t = range(0,stop=2π,length=200)\n", + " #x,y = map(cos,t), map(sin,t)\n", + " x,y = r*cos.(t), r*sin.(t)\n", + " x,y = x0 .+ x, y0 .+ y\n", + " plot!(x,y)\n", + "end\n" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\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", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "trialcircle(1,0,0)\n", + "trialcircle!(2,0,0)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\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", + "\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", + "\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", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "trialcircle(0,0,0)\n", + "\n", + "for r = .1:.1:4\n", + " trialcircle!(r,0,0)\n", + "end\n", + "plot!()" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\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", + "\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", + "\n" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot!(grid=false,legend=false,aspect_ratio=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\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", + "\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" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "trialcircle(0,0,0)\n", + "\n", + "for r = .1:.1:4\n", + " trialcircle!(r,r,0)\n", + "end\n", + "plot!(grid=false,legend=false,aspect_ratio=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.2.0", + "language": "julia", + "name": "julia-1.2" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.2.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Week3_Peer graded quiz on Types.ipynb b/Week3_Peer graded quiz on Types.ipynb new file mode 100644 index 0000000..290b9ac --- /dev/null +++ b/Week3_Peer graded quiz on Types.ipynb @@ -0,0 +1,282 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Peer-graded quiz for `Types`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Please note that this notebook was created in Julia version 0.4.6." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Answer the following questions by creating the appropriate code." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 1\n", + "\n", + "List the subtypes of the `Real` type." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Subtypes of Real\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 2\n", + "\n", + "Create a user-defined type called `MyFloat` with a single field called `x` that is of type `Float64`." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# Create a user-defined type called MyFloat with a single field called x of type Float64\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 3\n", + "\n", + "Instantiate the `MyFloat` type with a field values of $ 3 $." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Instantiate the MyFloat type with a field value of 3\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 4\n", + "\n", + "Create a user-defined parametrized type called `MyCube` with $ 3 $ fields called `h`, `w`, and `l`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a user-defined parametrized type called MyCube with $ 3 $ fields called h, w, and l.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 5\n", + "\n", + "Instantiate the type `MyCube` with $ 2 $ variables called `cube1` and `cube2`, i.e. `cube1 = MyCube(...`. Pass the field values $ 2 $, $ 3 $, and $ 2 $ to `cube1` and $ 4 $, $ 3 $, and $ 2 $ to `cube2`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# Create 2 instances of the MyCube type\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 6\n", + "\n", + "Change the `h` field value of `cube1` to $ 3 $." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# Change the h filed value of cube1 to 3\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 7\n", + "\n", + "Import the `Base.log` function." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Import the Base.log function\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 8\n", + "\n", + "Create and external constructor that will specify the `log()` function for an instance of the `MyCube` type as the natural logarithm of the product of the values held in the fields `h`, `w`, and `l`." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# Create and external constructor for the natural logarithm of the volume of an instance of MyCube\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 9\n", + "\n", + "Calculate the `log()` of `cube1`." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# The natural log of cube1\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 10\n", + "\n", + "Overload the methods of the `+()` function to add the individual field values of $ 2 $ instances of the `MyCube` type." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "# Overload the methods of the +() functions for the MyCube type\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question 11\n", + "\n", + "Add `cube1` and `cube2`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "# Add cube1 and cube2\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "anaconda-cloud": {}, + "hide_input": false, + "kernelspec": { + "display_name": "Julia 1.0.0", + "language": "julia", + "name": "julia-1.0" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.0.0" + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}