{ “cells”: [ { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “# Unit 3 Section 8-10 Blog\n”, “- title: Unit 3 Section 3-4 Blog\n”, “- tags: [week 14]\n”, “- toc: true\n”, “- comments: true” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Section 8 Big Ideas/Learning Objectives\n”, “- Understanding What is Iteration\n”, “- Using for and while loops\n”, “\n”, “Section 10 Big Ideas/Learning Objectives\n”, “- Understanding how to edit lists by adding, inserting, and removing data\n”, “- Using loops to iterate through lists and abstract data\n”, “- Determine the results or side effects of iteration statements\n”, “- Write sorting algorithms using iteration\n”, “\n”, “Section 8 Necessary Vocabulary\n”, “\n”, “Iteration - Repitition of a Process\n”, “For Loop - FOR LOOP repeats a function for a set number of times; I is the number of times repeated\n”, “\n”, “While Loop - The while loop is used to repeat a section of code an unknown number of times until a specific condition is met\n”, “\n”, “Initialization - What sets the counter variable to a starting value. For example (var i = 0) represents an initial value of 0.\n”, “\n”, “Condition - Allows the computer to know whether or not to keep repeating the loop.\n”, “increment/decrement - Modifies the counter variable after each repetition.\n”, “\n”, “Section 10 Necessary Vocabulary\n”, “- Indexing / List Index - The position of an element in a list, starting from 0 append, remove, pop - Various methods, append adds an element to the end, remove removes at an index, and pop removes the last item.\n”, “- Elements [in a list] - An item in a list.\n”, “- Nesting - Having one data type or function inside another data type or function, such as lists or loops. array - Another name for a list, depends on the language\n”, “\n”, “What is Iteration?\n”, “An Iterative statements are also called loops, and they repeat themselves over and over until the condition for stopping is met.\n”, “\n”, “In College Board’s Pseudocode, the first is a REPEAT n TIMES loop, where the n represents some number.\n”, “\n”, “The second type of loop is a REPEAT UNTIL (condition) loop, where the loop will continue to run until a condition is met.\n”, “\n”, “A while loop is very similar to an if conditional, except that a while is continually executed until it’s no longer true and an if is only executed once.” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Practice\n”, “\n”, “Consider the Following Code Segment. What is the Counter Variable and the Condition set as?” ] }, { “cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [ “for (var i = 10; i > 0; i–) {\n”, “ println(i);\n”, “}” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Answer:Counter Variable: 10, Condition: Greater than 0\n” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Practice\n”, “\n”, “Consider the Following Code Segment. How Many times will print(ln) be called?\n”, “\n” ] }, { “cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [ “ for (var i = 1; i <= 10; i++) {\n”, “ for (var j = 1; j <= 10; j++) {\n”, “ println(i * j);\n”, “ }\n”, “}” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Answer: 100 Times.The computer will execute the outer for loop 10 times (starting at i = 1 and ending after i = 10). Each time that it executes the outer loop, it will execute the inner for loop 10 times (starting at j = 1 and ending after j = 10). Each inner execution will call println() once.10 times 10 times 1 is 100, so the computer will call println() 100 times.\n” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Practice\n”, “\n”, “Consider the Following Code Segment. What is it initial value? What does the while loop check?” ] }, { “cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [ “var numRabbits = 2;\n”, “var numYears = 0;\n”, “while (numRabbits <= 100) {\n”, “ numRabbits += (numRabbits/2 * 30);\n”, “ numYears++;\n”, “}\n” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “2 Rabbits. The while loop checks that the number of rabbits is less than 100. As long as the population is less the 100, the code inside the loop continues to run” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Exercise 1\n”, “\n”, “\n”, “\n”, “This exercise was quite easy since I overheard you talking about list.reverse, but the instructions asked us to do it with iteration, so I used a for loop and made it run for every value inside the list. The code that I used here was really simple, and all I did was take the original list and then reverse it into a new list and then print that.\n”, “\n”, “\n”, “ \n”, “\n”, “By using a for loop here, I was able to actually fufill the iteration requirement and able to print the reverse order. However, I am not sure if we were supposed to print each number on a separate line, but I could not figure that out. I assume we would use a for loop function and then print something like i, it would be interesting to find out how to do this, and see if I was close.\n”, “\n”, “Excercise 2\n”, “\n” ] }, { “cell_type”: “code”, “execution_count”: 2, “metadata”: {}, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “array before sort [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]\n”, “array after sort [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n” ] } ], “source”: [ “list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]\n”, “print(f"array before sort {list}")\n”, “def insertion_sort(list):\n”, “ for index in range(1,len(list)): # repeats through length of the array\n”, “ value = list[index]\n”, “ i = index - 1\n”, “ while i >= 0:\n”, “ if value < list[i]:\n”, “ list[i+1] = list[i] # shift number in slot i to the right\n”, “ list[i] = value # shift value left into slot i\n”, “ i = i - 1\n”, “ else:\n”, “ break\n”, “\n”, “IS = insertion_sort(list)\n”, “print(f"array after sort {list}")” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “ \n”, “ \n”, “ \n”, “ \n” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “I got Question #10 on the quiz because I wasn’t exactly sure how many for loops we needed to use, but I knew that we needed to use a for loop because. There was 3 lists in the list, so I thought we could just use one for loop in order to iterate through. I believe that using one for loop would work but be less efficient, I think that by using 2 for loops it iterates through the lists much faster.” ] }, { “cell_type”: “markdown”, “metadata”: {}, “source”: [ “Overall this homewortk was quite challenging but I think I learned a lot through trial and error. I did not eknow much at the start but I turned to learn how to do some of it making it easier and easier the more I did it. I felt like I knew everything from the quiz and could answer every question. I just had a little bit of a problem with Question 10. I was not sure how many loops are exactly needed to iterate through the lists efficiently. Overall, I felt that the quiz was pretty easy and everything that was in the quiz was covered during the lesson” ] } ], “metadata”: { “kernelspec”: { “display_name”: “Python 3.10.7 64-bit”, “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.10.7” }, “orig_nbformat”: 4, “vscode”: { “interpreter”: { “hash”: “aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49” } } }, “nbformat”: 4, “nbformat_minor”: 2 }