Vocab

Algorithm: a finite set of instruction that accomplish a task, it can be expressed by natural language, diagrams, and various other ways

  • Three parts of Algorithm: selection, sequence, and iteration
    • Sequence: the order of how to do something to achieve a result, similarly to how you follow the instructions from a teacher
num1 = 5
num2 = num1*6 - 20
num3 = num1*num2 - 2*6

result = num1* 6 + num2/2 - (num3/2)
print(result)
16.0
- selection: it allows an algorithm to make a decision based on if a condition is met
x = 3
y = 2
if x > y:
    print("{} is bigger than {}".format(x,y))
elif x < y:
    print("{} is smaller than {}".format(x,y))
else: 
    print("{} and {} are same".format(x,y))
3 is bigger than 2
- Iteration: loop and doing something again until a condition is met
x = 0
a = 0
while x < 5:
    a += x
    print(a)
    x +=1
0
1
3
6
10
  1. Addition: a + b
  2. Subtraction: a - b
  3. Multiplication: a * b
  4. Division: a/b
MO = 102 % 5
ad = 3 + MO
s = 6 - 7
m = 564*349
d = 1024/ 16
print(MO,ad, s, m, d)
2 5 -1 196836 64.0

Note

  • Algorithms are a finite set of instructions that accomplish a task. it has three parts, sequence, selection, and iteration
  • A sequence is the order of how to do something to achieve a result, similarly to how you follow the instructions from a teacher.
  • A selection allows an algorithm to make a decision based on if a condition is met, an example of this is when your car is out of fuel, you go to the gas station to fill your car, but if your car is full you wouldn't go to the gas station.
  • An iteration is a loop and doing something again until a condition is met, like you put away your computer when you are finished with your work.
  • Arithmetic uses addition, subtraction, division, multiplication, and modulus operator
  • Addition: a+b
  • Subtraction: a-b
  • Multiplication: a*b
  • Division: a/b
  • Modulus: a MOD b
  • (a and b can be string or number)
  • A string concatenation connects two or more string end-to-end to make a new string
  • Len() gives the character number
  • strings are variables and can be joined together through the print() command to make a statement

Hack 1

  • Set time to number to search for: sequence
  • Get next number in the list: sequence
  • If number = item, display "item found": selection
  • If there are more numbers in the list, go back to step 2: iteration, selection
  • Display "item no found": sequence

Hack 2

num1 = 5
num2 = num1 * 3
num3 = num2 / num1 * (9 % 2) * 4
result = (num3 % num1 + num2) % num3 * 3 / 5

the result will be...

num2 = 5 3 = 15
num3 = (15 / 5)
(9 % 2) 4 = 3 1 4 = 12
result = (12 % 3 + 15) % 12
3 / 5 = (2 + 15) % 12 3 / 5 = 17 % 12 3 / 5 = 5 * 3 / 5 = 3

crossword

  1. Iteration
  2. Selection
  3. Sequence

screenshot