HACK 1

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.

A decidable problem is a problem which can use an algorithim in order to be solved. An undecidable problem is a problem in which no algorithm is able to be built in order to provide a solution.


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 </p>

D. None of the above

E. All of the above

The first step is 3 x 8 The second step is getting the answer 24 The third step is putting 24 to the power of 2


</div> </div> </div>

HACK 3 Rewrite this JavaScript Code in a More Efficient Way (Hint: Use Binary Search)

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
    }
  }
}
}
  Input In [6]
    function peak_finder(array){
             ^
SyntaxError: invalid syntax

Wasn't really sure how to do this. I thought that if I tried to edit some stuff out which weren't neccesary it would work. But after I ran it, it still wouldn't work.

function peak_finder2(array){
    if (array.length)=== 0{
       return  `Array cannot be empty`
     }else if (array.length === 1){
       return array[0]
     }else{
       let mid_index = Math.floor(array.length*0.5)
      if (array[mid_index +1]>array[mid_index]){
         return peak_finding(array.slice(mid_index + 1 ))
       }else if (array[mid_index -1]>array[mid_index]){ 
        new=array.reverse().slice(mid_index+1).reverse()
        return peak_finding(new)  
        }else{
         return array[mid_index]
        }
      }
}
  Input In [5]
    function peak_finder2(array){
             ^
SyntaxError: invalid syntax

HACK 4: Rewrite this Python Code in a More Efficient Way

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)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I got rid of the unecessary code. I just added the data and print(data) command. I also added a data sort which made it simplified overall.

data = [9, 1, 7, 6, 2, 8, 5, 3, 4, 0]

print(data)
data.sort()
print(data)
[9, 1, 7, 6, 2, 8, 5, 3, 4, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

HACK 5: Rewrite this Python Code in a More Efficient Way

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))
[1, 2, 3]
[2, 1, 3]
[3, 1, 2]
[1, 3, 2]
[2, 3, 1]
[3, 2, 1]

I was able to rewrite this code to make it more efficent by getting rid of " 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]" This overall resulted in the same output but with less words.

def heap_permutation(data, n):
    if n == 1:
        print(data)
        return
        
from itertools import permutations 

perm = permutations([3,2,1]) 

for i in list(perm):
    print(i)
  
(3, 2, 1)
(3, 1, 2)
(2, 3, 1)
(2, 1, 3)
(1, 3, 2)
(1, 2, 3)
</div>