Note Template
What are procedures?
Fill in the blanks please:
Procedure: a named group of programming instructions that may have parameters and return values.
Parameters: are input values of a procedure
Arguments: specify the values of the parameters when a procedure is called
Modularity: specify the values of the parameters when a procedure is called
Procedural Abstraction: 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 or function
Why are procedures effective?: reduces the complexity and amount of code present
decimal = 7
def convertToBinary(n):
n=int(n)
n=bin(n)
return n[2:]
binary=convertToBinary(96)
print(binary)
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
function maxMin(array, operation) {
var res = "Error: invalid input(s)"
if (operation == "max") {
res = array.max()
} else if (operation == "min") {
res = array.min()
}
return res
}
var array = [1, 3, 5, 2, 4, 6]
var res = maxMin(array, "max")
console.log("Maximum of array " + array + " -> " + res)
var res = maxMin(array, "min")
console.log("Minimum of array " + array + " -> " + res)
var array = [35, 2, 65, 7, 8, 9, 12, 121, 33, 99]
var res = maxMin(array, "max")
console.log("Maximum of array " + array + " -> " + res)
var res = maxMin(array, "min")
console.log("Minimum of array " + array + " -> " + res)
i can't run javascript on my notebook but here is the expected result:
APCSP-Fastpages-1/javascript on master [?] via ⬢ v16.17.0 via 🅒 base
➜ node maxMin.js
Maximum of array 1,3,5,2,4,6 -> 6
Minimum of array 1,3,5,2,4,6 -> 1
Maximum of array 35,2,65,7,8,9,12,121,33,99 -> 121
Minimum of array 35,2,65,7,8,9,12,121,33,99 -> 2
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):
output = []
for char in x:
decimal = ord(char)
binary = bin(decimal)
binary = (binary)[2:]
output.append(f"0{binary}")
return output
binary_output = charToBinary("APCSP")
print(binary_output)
# The output shown below is the output you are supposed to get