Algorithms sound somewhat spooky, mysterious, and complicated, but honestly they are just recipes to solve a problem, e.g. sort a list, find the shortest path through a maze, reverse my name.
Algorithms are NOT code!
Algorithms are language agnostic descriptions of problem solving steps, i.e. English description to solve a problem.
Why are they important? Well, programmers are lazy and they don't need to solve the same problem over and over again when it's already solved. Instead, programmers use already specified algorithms and convert them into code.
Practice With Algorithms (5 minutes)
Task: You are given a list of prices associated with items, calculate the sum price of all the items on the list and apply a sales tax on it of 4%.
Describe an algorithm that tells me the total price.
Remember NO code. Write some steps you would give a little kid with a calculator to solve this problem. The more specific, the better, remember you're giving these to a kid.
Practice With Algorithms Cont. (5 minutes)
Task: Given two same length arrays of numbers, array1 and array2, print "Found one!" if the two numbers at the same index are the same.
For example, arrayOne = {1, 2, 3} and arrayTwo = {3, 2, 1}, the algorithm should only print "Found one!" when it hits index 2.
Describe the algorithm (which means NO CODE) that solves this problem.
"Conjunction Junction, What's Your Function?": What are functions?
Functions are pretty awesome. They let us reuse code when we have to do something over and over again. In general, functions take in input (called parameters or arguments) and return some output.
Functions have three parts: the name, the return type (i.e. the output type), and the arguments/parameters (i.e. the inputs). Identify those three below.
booleanfoo(int[] a){
for (int i = 0; i < a.length; i++) {
if (a[i] < 0) {
returntrue;
}
}
returnfalse;
} // Bonus: What does this function do? What's a better name for it?
Practice With Functions (10 minutes)
Remember describing an algorithm to calculate a grocery store total? Fill out the blanks below to make that algorithm a reality.
Give us feedback! Tell us where we can improve and help you.
Have a good weekend!
Solutions Section
Practice With Algorithms (Solution)
(1) have a variable called total set to zero(2) go through the list of prices one at a time:
(2.1) add price p to total(3) multiply 1.04 to the total and make that the new total(4) read the total out
Practice With Algorithms Cont. (Solution)
(1) loop through 0 to the length of the list - 1 (because index by 0)
(1.1) set a to the value stored in list1 at our index(1.2) set b to the value stored in list2 at our index(1.3) check if a and b are the same thing:
(1.3.1)iftrue, then print "Found one!"
Practice With Functions (Solution)
doublecalculateTotal(double[] prices){
double total = 0;
for (int i = 0; i < prices.length; i++) {
total += prices[i];
}
return total * 1.04;
}
Practice With Functions (Solution Cont.)
intcountMatches(int[] firstArr, int[] secondArr){
int matches = 0;
// Remember the arrays are the same length.for (int i = 0; i < firstArr.length; i++) {
if (firstArr[i] == secondArr[i]) {
matches++;
}
}
return matches;
}
Practice With While Loops (Solution)
int i = 4096;
while (i > 0) {
System.out.println(i);
i = i / 2; // or i /= 2;
}