Algorithms

An Algorithm has three components:

  • Sequencing
  • Selection
  • Iteration

Operations

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modules (%)

TEST BELOW:

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           
print(hair)                             
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"

Problem 2 solved

Variables

  • Values stored in a variable can be numerical

  • The result of an operation is stored in a variable.

  • The result of the procedure is also stored in the variable.

Sequencing

Changes the overall outcome since the order changes.

Tracking Variables

var1 = 9
var2 = 7
var3 = 2

#var = var1 + 5
#var 2 = var1 - var3

print(var1)
print(var2)
print(var3)
9
7
2

String

Collection of characters, can be any character. A string is a collection of characters.

  • len() finds the lenght of a string
  • lower () to convert to lowercase
  • concat() returns a string made up of the concatenated strings. Which is combining 2 more strings to make 1 string.
  • substring()
  • PSEUDOCODE STARTS AT 1
  • CB uses brackets and says display.

Problem Set [1-4]

Num1 = 50
Num2 = Num1 % 9 + 15 # 50/9 = 5 remainder --> add 15 = 20
Num3 = Num2 / Num1 + ( Num2 * 2 ) # 20/50 + 40 = 40.4
Num4 = Num3 + Num1 / 5 - 10 # 40.4 + 50/5 - 10 = 40.4
Result = Num4 - Num2 # 40.4 - 20
print(Result)
20.4
Num1 = 10
Num2 = Num1 % 3 * 4 # 10/3 * 4
Num1 = Num2 
Num3 = Num1 * 3
Result = Num3 % 2
print(Result)
0
valueA = 4
valueB = 90
valueC = 17
valueB = valueC - valueA
valueA = valueA * 10
if valueB > 10:
    print(valueC)
17
type = "curly"
color = "brown"
length = "short"
type2 = "straight"
hair = type + color + length + type2
print(hair)
curlybrownshortstraight

Problem Set 2

Noun = "Mr.Mortensen" 
Adjective = "handsome" 
Adjective2 = "extremely" 
Verb = "is" 
abrev = Noun[0:7]
yoda = abrev + " " + Verb + " " + Adjective2 + " " + Adjective
print(yoda)
Mr.Mort is extremely handsome
cookie = "chocolate" 
cookie2 = "rasin" 
len1 = len(cookie) / 2 #MANUALLY CALCULATED
len2 = len(cookie2) * 45 
vote1 = (cookie, "vote", len2) 
vote2 = (cookie2, "vote", len1) 
votes = "the first Cookie has " + str(len1) + " and the second Cookie has " + str(len2)
print(votes)
the first Cookie has 4.5 and the second Cookie has 225