Important Vocab/Notes
- There are 4 types of algorithms, 1 step, 2 step, 3 step, and 4 step
- An example of a 1 step is: 2 * n
- An example of a 2 step is: 2^n
- An example of a 3 step is: (2 * n)^2
- An example of a 4 step is: 5! = 5 4 3 2 1 = 120
- There are problems that computers cannot solve
- If a computer can solve a problem it may take an unreasonable amount of time
- Undecidable problem: problems for which no algorithms can be built that can provide a correct yes or no answer or a solution
- Decidable problem: problems for which algorthms could be written to solve/produce a correct output for all inputs.
Please write a short 1-2 sentence explanation describing the difference between decidable and undecidable problems. Make sure to provide at least one example of each.
- The difference between an decidable problem and an undecidable problem is that an undecidable problem has no algorithms that provide a solution whereas a decidable problem has algorithms that are solutions.
def ben():
z = 0
i = 1
while z != 1:
i = i + 1
print(i)
ben()
vardaan = 5
if vardaan > 10:
print("greater than 10")
else:
print("not greater than 10")
Which of the following is a 3 step algorithm?
A. 2 x 6 x 8
B. 4^5
C. (3 x 8)^2
- This is the answer because there is 3 steps (3 x 8) and then you have to put it to the power of 2, which is another 2 steps (24 x 2 x 2) this makes a total of 3 steps.
D. None of the above
E. All of the above
function peak_finder(array){
let counter = 0
let peak = 0
let peak_index =0
while (counter <= array.length){
console.log(counter)
if (counter === 0){
if (a[0]>=a[1]){
peak = a[0]
peak_index = counter
counter = array.length
return `The ${counter-1} indexed number, ${peak} is a peak`
}else{
counter+=1
}
}else if(counter === array.length-1){
if (a[array.length-1] >= a[array.length-2]){
peak = a[array.length-1]
peak_index = counter
counter = array.length
return `The ${counter-1} indexed number, ${peak} is a peak`
}
}else{
if (a[counter]> a[counter+1] && a[counter]> a[counter-1]){
peak = a[counter]
peak_index = counter
counter = array.length
return `The ${counter-1} indexed number, ${peak} is a peak`
}else{
counter += 1
}
}
}
}
Rewritten One:
function peakFinder(array) {
let peak = 0;
let peakIndex = 0;
for (let i = 0; i < array.length; i++) {
if (i === 0 && array[i] >= array[i + 1]) {
peak = array[i];
peakIndex = i;
break;
} else if (i === array.length - 1 && array[i] >= array[i - 1]) {
peak = array[i];
peakIndex = i;
break;
} else if (array[i] > array[i - 1] && array[i] > array[i + 1]) {
peak = array[i];
peakIndex = i;
break;
}
}
return `The ${peakIndex} indexed number, ${peak} is a peak`;
}
Rewritten One
- This was hard to do because I am not too good at JavaScript, so I used what I new and made the code more efficient with binary search
data = [9, 1, 7, 6, 2, 8, 5, 3, 4, 0]
print("This is the unsorted data:", data)
data.sort()
print("This is the sorted data", data)
- Instead of having all of the extra code I simply used a sort function to sort the data.
from itertools import permutations
perm = permutations([1, 2, 3])
for i in list(perm):
print (i)
- This version of the function uses an in-place algorithm, which means that it does not create a new list for each permutation.
Blog/Final Thoughts
- Found out there are undecidable problems are
- Also found out what a 1 step, 2 step, and 4 step, functions are
- These were things that I did not even no about before
- For creating the more effiecient code I understood it a little, but it was pretty hard to understand what to do, at least for #3
- Hack 3 and 5 for probably the hardest and 3 was pretty confusing
- Also learned how to make code more effective and simplify it
def heap_permutation(data, n):
if n == 1:
print(data)
return
for i in range(n):
heap_permutation(data, n - 1)
if n % 2 == 0:
data[i], data[n-1] = data[n-1], data[i]
else:
data[0], data[n-1] = data[n-1], data[0]
if __name__ == '__main__':
data = ["apple", "banana", "carrot"]
heap_permutation(data, len(data))
from itertools import permutations
perm = permutations(["apple", "banana", "carrot"])
for i in list(perm):
print (i)