Commit 52206d18 authored by Tommy Marrinan's avatar Tommy Marrinan
Browse files

adding function sort notebook

parent 7717feb9
{
"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": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"orginal myarray [80, 75, 59, 69, 10, 94, 100, 20, 85, 69]\n",
"sorted myarray [10, 20, 59, 69, 69, 75, 80, 85, 94, 100]\n"
]
}
],
"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\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": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"orginal arr1 []\n",
"sorted arr1 []\n",
"\n",
"orginal arr2 []\n",
"sorted arr2 []\n",
"\n",
"orginal arr3 []\n",
"sorted arr3 []\n"
]
}
],
"source": [
"# Now sort using functions!\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",
"# function to sort a list from lowest to highest\n",
"#def sort(array):\n",
" # code sort algorithm here \n",
" \n",
"arr1 = []\n",
"#populate_random(arr1, 10)\n",
"print(\"\\norginal arr1 %s\" % arr1)\n",
"#sort(arr1)\n",
"print(\"sorted arr1 %s\" % arr1)\n",
"\n",
"\n",
"arr2 = []\n",
"#populate_random(arr2, 6)\n",
"print(\"\\norginal arr2 %s\" % arr2)\n",
"#sort(arr2)\n",
"print(\"sorted arr2 %s\" % arr2)\n",
"\n",
"arr3 = []\n",
"#populate_random(arr3, 14)\n",
"print(\"\\norginal 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": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'the morse code' has different chars from 'here come dots'\n",
"'the morse code' has different chars from 'a decimal this'\n"
]
}
],
"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",
"# compare the characters to see if they are the same\n",
"# code here!\n",
"\n",
"if string1_chars == string2_chars:\n",
" print(\"'%s' has same chars as '%s'\" % (string1, string2))\n",
"else:\n",
" print(\"'%s' has different chars from '%s'\" % (string1, string2))\n",
"\n",
"if string1_chars == string3_chars:\n",
" print(\"'%s' has same chars as '%s'\" % (string1, string3))\n",
"else:\n",
" print(\"'%s' has different chars from '%s'\" % (string1, 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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment