Lesson 3.3-3.4 Notes and Homework
This is the jupyter notebook for lessons 3.3-3.4.
- What is an Algorithm?
- Math Operations
- Different Ways Values can be stored in variables
- Sequencing
- Strings
What is an Algorithm?
An algorithm: a set of instructions that can accomplish a specific task.
An Algorithm Has Three Components
- Sequencing: Algorithms do tasks in the order of specification.
- Selection: Helps choose two different outcomes based off a decision.
- Iteration: If a condition is true, then the code can repeat.
Algorithms Can Be Represented in Two Ways
- Flowcharts: Use shapes and arrows to represent the steps of an algorithm.
- Pseudocode: A blend of human language and coding format.
Hacks: Jamboard Flowchart
score = 0 # 1
score = newScore # 2
score = newScore + 2 # 3
avgScore = allscores(20, 60, 80) # 4
Num1 = 50
Num2 = Num1 % 9 + 15
Num3 = Num2 / Num1 + ( Num2 * 2 )
Num4 = Num3 + Num1 / 5 - 10
Result = Num4 - Num2
# Result should be 20.4
Num1 = 10
Num2 = Num1 % 3 * 4
Num1 = Num2
Num3 = Num1 * 3
Result = Num3 % 2
# Result should be 0
valueA = 4
valueB = 90
valueC = 17
valueB = valueC - valueA
valueA = valueA * 10
if valueB > 10:
print(valueC)
# valueB is 86 so it should print valueC which is 17
type = "curly"
color = "brown"
length = "short"
type = "straight"
hair = type + color + length
print(hair)
# hair is straightbrownshort
Strings
String: a collection of characters(numbers, letters, spaces, special symbols, etc.)
Ways to use strings:
len()
: find the length of a string
lower()
: convert to lowercase
Pseudocode:
len()
: returns the length of a string
concat()
: returns a string made up of the concatenated strings ex. concat("string1", "string2") would return string1string2
substring()
: returns the characters from the string beginning at the at the first position to the last so an example of this would be substring ("abcdefghijk", 2, 5)
would print bcde
(pseudocode starts at 1)
Hacks
Find the result of the following problems. Then convert the pseudocode to working python code using your knowledge of python string operators.
Problem 1
Noun = "Mr.Mortenson"
Adjective = "handsome"
Adjective2 = "Very"
Verb = "is"
abrev = Noun[0:7]
yoda = Adjective2+ " "+ Adjective+ " "+ abrev+ " "+Verb+ "."
print(yoda)
Problem 2
cookie = "choclate"
cookie2 = "raisin"
len1 = len(cookie) / 2
len2 = len(cookie2) * 45
vote1 = cookie + " vote " + str(len2)
vote2 = cookie2 + " vote " + str(len1)
votes = vote1+ " " + vote2
print(votes)