diff --git a/f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb b/f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb index d66f14f8e1b75b02da10e1750f44059678bd876e..8a2d2131f8028f441872fa73b3c71bc35888e038 100644 --- a/f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb +++ b/f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb @@ -114,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "d67a080f", "metadata": {}, "outputs": [ @@ -124,7 +124,7 @@ "992" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -143,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "bde8dc35", "metadata": {}, "outputs": [ @@ -153,7 +153,7 @@ "'21'" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -165,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "b0f87a2c", "metadata": {}, "outputs": [ @@ -175,7 +175,7 @@ "str" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -197,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "ba993090", "metadata": {}, "outputs": [ @@ -207,7 +207,7 @@ "'LEC001'" ] }, - "execution_count": 7, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -218,7 +218,7 @@ }, { "cell_type": "markdown", - "id": "6e3e3c0a", + "id": "d6edf4d5", "metadata": {}, "source": [ "### What are the age and pizza choices of the first 10 students? \n", @@ -227,8 +227,8 @@ }, { "cell_type": "code", - "execution_count": 10, - "id": "46ec7632", + "execution_count": 7, + "id": "883e25cc", "metadata": {}, "outputs": [ { @@ -264,7 +264,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 8, "id": "e024c488", "metadata": {}, "outputs": [ @@ -296,7 +296,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 9, "id": "38bd778a", "metadata": {}, "outputs": [ @@ -336,7 +336,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 10, "id": "ea77e0cd", "metadata": {}, "outputs": [ @@ -351,7 +351,7 @@ "source": [ "min_age = int(project.get_age(0) ) #assume first student is youngest,and has age\n", "for i in range(project.count()):\n", - " if project.get_lecture != \"LEC001\":\n", + " if project.get_lecture(i) != \"LEC001\":\n", " continue\n", " if project.get_age(i) == '': \n", " continue\n", @@ -363,6 +363,36 @@ "print(min_age)" ] }, + { + "cell_type": "code", + "execution_count": 15, + "id": "c6b48319", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "17\n" + ] + } + ], + "source": [ + "# REDO this using None as an initial value for min_age\n", + "min_age = None\n", + "for i in range(project.count()):\n", + " if project.get_lecture(i) != \"LEC001\":\n", + " continue\n", + " if project.get_age(i) == '': \n", + " continue\n", + " age = int(project.get_age(i))\n", + " if age < 0 or age > 100:\n", + " continue\n", + " if min_age == None or age < min_age:\n", + " min_age = age\n", + "print(min_age)" + ] + }, { "cell_type": "markdown", "id": "296c2a49", @@ -373,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 11, "id": "8b7c8367", "metadata": {}, "outputs": [ @@ -383,7 +413,7 @@ "19.608180839612487" ] }, - "execution_count": 38, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -415,24 +445,48 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 16, "id": "a524873b", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "17" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "17 Statistics\n" + ] } ], "source": [ "# we will do this in lecture on Wednesday\n", - " " + "# use None\n", + "# keep track of two variables, the min_age and index of the min_age\n", + "\n", + " \n", + "min_age = None\n", + "min_age_idx = None\n", + "for i in range(project.count()):\n", + " if project.get_lecture(i) != \"LEC001\":\n", + " continue\n", + " if project.get_age(i) == '': \n", + " continue\n", + " age = int(project.get_age(i))\n", + " if age < 0 or age > 100:\n", + " continue\n", + " if min_age == None or age < min_age:\n", + " min_age = age\n", + " min_age_idx = i\n", + "print(min_age, project.get_major(min_age_idx))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "84adf236", + "metadata": {}, + "outputs": [], + "source": [ + "# help(project)" ] }, { @@ -445,7 +499,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 21, "id": "fada2a40", "metadata": {}, "outputs": [ @@ -453,22 +507,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "19 <class 'str'>\n" + "None\n" ] } ], "source": [ + "# REDO THIS USING NONE\n", + "\n", + "target_zip = 53715\n", + "target_lecture = \"LEC001\"\n", + "age_of_target = None\n", "for i in range(project.count()):\n", - " # put a print statement here\n", - " if project.get_lecture(i) == \"LEC002\" and project.get_zip_code(i) == \"53715\":\n", - " print(project.get_age(i), type(project.get_age(i)))\n", - " break" + " if project.get_lecture(i) == target_lecture and project.get_zip_code(i) == target_zip:\n", + " age_of_target = project.get_age(i)\n", + " break # often in a search we break to be efficient\n", + "print(age_of_target)" ] }, { "cell_type": "code", "execution_count": 25, - "id": "772f7a58", + "id": "7a3fdbf6", "metadata": {}, "outputs": [ {