3.3 & 3.4

Algorithms

  • Sequencing
    • do tasks in order
  • Selection
    • helps choose two different outcomes based off 1 decision
  • Iteration
    • if a condition is true, then it can repeat starting from a certain step until said condition is false
  • Flowcharts
    • show the function of an algorithms

Arithmetic Operation

  • subtraction (-)
  • addition (+)
  • multiplication (*)
  • division (/)
  • Getting the remainder (MOD) (%)
  • ORDER OF OPERATIONS MATTERS
    • reassigning variables using operations on other variables can get confusing

Arithmetic Hacks

Num1 = 50
Num2 = Num1 % 9 + 15                    # num2 = 20
Num3 = Num2 / Num1 + ( Num2 * 2 )       # num3 = 20/50 + 40 = 40.4
Num4 = Num3 + Num1 / 5 - 10             # num4 = 40.4 + 10 - 10
Result = Num4 - Num2                    # Result = 40.4 - 20 = 20.4
Num1 = 10
Num2 = Num1 % 3 * 4                     # num2 = 1 * 4 = 4
Num1 = Num2                             # num1 = 4
Num3 = Num1 * 3                         # num3 = 4 * 3 = 12
Result = Num3 % 2                       # Result = 0
valueA = 4
valueB = 90
valueC = 17
valueB = valueC - valueA                # valueB = 17 - 4 = 13
valueA = valueA * 10                    # valueA = 4 * 10 = 40
if valueB > 10:                         # valueB is 13, which is greater than 10
    print(valueC)                       # This will print 17
17
type = "curly"
color = "brown"
length = "short"
type = "straight"
hair = type + color + length            # hair = "straightbrownshort" (concatenation)
print(hair)                             # This will print straightbrownshort
straightbrownshort

Strings

  • Collection of character (#s, letters, spaces, special characters)
  • procedures can be used with strings
    • len() finds length of strings
  • concatenation combines strings
    • concat("cookie", "monster") outputs "cookiemonster"

Strings Hacks

Problem 1 CollegeBoard Code

Noun = "Mr.Mortenson" 
Adjective = "handsome" 
Adjective2 = "Very" 
Verb = "is" 
abrev = subtring(Noun, 1, 7) # Abbreviate "Mr.Mortensen" to "Mr.Mort"
yoda = concat(Adjective2, " ", Adjective, " ", abrev, " ",Verb, ".") # yoda = VeryhandsomeMr.Mortis
display[yoda]

Problem 1 Correct Code

Noun = "Mr.Mortenson" 
Adjective = "handsome" 
Adjective2 = "Very" 
Verb = "is" 
abrev = Noun[0:7]
yoda = abrev + " " + Verb + " " + Adjective2 + " " + Adjective + "."
print(yoda)
Mr.Mort is Very handsome.

Problem 2 CollegeBoard Code

cookie = "choclate" 
cookie2 = "rasin" 
len1 = len(cookie) / 2 # len1 = 4
len2 = len(cookie2) * 45 # len2 = 225
vote1 = (cookie, "vote", len2) # vote1 = choclatevote225
vote2 = (cookie2, "vote", len1) # vote2 = rasinvote4
votes = concat(vote1, " ", vote2) # votes = choclatevote225 rasinvote4
display[votes]

Problem 2 Correct Code

cookie = "choclate" 
cookie2 = "rasin" 
len1 = len(cookie) / 2 
len2 = len(cookie2) * 45 
vote1 = (cookie, "vote", len2) 
vote2 = (cookie2, "vote", len1) 
votes = "cookie has " + str(len1) + " votes and the second cookie has " + str(len2) + " votes."
print(votes)
cookie has 4.0 votes and the second cookie has 225 votes.