Posts

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

Dynamic Programming - Parts      PART 1 - Memoization     P ART 2 - Tabulation Dynamic Programming with Fibonacci Example Recursion: Slow because time complexity is O(2^n) func fibR (x int ) int {     if x <= 2 {         return 1     }     return fibR (x- 1 ) + fibR (x- 2 ) } Dynamic Way Example Recursion with Stored Value:  // Fast because it removes duplicate processes by checking map. // Tail recursion-ish // Results are hitchhiking each recursive call func fibR2 (x int , y map [ int ] int ) int {     if _ , ok := y[x]; ok {         return y[x]     }     if x <= 2 {         return 1     }     y[x] = fibR2 (x- 1 , y) + fibR2 (x- 2 , y)     return y[x] }

59. Go - goto

Goto statement allows label jumping. goto only works on labels within the same function bracket scope. package main import "fmt" func main () { /* local variable definition */ var a int = 10 /* do loop execution */ LOOP : for a < 20 { if a == 15 { /* skip the iteration */ a = a + 1 goto LOOP } fmt . Printf ( "value of a: %d\n" , a ) a ++ } } OUTPUT: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19

4. Algorithms - Bogo Sort

Bogo sort is a randomizing algorithm that shuffles an array and then checks whether it is sorted. package main import (     "fmt"     "math/rand"     "time" ) // isSorted checks whether a list is sorted by comparing first and next value. func isSorted (list [] int ) bool {     for i , _ := range list {         if i != ( len (list) - 1 ) {             if list[i] > list[i+ 1 ] {                 return false             }         }     }     return true } // bogoSort shuffles a list. func bogoSort (list *[] int ) {     rand. Shuffle ( len (*list), func (i, j int ) {         (*list)[i], (*list)[j] = (*list)[j], (*list)[i]     }) } func main () {     rand. Seed (time. Now (). UnixNano ())     x := [] int { 4 , 1 , 7 , 5 , 8 }     tries := 0     for {         tries++         fmt. Println (tries)         bogoSort (&x)         fmt. Println (x)         if isSorted (x) {             break         }     }     fmt. Println (x) }

3. Algorithms - Selection Sort

Selection sort sorts an array by finding the lowest value element and appending it to an empty array. The algorithm will shrink the unsorted array and repeat the process recursively until all unsorted elements are used up. package main func selectionSort (list *[] int ) {     sorted := make ([] int , 0 , len (*list))     unSorted := *list     for i := 0 ; i < len (*list); i++ {         min := unSorted[ 0 ]         index := 0         for i , e := range unSorted {             if e <= min {                 min = e                 index = i             }         }         sorted = append (sorted, (unSorted)[index])         if index == 0 {             unSorted = unSorted[ 1 :]         } else if index == ( len (*list) - 1 ) {             unSorted = unSorted[:index]         } else {             unSorted = append (unSorted[ 0 :index], unSorted[index+ 1 :]...)         }     }     * list = sorted } /*func main() {     x := []int{4, 6, 3, 2, 9, 7, 3, 5}     fmt.Print

2. Algorithms - Quick Sort

Quicksort, sorts an array recursively by picking a pivot point (usually the middle element). Then creating two subArrays left and right of the pivot. Left would contain elements in array that is less than pivot, and right is greater. subArrays are put into quicksort again with base statement returning an array with length of 1 or 0. package main //import "fmt" func quickSort (list [] int ) [] int {     if len (list) <= 1 {         return list     }     pivot := make ([] int , 1 )     pivot[ 0 ] = list[ 0 ]     leftList := [] int {}     rightList := [] int {}     for i := 1 ; i < len (list); i++ {         if list[i] > pivot[ 0 ] {             rightList = append (rightList, list[i])         } else {             leftList = append (leftList, list[i])         }     }     leftSublist := quickSort (leftList)     rightSublist := quickSort (rightList)     answer := [] int {}     answer = append (leftSublist, pivot...)     answer = append (answer, rightSubl

1. Algorithms - Merge Sort

Merge Sort Code: using merge sort and merge method recursively to sort/merge an unsorted list package main import (     "fmt" ) func mergeSort (list [] int ) [] int {     // If list only have 1 number or none, return it.     if len (list) < 2 {         return list     }     // Split recursion     left := mergeSort (list[: len (list)/ 2 ])     right := mergeSort (list[ len (list)/ 2 :])     return merge (left, right) } func merge (left, right [] int ) [] int {     answer := [] int {}     leftCount := 0     rightCount := 0     for leftCount < len (left) && rightCount < len (right) {         if left[leftCount] < right[rightCount] {             answer = append (answer, left[leftCount])             leftCount++         } else {             answer = append (answer, right[rightCount])             rightCount++         }     }     for ; leftCount < len (left); leftCount++ {         answer = append (answer, left[leftCount])     }     for ; r

58. Go - Break/Return

Break : break out of a loop, but continue the code below the loop break is used to immediately terminate a for loop, a while loop or a switch statement. You can not break from an if block. Return : break out of loop and out of method or whole block {} and return the value to caller. return is used the terminate a method (and possibly return a value).