{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW08 InClass - Repetition and Programming \n", "\n", "This file is intended for interactive revision in the first hour of class. You write directly in the lines of code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 1 - Ball Surface \n", "\n", "- Define the function ``ball_surface(radius)``, which can calculate and **return** the surface area of ​​a sphere.\n", "- Provide the function with **Type-Hints**\n", "- Provide the function with a **Doc string**\n", "- Give the function a **default value** of 5\n", "- Test this" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 2 - Distance 2d \n", "- Define the function that can calculate the distance between two points in 2D: ``distance_2D((x1,y1)(x2,y2))``\n", "- test these" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The distance between points is 0.0 units\n" ] } ], "source": [ "import math\n", "\n", "x1,y1 = float(input(\"Type x1: \")),float(input(\"Type y1: \"))\n", "x2,y2 = float(input(\"Type x2: \")),float(input(\"Type y2: \"))\n", "\n", "distance_x = (x2-x1)\n", "distance_y = (y2-y1)\n", "\n", "def distance_2D(distance_x,distance_y):\n", " distance = math.sqrt((distance_x)**2 + (distance_y)**2)\n", " print(f\"The distance between points is {round(distance, 2)} units\")\n", "\n", "distance_2D(distance_x,distance_y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 3 - Farenheit \n", "\n", "- Write a function ``celcius_fahrenheit(deg:float)`` which converts a temperature from **°Celcius to °Fahrenheit**.\n", "- Extend the **input and return values** with **type hints**\n", "- Create a **DocString** for the function\n", "\n", "- Test the function\n", "- 0°C = 32°F\n", "- 37°C $\\approx $ 98.6°F" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "37.0°C are 98.6°F\n" ] } ], "source": [ "deg = input(\"Degree in Celsius: \")\n", "def celsius_farenheit(deg:float)->float:\n", " \"\"\"\n", " Parameters\n", " deg: Degrees [°C]\n", "\n", " Result\n", " return far: Converted degrees [°F]\n", " \"\"\"\n", " far = (float(deg)*9/5)+32\n", " return far\n", "\n", "print(f\"{float(deg)}°C are {celsius_farenheit(deg)}°F\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Array tasks " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Arithmetic I \n", "\n", "Write a NumPy program to create an **8x8 matrix** and fill it with a **checkerboard pattern** (``1 = \"white\" and 0 = black``). Use **array indexing** to create the checkerboard.\n", "\n", "
\n", "\"vinyl\n", "
\n", "\n" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]\n", " [1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]\n", " [1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]\n", " [1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "checkboard_1 = np.array([[1,0,1,0,1,0,1,0],[0,1,0,1,0,1,0,1]]*4)\n", "print(checkboard_1)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]\n", " [1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]\n", " [1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]\n", " [1 0 1 0 1 0 1 0]\n", " [0 1 0 1 0 1 0 1]]\n" ] } ], "source": [ "checkboard_2 = np.ones((8,8)).astype(int)\n", "checkboard_2[::2,1::2] = 0\n", "checkboard_2[1::2,::2] = 0\n", "\n", "print(checkboard_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Arithmetic II (OPTIONAL -> difficult) \n", "\n", "Write the function ``same_elements(arr_1,arr_2), `` which returns the **common values** between the **two 1D arrays**.\n", "\n", "- Extend the function with **Type Hints** for the **input and return values** (Type array: ``np.nd_array``)\n", "- Create a **DocString**\n", "- Test the function with the following arrays:\n", "\n", "```python\n", "x = np.array([1,2,3,4,5,6,7,8])\n", "y = np.array([4,5,6])\n", "```" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 5 6]\n" ] } ], "source": [ "x = np.array([1,2,3,4,5,6,7,8])\n", "y = np.array([4,5,6])\n", "\n", "def same_elements(x:np.ndarray,y:np.ndarray)->np.ndarray:\n", " res = []\n", " for elem in x:\n", " if elem in y:\n", " res.append(elem)\n", " return np.array(res)\n", "\n", "print(same_elements(x,y))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }