APP INVENTOR Test Review
Main Concepts App Inventor Lists Creating Random Numbers Variables Searching and Sorting Data Linear Search Binary Search Selection Sort Quick Sort Abstraction Modulus Division
Lists
Lists Lists allow you to store a large group of data within a variable. Lists are an important part of computer science as we learned with searching and sorting data (see notes below)
Lists Each location in a list is called an index. Example: the second item (no way) found in the list below is found in index 2. The length of a list is determined by how many items are found in the list. Example: The list below has a length of 8.
Random Numbers
Random Numbers Often in programs we need to create random numbers. The random blocks are found in the Math block category. The block above will create a random number between 1 and 100 including 1 and 100. We used this block in the magic 8 ball program.
Variables
Variables A variable is a named place in memory. Each variable has a name and stores a value. Variables can hold: Numbers Colors Words Names of files media files Variables can only hold one value at a time but that value can change.
Creating a Variable App Inventor 1. Navigate to the Blocks View 2. Select the Variable built in category of blocks 3. Select the initialize global name to block 4. Give it a value (variables can hold, numbers, words, lists, colors, logic blocks) 5. Assign the variable a meaningful name
Variables Initialize a variable means to create a new variable and give it a value: Variable has 2 blocks Setter Blocks Used to set a new value stored in a variable Getter Blocks Used to get the value stored in a variable
Searching and Sorting Data
Searching And Sorting Why? Most of what a computer does is searching and sorting data. Because of this, it is a widely studied part of computer science. Producing many solutions to the problem of sorting and search data. We looked at 2 searches Linear Search Binary Search 2 Sorts Selection Quick
Searching Linear Search Linear Search: Checks each element in a sequence until the desired element is found or the list is exhausted, meaning that the desired target is not in the list. Used if the list that is to be searched is not ordered. Generally, this technique is used for small lists, or lists that are not searched often. Key Points: Speed: This search method is on average slower than the Binary Search. Order: This search can be used if the data is sorted or Not. Best Case Scenario: The item is found in the first comparison. Worst Case Scenario: The item is found in the last index of the list or it is not located in the list at all.
Linear Search - Example
Searching: Binary Search Binary Search: starts by comparing the target (the value that you are looking for) with the value found in the middle index of the list. If the value found in the middle index is greater than that of the target, you can ignore all of the data that is after that middle index. The process is then repeated with the remaining data until we either find the target value or satisfy ourselves that the target is not in the list. Find the new middle, compare, and remove the half where the target is not found.
Searching Binary Search Key points: Speed: This search method is on average faster than a Linear Search. Order: This search can only be used if the data is sorted. Best Case Scenario: The item will be found faster than a Linear Search. Worst Case Scenario: The item will be found just as fast as a Linear Search.
Binary Search: Example
Sorting- Selection Sort 1. Divide the list to be sorted into a sorted portion at the front (initially empty) and an unsorted portion at the end (initially the whole list). 2. Find the smallest element in the unsorted list: 1. Select the first element of the unsorted list as the initial candidate. 2. Compare the candidate to each element of the unsorted list in turn, replacing the candidate with the current element if the current element is smaller. 3. Once the end of the unsorted list is reached, the candidate is the smallest element. 3. Swap the smallest element found in the previous step with the first element in the unsorted list, thus extending the sorted list by one element. 4. Repeat the steps 2 and 3 above until only one element remains in the unsorted list.
Selection Sort - Key points Difficulty: Easy. The simplest sorting technique. Data Set Size: Very small. This is a good algorithm to sort a small number of elements because the number of comparisons is low. Speed: Slow... maybe. It is potentially very slow if you are sorting a large set of data of 100,000 phone numbers or example.
Sorting - Quick Sort 1. Randomly pick an element, called a pivot, from the list. 2. Reorder the list so that all elements with values less than the pivot are placed before the pivot, while all elements with values greater than the pivot are placed after it (equal values can go either way). 3. After this partitioning, the pivot is in its final position. 4. Repeat the process by sorting the sub-list of lesser elements and the sub-list of greater elements. (Source: Wikipedia.org)
Quick Sort Key Points Difficulty: Low to medium. More complex than a Selection Sort but still quite simple to understand. Data Set Size: Large. This is a good algorithm to sort a large number of elements because the number of comparisons is small in comparison to the number of items in the list. Speed: Faster... maybe. It is potentially very fast if you are sorting a large set of data of 100,000 phone numbers or example. It will never be slower than a selection sort
Abstraction
Abstraction - Definition In computer science the term abstract means to simplify, generalize or to eliminate irrelevant detail. In computer science, we use the term to mean simplifying or eliminating irrelevant detail so that we can create a model or plan a solution to a problem. An abstraction is a general representation of something -- of some person or place or event or process. Abstractions are formed by including only those details needed to make the abstraction useful to us in some way.
Abstraction - Example A map is an example of abstraction because it removes all unnecessary information from the map. It only keeps what is important; general aspects of the geography. It doesn t show every part of the geography. It can t. It can t show every tree and flower. The person who made the map abstracted out the irrelevant information and left the most important information for finding the treasure.
Abstraction - Example A variable is an example of abstraction. In a program a variable can hold many different values. The programmer doesn t have to worry about every single value that it holds. They just have to worry if the variable value can be used in an equation. The specific value in the variable number is not important. What is important is the equation. It will always add one to the current value of number.
Modulus Division
Modulus Division Modulus division returns the remainder after the division of one number by another. Rather than returning the quotient, modulus division returns the remainder. 26 % 5 = 1 (% = Modulus division) The modulus division block is found in the math category
Modulus division We used modulus division in the list_picker practice app We used it to know if the length of the list is odd or even. If it is odd then the modulus division will result in a number. If it results in a 0 then the list is even. 4 % 2 = 0 (even) 5 % 2 = 1 (odd)