number = 20 #set number equal to random value
if number > 10: # if the number is greater than 10
print("greater than 10") #print
else: # if less than 10
print("not greater than 10") # print
num = 1
while num == 0: # the number will never equal 0 since we set it to 1, so this is undecidable
print(num)
num = num + 1
Hack 2
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 correct answer because a three step algorithm is an algorithm where you multiply a variable and integer, all to the power of two. This algorithm fits this criteria.
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
}
}
}
}
function recursiveBinarySearch(n, arr) {
let mid = Math.floor(arr.length / 2);
if (arr.length === 1 && arr[0] != n) {
return false;
}
if (n === arr[mid]) {
return true;
} else if (n < arr[mid]) {
return recursiveBinarySearch(n, arr.slice(0, mid));
} else if (n > arr[mid]) {
return recursiveBinarySearch(n, arr.slice(mid));
}
}
def merge_sort(data):
if len(data) <= 1:
return
mid = len(data) // 2
left_data = data[:mid]
right_data = data[mid:]
merge_sort(left_data)
merge_sort(right_data)
left_index = 0
right_index = 0
data_index = 0
while left_index < len(left_data) and right_index < len(right_data):
if left_data[left_index] < right_data[right_index]:
data[data_index] = left_data[left_index]
left_index += 1
else:
data[data_index] = right_data[right_index]
right_index += 1
data_index += 1
if left_index < len(left_data):
del data[data_index:]
data += left_data[left_index:]
elif right_index < len(right_data):
del data[data_index:]
data += right_data[right_index:]
if __name__ == '__main__':
data = [9, 1, 7, 6, 2, 8, 5, 3, 4, 0]
merge_sort(data)
print(data)
data = [9, 1, 7, 6, 2, 8, 5, 3, 4, 0] # list of numbers not in order
print("unsorted data:", data) # print unsorted data
print ("---------------------------------------------")
data.sort() # sort function to sort through data
print("sorted data", data) # print sorted data
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 = [1, 2, 3]
heap_permutation(data, len(data))
# permutations using library function
from itertools import permutations
# Get all permutations of [1, 2, 3]
perm = permutations([1, 2, 3], 3) # the 3 outside the bracket is all the permutations of the length 3
# if we changed it to 2 it would show all the permutations of the length 3.
# Print the obtained permutations
for i in list(perm):
print ([i])