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.

Decidable problems: A problem in which an algorithm can be used to solve or produce outputs in a finite number of steps. Undecidable problems: A problems where there is no algorithm that can be built to provide a solution for all parameters.

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
greater than 10
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

  • A variable multiplied by an integer to the power of two. This algorithm fits this criteria.

D. None of the above

E. All of the above

Hack 3

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

function run_peak_finder()
{
  let array_r= (3,5,7,5)
  print (peak_finder(array_r))
}


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
      print (`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
     print(`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
      print (`The ${counter-1} indexed number, ${peak} is a peak`)
    }else{
      counter += 1
    }
  }
}
}
function findpeak(ar)
{
    var peak = -Infinity;

    // Cycle through all the elements of the array
    for(var i = 0; i < ar.length; i++)
    {
        var el = ar[i];

        // If an element is of type array then invoke the same function
        // to find out the maximum element of that subarray
        if ( Array.isArray(el) )
        {
            el = findpeak( el );
        }

        if ( el > peak )
        {
            peak = el;
        }
    }

    return peak;
}

I was a bit confused on how to tackle this problem and I tried to utilize a recursive function. I am not really sure how to write this code optimally and would like to learn more about recursive functions.

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

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

This method is much more efficient as it no longer needs while loops to sort through the data set. I utilized a built in method to sort the data.

Hack 5

Rewrite this Python Code in a More Efficient Way

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)

Reflection

  • I am thoroughly interested in the recursive function and the utilization of the permutation method. I enjoyed learning about undecidable vs undecidable problems, and how to make code more efficient. I am not entirely sure on the uses of a recursive function and would like to learn more about how to apply it.