{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Exploratory Computing with Python\n",
"*Developed by Mark Bakker*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Test for Notebooks 1 - 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1\n",
"\n",
"Write a function that plots a square centered at the origin. The length of the side of the square is $L$. The input of the function is the length $L$. \n",
"\n",
"Next, write a loop that calls your function for $L$ going from 1 up to and including 5 with steps of 1. \n",
"Use `plt.axis('scaled')` to make sure your squares look like squares and not rectangles."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer to Exercise 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2\n",
"Write a function called `countsquares` that takes as input the filename of a file that consists of a bunch of numbers separated by spaces. Inside the function, you must first read the numbers from the filename and then you must determine how many of these numbers are a perfect square. A perfect square means that the root of the number is an integer (i.e., 1, 4, 16, 25, etc.). The function returns the number of perfect squares in the file. Note: to convert a number to an integer, use the `int` function. \n",
"\n",
"Demonstrate that your function works by using the file `numbers2016.txt` and execute the following line of code:\n",
"\n",
"`print(countsquares('numbers2016.txt'), 'numbers are perfect squares')`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer to Exercise 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3\n",
"The file `quiz_answers.dat` contains the result of a small multiple-choice quiz with 8 questions. Every row contains the 8 answers to the quiz of one of the students. Your job is to count how many students had the correct answer for each question. Write a double loop to compute the number of correct answers for each question and present the results in a bar graph. The correct answers for the 8 questions of the quiz are: `a b c d a b c d`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer to Exercise 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 4\n",
"Create the flag of Norway as shown in the figure below.\n",
"\n",
"\n",
"\n",
"Create an array of size 40 by 55. Fill the array with zeros, ones and two-s to represent the blue, white, and red parts, respectively. You may only use a maximum of 5 assignment statements to set the correct values for the colors. Make sure you plot the flag using the colormap `bwr` (which stands for blue-white-red)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer to Exercise 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 5\n",
"Write a function that computes the percentage of grades that is above a given value. The function takes as input arguments an array with grades between 1 and 10 and a minimum value and returns the precentage of grades (so between 0% and 100%) that are above or equal to that value. Demonstrate that your function works by loading the grades in the file `schoolgrades2016.txt` and print the result of the function to the screen with two decimal places, given a minimum value of 7."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer to Exercise 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Brief answers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer to Exercise 1\n",
"\n",
"You probably know what this should look like\n",
"\n",
"Back to Exercise 1\n",
"\n",
"Answer to Exercise 2\n",
"\n",
"93\n",
"\n",
"Back to Exercise 2\n",
"\n",
"Answer to Exercise 3\n",
"\n",
"Q1: 4, Q2: 4, Q3: 5, Q4:3, Q5:5, Q6:5, Q7:4, Q8:4\n",
"\n",
"Back to Exercise 3\n",
"\n",
"Answer to Exercise 4\n",
"\n",
"Your graph should look like the provided figure\n",
"\n",
"Back to Exercise 4\n",
"\n",
"Answer to Exercise 5\n",
"\n",
"31.3\n",
"\n",
"Back to Exercise 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}