Skip to content

Instantly share code, notes, and snippets.

View nathaponb's full-sized avatar
🎯
Focusing

CuriousCow nathaponb

🎯
Focusing
View GitHub Profile
package main
import (
"fmt"
"log"
"net/http"
)
type Configs struct {
Addr string
@nathaponb
nathaponb / deadfish.go
Created July 7, 2023 06:08
Make the deadfish swim
package main
import (
"fmt"
)
// https://www.codewars.com/kata/51e0007c1f9378fa810002a9/train/go
func main() {
input := "idoiido"
@nathaponb
nathaponb / two_sum_algo.go
Last active July 19, 2023 13:08
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
package main
import "fmt"
func main() {
fmt.Println(findPairs([]int{1, 2, 3, 4, 5}, 3))
}
func findPairs(nums []int, target int) [][]int {
@nathaponb
nathaponb / highest-sum-of-sequence-sub-slice.go
Created June 30, 2023 10:09
find the highest sum of sequence values from sub slice from the given slice
// instruction: find the highest sum of sequence values from sub slice from the given slice
package main
import "fmt"
func main() {
input1 := []int{1, -1, 0, 3, 4, -3, 2}
const data = [5, 8, 11, 19, 7, 15, 12, 20, 10, 1, 6, 3];
// sorting function
const merge = (arr1, arr2) => {
let sorted = [];
while (arr1.length && arr2.length) {
if (arr1[0] < arr2[0]) sorted.push(arr1.shift());
else sorted.push(arr2.shift());
}
// O(logN)
// Collection must be in sorted order, merge sort and binary search are the great implementation.
function recursiveFn(arr, x, startIndex, endIndex){
// ends recursive fn when nothing left to searching for.
if(startIndex > endIndex) return false;
// find the middle index
let mid = Math.floor((startIndex + endIndex) / 2)
function factorial(){
const cache = {}; // create closure
return function factMemoized(n) {
if(n < 2) {
return 1;
}
if(cache[n]) {
console.log('return from cache ' + cache[n])
return cache[n]; // return from cache
}else {