Hack 1
- A decidable problem is a problem for which an algorithm or a procedure can be written that always terminates after a finite number of steps and correctly answers whether a given input to the problem is a valid solution or not. An example of a decidable problem is determining whether a given string of text is a palindrome (a word or phrase that reads the same forwards and backwards). An undecidable problem is a problem for which it is impossible to design an algorithm or a procedure that always terminates and correctly answers whether a given input is a valid solution or not. An example of an undecidable problem is the Halting problem, which is the problem of determining, given a description of an arbitrary computer program and an input, whether the program will finish running or continue to run forever.
Hack 2
- Answer = None of the above.
A. 2 x 6 x 8 is a mathematical expression that evaluates to 96, but it is not an algorithm.
B. 4^5 is a mathematical expression that evaluates to 1024, but it is not an algorithm.
C. (3 x 8)^2 is a mathematical expression that evaluates to 144, but it is not an algorithm.
- None of these are a three step alogorithm because these answers only take two steps. So none of the above.
Hack 3
-
function peakFinder(array) { let start = 0; let end = array.length - 1; while (start <= end) { let mid = Math.floor((start + end) / 2); if (array[mid] > array[mid-1] && array[mid] > array[mid+1]) { return
The ${mid} indexed number, ${array[mid]} is a peak
; } else if (array[mid] < array[mid-1]) { end = mid - 1; } else { start = mid + 1; } } return “No peak element found”; } -
This function is using a while loop to search for a “peak” element in an array. A peak element is defined as an element that is greater than or equal to its neighbors in the array.The while loop will iterate through each element in the array and check if it is a peak. If the element is the first or last element in the array, it will only check if it is greater than or equal to its one neighbor. If it is any other element, it will check if it is greater than or equal to both of its neighbors.
Hack 4
-
def merge_sort(data): if len(data) <= 1: return data
mid = len(data) // 2 left_data = merge_sort(data[:mid]) right_data = merge_sort(data[mid:])
left_index = 0 right_index = 0 sorted_data = []
while left_index < len(left_data) and right_index < len(right_data): if left_data[left_index] < right_data[right_index]: sorted_data.append(left_data[left_index]) left_index += 1 else: sorted_data.append(right_data[right_index]) right_index += 1
sorted_data += left_data[left_index:] sorted_data += right_data[right_index:]
return sorted_data
if name == ‘main’: data = [9, 1, 7, 6, 2, 8, 5, 3, 4, 0] sorted_data = merge_sort(data) print(sorted_data)
- This is a Python function that implements the merge sort algorithm for sorting a list of numbers in ascending order.
Merge sort is a divide and conquer algorithm that works by repeatedly dividing the input list in half until each sublist contains only one element, and then merging the sublists back together in a sorted manner.
Hack 5
-
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))
- This is a Python function that generates all possible permutations of a given list using the heap’s algorithm. A permutation is an arrangement of a set of items in a specific order. For example, the permutations of the list [1, 2, 3] are [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1].