Take some additional notes that you would like here for 3.12 and 3.13. We will be looking for additional notes from the presentation.
- need to examine code line by line to determine what a procedure does
- proceude may or may not produce value
- proceudre needs to be defined and called
- proceudral abstracation: only know what is not done, not how
What are procedures?
Fill in the blanks please:
Procedure: named group of programming instructions that may have parameters and return values
Parameters: input values of a procedure
Arguments: specify the values of the parameters when a procedure is called
Modularity: separating a program's functions into independent pieces or blocks
Procedural Abstraction: provides a name for a process that allows a procedure to be used only knowing WHAT it does, not HOW it does it
What are some other names for procedures?: method, function
Why are procedures effective?: you can alter result without changing changing calls to program, you can breeak up the code with modularity
decimal = 7
def convertToBinary(n):
bin = ""
i = 7
while i >= 0:
if n % (2**i) == n:
bin = bin + "0"
i -= 1
else:
bin = bin + "1"
n -= 2**i
i -= 1
print(bin)
convertToBinary(decimal)
num1 = 6
num2 = 7
function findMax(numberA, numberB) {
if (numberA > numberB) {
console.log(numberA + " is max")
} else {
console.log(numberB + " is max")
}
}
function findMin(numberA, numberB) {
if (numberA < numberB) {
console.log(numberA + " is min")
} else {
console.log(numberB + " is min")
}
}
findMax(num1, num2)
findMin(num1,num2)
Homework/Hacks: For the hw, you have two options, easy or hard. The easy hack is for a 2.7 + extra work for the full 3. The easy hack is simply creating your own procedure with your own creativity. Since there is a lot of leeway for this one, you must do additional work to get a 3. For the hard hack, below is the start to a character to binary convertor. This is just a template, but the goal is to translate "APCSP" into binary. You can delete the existing code if you want. The only contraint is that you must use a procedure. Doing this will get you a 3.
string = "APCSP"
up = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
low = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
declist = []
binlist = []
def charToBinary(x):
for y in up:
if x == y:
declist.append((up.index(x)) + 65)
for z in low:
if x == z:
declist.append((low.index(x)) + 97)
def convert(dec):
bin = ""
i = 7
while i >= 0:
if dec % (2**i) == dec:
bin = bin + "0"
i -= 1
else:
bin = bin + "1"
dec -= 2**i
i -= 1
binlist.append(bin)
for ch1 in string:
charToBinary(ch1)
for decimal in declist:
convert(decimal)
print(binlist)
# The output shown below is the output you are supposed to get