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.

What are procedures?

Fill in the blanks please:

Procedure: a 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, each containing all the parts needed to execute a single aspect of the functionality

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?: functions, def

Why are procedures effective?: Procedures allow someone to execute a block of code that would otherwise have to be rewritten several times. This saves time and effort, and makes the code more modular.

My Notes:

  • Procedures can be called recustively, meaning th same procedure can be called inside the procedure. However, for this to work, there must be some else, break, or exit case, or else the function will keep calling itself forever
  • python uses def to define a procedure but javascript uses function
  • in both languages, if you want, you can have a return statement which returns whatever you would like, and stores it in a variable if the function is set equal to a variable in the main code.
  • procedures are defined in the order name, arguments
  • a procedure must be defined before use

Challenge 1 below: Add the command that will call the procedure.

decimal = 11

def convertToBinary(n):
    power = 0
    while 2**power <= n:
        power += 1
    power -= 1
    answer = ""
    while power >= 0:
        if  n >= 2**power:
            n -= 2 ** power
            answer += "1"
        else:
            answer += "0"
        power -= 1
    return answer
print(convertToBinary(decimal))
1011

Challenge 2 below: Complete the Min and Max procedure in either JavaScript and Python using the instructions from the JavaScript page. (JavaScript will get you a extra 0.1)

def findMax(listnums):
    largest = -10000000000000000
    for i in listnums:
        if i > largest:
            largest = i
    return largest
def findMin(listnums):
    smallest = 10000000000000000
    for i in listnums:
        if i < smallest:
            smallest = i
    return smallest
print(findMax([1,-3,-5,2,21437,-218,109,0]))
print(findMin([1,-3,-5,2,21437,-218,109,0]))
21437
-218

bkjh

Category My Score Collegeboard Score Comments
Program purpose and function 1 1 A throrough explanation of the program purpose and function with all the required parts is present
Data Abstraction 1 1 clear code segments shown with explanation of contribution toward program purpose
Managing Complexity 1 1 explanation on use of list and how the code owuld change if not for a list
Procedural Abstraction 1 1 Good descriptions, clearly explaining the procedure and all of the details
Algorithm Implementation 0 1 Iteration is present, but only 3 items are iterated through, so it seemed trivial
Testing 1 1 good testing, explaining each test

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.

def daysapart(day1,month1,year1,day2,month2,year2):
    months = [0,31,28,31,30,31,30,31,31,30,31,30,31] 
    answer = months[month1]-day1
    for i in range(month1,len(months)):
        answer += months[i]
    year1copy = year1
    while year1copy < year2:
        answer += 365
        year1copy += 1
    for i in range(len(months)-1,month2-1,-1):
        answer -= months[i]
    answer -= months[month2]-day2
    return answer-1
month1 = int(input("enter a number of a month"))
day1 = int(input("enter a day in that month"))
year1 = int(input("enter a year"))
print("the second date should be after the first")
month2 = int(input("enter a number of a month"))
day2 = int(input("enter a day in that month"))
year2 = int(input("enter a year"))
print("the number of days between "+str(month1)+"/"+str(day1)+"/"+str(year1)+" and " +str(month2)+"/"+str(day2)+"/"+str(year2)+ " is " +str(daysapart(day1,month1,year1,day2,month2,year2)))
the second date should be after the first
the number of days between 5/5/1900 and 8/6/2000 is 36592
def charToBinary(c):
    n = ord(c)
    power = 0
    while 2**power <= n:
        power += 1
    power -= 1
    answer = ""
    while power >= 0:
        if  n >= 2**power:
            n -= 2 ** power
            answer += "1"
        else:
            answer += "0"
        power -= 1
    return answer
answer = ""
print("''APCSP'' in binary is ")
for i in "APCSP":
    answer = answer + charToBinary(i) + " "
print(answer)
# The output shown below is the output you are supposed to get
''APCSP'' in binary is 
1000001 1010000 1000011 1010011 1010000