### renaming

parent 9e9cb382
 { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 'Sort' as a Function\n", "\n", "What can we do if we want to sort many lists? Rather than writing code to loop over each list, we can encapsulate the 'sort' algorithm in a function. Then we can call the function as often as we like!\n", "\n", "##### Make 'sort' into a function\n", "\n", "Modify the code below to populate a list with a set amount of random numbers, then to sort that list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Original: one-time sort with loops\n", "import random\n", "\n", "# create a list with random numbers\n", "myarray = []\n", "for i in range(10):\n", " myarray.append(random.randint(1, 100))\n", "\n", "print(\"orginal myarray %s\" % myarray)\n", "\n", "# sort the list: each iteration moves the smallest remaining number to position 'i'\n", "for i in range(len(myarray)):\n", " for j in range(i+1, len(myarray)):\n", " if myarray[j] < myarray[i]:\n", " tmp = myarray[i]\n", " myarray[i] = myarray[j]\n", " myarray[j] = tmp\n", "\n", "print(\"sorted myarray %s\" % myarray)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now sort using functions!\n", "\n", "\n", "# function to fill and array with 'num_items' elements of random numbers between 1 and 100\n", "#def populate_random(array, num_items):\n", " # code populating a list here\n", "\n", "\n", "# function to sort a list from lowest to highest\n", "#def sort(array):\n", " # code sort algorithm here \n", "\n", "\n", "arr1 = []\n", "#populate_random(arr1, 10)\n", "print(\"\")\n", "print(\"orginal arr1 %s\" % arr1)\n", "#sort(arr1)\n", "print(\"sorted arr1 %s\" % arr1)\n", "\n", "\n", "arr2 = []\n", "#populate_random(arr2, 6)\n", "print(\"\")\n", "print(\"orginal arr2 %s\" % arr2)\n", "#sort(arr2)\n", "print(\"sorted arr2 %s\" % arr2)\n", "\n", "arr3 = []\n", "#populate_random(arr3, 14)\n", "print(\"\")\n", "print(\"orginal arr3 %s\" % arr3)\n", "#sort(arr3)\n", "print(\"sorted arr3 %s\" % arr3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use the 'sort' algorithm: How to tell if two strings contain the same characters?\n", "\n", "Given two strings (such as `\"the morse code\"` and `\"here come dots\"`), do they contain the exact same letters (and the exact same amount of each letter)?\n", "\n", "One way to find out would be to loop through the characters in the first string and count how often each letter occurs. Then we do the same thing with the second string and compare the frequency of each character. If all frequencies match, the two strings contain the same characters!\n", "\n", "However, this approach is a bit heavy handed. Can you think of a better way using 'sort'?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "string1 = \"the morse code\"\n", "string2 = \"here come dots\"\n", "string3 = \"a decimal this\"\n", "\n", "# convert the strings to a list of characters\n", "string1_chars = list(string1)\n", "string2_chars = list(string2)\n", "string3_chars = list(string3)\n", "\n", "\n", "# compare the characters to see if they are the same\n", "# code here!\n", "\n", "\n", "# if characters in string1 are the same as characters in string2\n", "# print `string1` has same characters as `string2`\n", "# else\n", "# print `string1` has different characters from `string2`\n", "\n", "# if characters in string1 are the same as characters in string3\n", "# print `string1` has same characters as `string3`\n", "# else\n", "# print `string1` has different characters from `string3`" ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }
Ex4.2-Turtle.py 0 → 100644
 #! /usr/bin/env python import time red=[1.0, 0.0, 0.0] green=[0.0, 1.0, 0.0] blue=[0.0, 1.0, 0.0] yellow=[1.0, 1.0, 0.0] purple=[0.5, 0.0, 1.0] orange=[1.0, 0.5, 0.0] cyan=[0.0, 1.0, 1.0] black=[0.0, 0.0, 0.0] white=[1.0, 1.0, 1.0] import sys, turtle def mysquare(size, color=black, filled=True): t.color(color) if filled: t.begin_fill() for x in range(4): t.forward(size) t.left(90) if filled: t.end_fill() def myrectangle(size1, size2, color=black, filled=True): t.color(color) if filled: t.begin_fill() for x in range(2): t.forward(size1) t.left(90) t.forward(size2) t.left(90) if filled: t.end_fill() def mycircle(size, color=yellow, filled=True): t.color(color) if filled: t.begin_fill() t.circle(size) if filled: t.end_fill() def mytriangle(size, color=red, filled=True): t.color(color) if filled: t.begin_fill() for x in range(3): t.forward(size) t.left(120) if filled: t.end_fill() t=turtle.Pen() t.up() # Draw the face t.right(90) t.forward(350) t.left(90) t.down() mycircle(350) t.up() # Draw the left eye t.left(90) t.forward(450) t.left(90) t.forward(200) t.right(180) t.down() mysquare(100) t.up() # Draw the right eye # Move to the correct position # (HINT: there is one blank line for each turn/move required) t.down() mysquare(100) t.up() draw_nose=False # Draw the nose # Move to the correct position # (HINT: there is one blank line for each turn/move required) # Dont for get to indent. if (draw_nose): t.down() mytriangle(100) t.up() draw_mouth=False # Draw the mouth # Move to the correct position # (HINT: there is one blank line for each turn/move required) # Dont for get to indent. if (draw_mouth): t.down() myrectangle(400,75) t.up() # Feeling adventurous? # Draw the teeth # First the bottom row of 3 draw_bottom_tooth_l=False # Left tooth # (HINT: there is one blank line for each turn/move required) # Dont for get to indent. if (draw_bottom_tooth_l): t.down() mytriangle(50, yellow) t.up() draw_bottom_tooth_c=False # Center tooth # (HINT: there is one blank line for each turn/move required) # Dont for get to indent. if (draw_bottom_tooth_c): t.down() mytriangle(50, yellow) t.up() draw_bottom_tooth_r=False # Right tooth # (HINT: there is one blank line for each turn/move required) # Dont for get to indent. if (draw_bottom_tooth_c): t.down() mytriangle(50, yellow) t.up() # Then the top row of 2 draw_top_tooth_r=False # Right tooth # (HINT: there is one blank line for each turn/move required) # Dont for get to indent. if (draw_top_tooth_r): t.down() mytriangle(50, yellow) t.up() draw_top_tooth_l=False # Left tooth # (HINT: there is one blank line for each turn/move required) # Dont for get to indent. if (draw_top_tooth_l): t.down() mytriangle(50, yellow) t.up() # Now move the turtle/curser out of the way t.forward(800) # Save the image as a postscript file ts=turtle.getscreen() ts.getcanvas().postscript(file='my_name.eps')