Note Template
What are procedures?
Fill in the blanks please:
Procedure: A named algorithm that has parameters and return values
Parameters: inputs of a procedure
Arguments: specifying values of parameter when procedure is called
Modularity: Seperating parts of functions into other functions for easier usage
Procedural Abstraction: The ability to call a process without stating what it does, but the name of the procedure
What are some other names for procedures?: method, function
Why are procedures effective?: They can do certain things without stating the same thing many times in different places
decimal = 7
def convertToBinary(n):
ans = ""
if n == 0:
return 0
while n:
ans += str(n&1)
n = n >> 1
ans = ans[::-1]
return ans
print(convertToBinary(decimal))
def findMax(*args):
ans=args[0]
for arg in args:
if ans<arg:
ans=arg
return ans
def findMin(*args):
ans=args[0]
for arg in args:
if ans>arg:
ans=arg
return ans
print(findMax(1,3,4,5,2,6))
print(findMin(1,3,4,5,2,6))
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 charToBinary(x):
ans=[]
for chr in x:
dec=ord(chr)
ans.append(convertToBinary(dec))
return ans
string="APCSP"
print(string+" in binary is")
print(charToBinary(string))
# The output shown below is the output you are supposed to get
# ''APCSP'' in binary is
# [1000001, 1010000, 1000011, 1010011, 1010000]