{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8a8d5e8f-5498-4782-8ece-fa021c995c68",
   "metadata": {},
   "source": [
    "# Homework 2\n",
    "\n",
    "Complete the following code fragments one after another. When you run the cells, a test \n",
    "will show you whether you were successful.\n",
    "\n",
    "You may add cells to try out code.\n",
    "\n",
    "_Please run the following code cell first._"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7376bff9-d18a-4169-b71a-8c83fbdaa468",
   "metadata": {},
   "outputs": [],
   "source": [
    "macro mtest(tests...)\n",
    "    results = []\n",
    "    \n",
    "    for test in tests\n",
    "        test_str = string(test)\n",
    "        push!(results, quote\n",
    "            local test_result = try\n",
    "                $(esc(test))\n",
    "            catch e\n",
    "                e\n",
    "            end\n",
    "            \n",
    "            if test_result isa Exception\n",
    "                println(\"❌ FAILED: \", $test_str)\n",
    "                println(\"   Error: \", test_result)\n",
    "                false\n",
    "            elseif test_result == true\n",
    "                println(\"✓ PASSED: \", $test_str)\n",
    "                true\n",
    "            else\n",
    "                println(\"❌ FAILED: \", $test_str)\n",
    "                println(\"   Result: \", test_result)\n",
    "                false\n",
    "            end\n",
    "        end)\n",
    "    end\n",
    "    \n",
    "    return quote\n",
    "        local test_results = []\n",
    "        \n",
    "        $(map(r -> :(push!(test_results, $r)), results)...)\n",
    "        \n",
    "        local passed = count(test_results)\n",
    "        local failed = length(test_results) - passed\n",
    "        \n",
    "        println(\"\\n\" * \"=\"^50)\n",
    "        println(\"Test Summary: \", passed, \" passed, \", failed, \" failed\")\n",
    "        println(\"=\"^50)\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1f46c6d-6541-484a-80cd-6151fcdddc64",
   "metadata": {},
   "source": [
    "## Exercise 1\n",
    "Define a vector `v` of floating point numbers with the following entries: $-\\frac{1}{3}, 12.3\\times10^{-7}, \\log(20), 2^6$ and 6 (american) billions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "322677f9-2e75-4f7a-b656-1731822c763d",
   "metadata": {},
   "outputs": [],
   "source": [
    "v = [ #= TODO: add the entries here  =# ] \n",
    "\n",
    "# test section. Do not change!\n",
    "@mtest length(v)==5 prod(v)== -471648.08914834034"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83e86326-5ec2-41df-948c-2efeabe9512a",
   "metadata": {},
   "source": [
    "## Exercise 2\n",
    "\n",
    "Define `my_types` such that it is a vector of the types in `my_vars`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3d52e42-8a75-4e28-8638-5b9283876446",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_vars = [-1.3, 0x0034, 101_303, \"String\", 2//3]\n",
    "my_types = [ #= TODO: add the entries here =# ]\n",
    "\n",
    "# test section. Do not change!\n",
    "@mtest isa(my_types, Vector{<:Type}) isequal(my_types,typeof.(my_vars))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03417c87-30fa-41a0-835c-dad6d9037be4",
   "metadata": {},
   "source": [
    "## Exercise 3\n",
    "\n",
    "- Write a function which takes a list (i.e., a vector) of numbers as argument and returns the product of these numbers. \n",
    "- The function should return `1` for an empty list.\n",
    "- Use a `for` loop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5ba786c-fd8d-4927-9ff1-301c86c06679",
   "metadata": {},
   "outputs": [],
   "source": [
    "function my_prod(list)\n",
    "  # TODO: write the function\n",
    "end\n",
    "\n",
    "# test section. Do not change!\n",
    "v = rand(7)\n",
    "@mtest my_prod([-17.2, 3//5, 28.3, 9]) == prod([-17.2, 3//5, 28.3, 9])    my_prod([]) == 1 my_prod(v) == prod(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80971a43-e200-4725-bf7a-91304dc03c41",
   "metadata": {},
   "source": [
    "### Exercise 4\n",
    "\n",
    "Write a function that computes the standard deviation of all elements of a vector:\n",
    "\n",
    "\n",
    "$$\n",
    "\\sigma = \\sqrt{\\frac{1}{n-1}\\sum_{k=0}^n (x_k - \\bar x)^2}\n",
    "$$\n",
    "\n",
    "\n",
    "where \n",
    "$$\n",
    "\\bar x = \\frac{1}{n}\\sum_{k=0}^n x_k\n",
    "$$\n",
    "is the mean of the data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8321239a-46d6-4213-aab3-82c5dd1d8433",
   "metadata": {},
   "outputs": [],
   "source": [
    "function my_std(v)\n",
    "    # TODO: write the function\n",
    "end\n",
    "\n",
    "# test section. Do not change!\n",
    "using Statistics\n",
    "v = rand(27)\n",
    "@mtest my_std(v) == std(v)  abs(my_std(collect(1:5)) - 1.5811388300841898) < 1.e-10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edf2b86b-3cc2-4d6f-b6fd-b2cfdc4388c6",
   "metadata": {},
   "source": [
    "## Exercise 5 - Euclidean algorithm\n",
    "\n",
    "\n",
    "Write a function calculating the GCD of two positive integers using the Euclidean algorithm, i.e., \n",
    "use the identity \n",
    "$$\n",
    " \\text{gcd}(a, b) = \\text{gcd}(b, a \\text{ mod } b) \n",
    "$$\n",
    "\n",
    "in a `while` loop.  The loop ends when the second argument is zero. Then, the first argument is the gcd.\n",
    "\n",
    "Example: gcd(48, 18) = gcd(18, 12) = gcd(12, 6) = gcd(6, 0) = 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd56f7a4-21e2-4083-b5a7-35c607fb800a",
   "metadata": {},
   "outputs": [],
   "source": [
    "function my_gcd(a,b)\n",
    "   # TODO: write the function\n",
    "end\n",
    "\n",
    "# test section. Do not change!\n",
    "x1 = abs(rand(Int)); x2=abs(rand(Int))\n",
    "@mtest my_gcd(342, 46)==gcd(342,46)   my_gcd(16, 1000)==gcd(16,1000)  my_gcd(1342,47)==gcd(1342,47) my_gcd(x1,x2)==gcd(x1,x2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b35034db-6eac-4d3b-8904-1b16aac14240",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.11.7",
   "language": "julia",
   "name": "julia-1.11"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
