From 88b224e82563b293503c48539679adebb2f95c7d Mon Sep 17 00:00:00 2001 From: eraycemagpurada Date: Mon, 25 Sep 2017 10:02:55 +0000 Subject: [PATCH] Done activity --- .../visualising-loops-checkpoint.ipynb | 414 ++++++++++++++++++ visualising-loops.ipynb | 209 +++++++-- 2 files changed, 597 insertions(+), 26 deletions(-) create mode 100644 .ipynb_checkpoints/visualising-loops-checkpoint.ipynb diff --git a/.ipynb_checkpoints/visualising-loops-checkpoint.ipynb b/.ipynb_checkpoints/visualising-loops-checkpoint.ipynb new file mode 100644 index 0000000..2868f48 --- /dev/null +++ b/.ipynb_checkpoints/visualising-loops-checkpoint.ipynb @@ -0,0 +1,414 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Python Notebooks block grid\n", + "\n", + "**ipythonblocks** is a module for practicing Python. It works in the IPython Notebook and gives you a grid of colors to manipulate while practicing for loops, if statements, and other aspects of Python. It's a lot of fun!\n", + "\n", + "[more about module ipythonblocks here](http://www.ipythonblocks.org/)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# you first have to import the module - either the whole thing like this: import ipythonblocks\n", + "# or just the BlockGrid data structure as follows:\n", + "from ipythonblocks import BlockGrid" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "grid = BlockGrid(15,15)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#draw a 15x15 grid, all blocks of the same colour\n", + "for block in grid:\n", + " block.rgb = (0,0,255)\n", + "grid" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#draw a grid where even colums are white and odd columns are black\n", + "for block in grid:\n", + " if block.col%2 == 0:\n", + " block.rgb = (0,0,0)\n", + " else:\n", + " block.rgb = (255,255,255)\n", + "grid" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#draw a grid where blocks in even rows and cols are white. Others are black\n", + "for block in grid:\n", + " if block.col%2 and block.row % 2 == 0:\n", + " block.rgb = (0,0,0)\n", + " else:\n", + " block.rgb = (255,255,255)\n", + "grid" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "#define the ada colours as RGB tuples\n", + "adaYellow=(245,225,52)\n", + "adaPurple=(149,96,159)\n", + "adaGreen=(161,200,84)\n", + "adaCoral=(236,98,113)\n", + "adaBlue=(108,184,231)\n", + "adaOrange=(246,131,82)\n", + "adaBlack=(9,20,8)\n", + "adaGrey=(134,136,140)\n", + "adaLightGrey=(211,212,211)\n", + "adaWhite=(255,255,255)\n", + "#define a list of these colours\n", + "adaColors=[adaYellow,adaPurple,adaGreen,adaBlue,adaOrange]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(245, 225, 52)\n", + "(149, 96, 159)\n", + "(161, 200, 84)\n", + "(108, 184, 231)\n", + "(246, 131, 82)\n" + ] + } + ], + "source": [ + "#step through the list of colours, print each one out\n", + "for color in adaColors:\n", + " print(color)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 (245, 225, 52)\n", + "1 (149, 96, 159)\n", + "2 (161, 200, 84)\n", + "3 (108, 184, 231)\n", + "4 (246, 131, 82)\n" + ] + } + ], + "source": [ + "#step thorugh a list of colours using their index numbers, print each one and its index out\n", + "for i in range(len(adaColors)):\n", + " print(i, \" \", adaColors[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "#define a 9x9 adaYellow grid\n", + "grid = BlockGrid(9,9,fill=adaYellow)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grid" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "i=0\n", + "for block in grid:\n", + " block.rgb = adaColors[i]\n", + " i=(i+1)%len(adaColors) #go to the next colour, but loop back to 0 if over 4\n", + "grid" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#start on a different color\n", + "i=4\n", + "for block in grid:\n", + " block.rgb = adaColors[i]\n", + " i=(i+2)%len(adaColors) #go forward 2 colours, but loop back to 0 if over 4\n", + "grid" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#here is how you used for loop indices to address each block in a different way\n", + "for row in range(9):\n", + " for col in range(9):\n", + " grid[row, col].rgb = adaColors[(col+row)%5]\n", + "grid\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#go row by row\n", + "for row in range(9):\n", + " #step through the column from the right\n", + " for col in range(8,-1,-1):\n", + " grid[row,col].rgb = adaColors[(row-col)%5]\n", + "grid " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "adaLogo = BlockGrid(8,8)\n", + "row=0\n", + "col=0\n", + "colour=0\n", + "\n", + "while row < 8:\n", + " while col < 8:\n", + " if row < 4 and col < 4 :\n", + " adaLogo[row,col].rgb=(0,0,0)\n", + " else :\n", + " adaLogo[row,col].rgb=(255,255,255)\n", + " \n", + " #add code here to fill the other 3 block of the ada logo\n", + " \n", + " if (row < 8 and row >= 4) and (col < 8 and col >= 4):\n", + " adaLogo[row, col].rgb = (0, 0, 0)\n", + " \n", + " if (row < 4 and row >= 0) and (col < 8 and col >= 4):\n", + " adaLogo[row, col].rgb = adaGreen\n", + " \n", + " if (row >= 4 and row < 8) and (col >= 0 and col < 4):\n", + " adaLogo[row, col].rgb = adaYellow\n", + " \n", + " \n", + " \n", + " col+=1 #go to the next column\n", + " \n", + " #after doing all the columns in this row \n", + " col=0 # go back to column zero\n", + " row+=1 # go to the next row\n", + " \n", + "adaLogo" + ] + } + ], + "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.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/visualising-loops.ipynb b/visualising-loops.ipynb index 0f1f738..2868f48 100644 --- a/visualising-loops.ipynb +++ b/visualising-loops.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -33,9 +33,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#draw a 15x15 grid, all blocks of the same colour\n", "for block in grid:\n", @@ -45,9 +59,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#draw a grid where even colums are white and odd columns are black\n", "for block in grid:\n", @@ -60,9 +88,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#draw a grid where blocks in even rows and cols are white. Others are black\n", "for block in grid:\n", @@ -75,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -96,9 +138,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(245, 225, 52)\n", + "(149, 96, 159)\n", + "(161, 200, 84)\n", + "(108, 184, 231)\n", + "(246, 131, 82)\n" + ] + } + ], "source": [ "#step through the list of colours, print each one out\n", "for color in adaColors:\n", @@ -107,9 +161,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 (245, 225, 52)\n", + "1 (149, 96, 159)\n", + "2 (161, 200, 84)\n", + "3 (108, 184, 231)\n", + "4 (246, 131, 82)\n" + ] + } + ], "source": [ "#step thorugh a list of colours using their index numbers, print each one and its index out\n", "for i in range(len(adaColors)):\n", @@ -118,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -128,20 +194,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "grid" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "scrolled": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "\n", "i=0\n", @@ -153,9 +247,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#start on a different color\n", "i=4\n", @@ -167,9 +275,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#here is how you used for loop indices to address each block in a different way\n", "for row in range(9):\n", @@ -180,9 +302,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#go row by row\n", "for row in range(9):\n", @@ -201,9 +337,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "adaLogo = BlockGrid(8,8)\n", "row=0\n", @@ -219,7 +369,14 @@ " \n", " #add code here to fill the other 3 block of the ada logo\n", " \n", + " if (row < 8 and row >= 4) and (col < 8 and col >= 4):\n", + " adaLogo[row, col].rgb = (0, 0, 0)\n", + " \n", + " if (row < 4 and row >= 0) and (col < 8 and col >= 4):\n", + " adaLogo[row, col].rgb = adaGreen\n", " \n", + " if (row >= 4 and row < 8) and (col >= 0 and col < 4):\n", + " adaLogo[row, col].rgb = adaYellow\n", " \n", " \n", " \n",