Skip to content

Instantly share code, notes, and snippets.

@echohes
echohes / bin_to_int.lua
Created August 22, 2022 10:08
Convert binary to int
--a binary number is represented as values in a table
t_bin = {1,0,0,0,0,0,0,0}
function bin_to_in(x)
r=0
for k,v in pairs(x) do
r = r + (v*2^(8-k))
end
return math.floor(r)
end
@echohes
echohes / int32_to_binary.lua
Created August 22, 2022 10:03
Convert int32 to binary
function conv_to_bin(x)
t={}
if x>0 then
while x>0 do
table.insert(t,x%2)
x=math.floor(x/2);
end
end
if #t<32 then
@echohes
echohes / example_custom_constraints.go
Last active March 22, 2022 07:09
Golang Generics examples
package main
import (
"fmt"
)
func main() {
var a, b uint
a = 1
@echohes
echohes / slices.go
Created March 3, 2022 07:09
Gorutine sync and change slice
package main
import (
"fmt"
"sync"
)
func test(n int, s []int, w *sync.WaitGroup) {
fmt.Println("start ", n)
for k, v := range s {
@echohes
echohes / buble_sort.lua
Last active February 26, 2022 22:46
Sort Algorithm
local a={}
for i=1,5000 do
a[i]=math.random(0, 650000)
end
local function bs(t)
for k,v in pairs(t) do
for kk,vv in pairs(t) do
if k~=kk then
@echohes
echohes / merge_2_sort_array_1.go
Last active February 28, 2022 14:46
Merge 2 sort array
package main
import "fmt"
func main() {
a := []int{1, 2, 4, 5}
b := []int{2, 5, 6, 7, 8, 9}
c := []int{}
bs := 0
i := 0
@echohes
echohes / bin_search.go
Created February 13, 2022 22:45
binary search in Golang
package main
import (
"fmt"
)
func binary_search(arr []int, item int) int {
max := len(arr)
low := 0
for low <= max {
@echohes
echohes / bin_search.lua
Created February 13, 2022 22:16
binary search in Lua
l={}
for i=1,1000000 do
table.insert(l,i)
end
function b(t,i)
max=#t
low=1
while low<=max do
@echohes
echohes / syncmaprace.go
Created March 18, 2021 08:13
go sync.map example
package main
import (
"fmt"
"sync"
"time"
)
var arrTest = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
@echohes
echohes / main.go
Last active March 9, 2023 08:13
golang errgroup example with context and subcontext for gracefull shutdown
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"