Skip to content

Instantly share code, notes, and snippets.

View vaguecoder's full-sized avatar
🤖
Confused

Bhargav Ravuri vaguecoder

🤖
Confused
View GitHub Profile
#!/usr/bin/env python
def distance(start:int, blocks:list) -> int:
frog1, frog2 = start, start
for i in range(start, 0, -1):
if blocks[i-1] < blocks[i]:
break
frog1 = i-1
for i in range(start, len(blocks)-1):
if blocks[i+1] < blocks[i]:
Marshal Encode
Type of Component Function Method
Processing Whole data at once A chunk of data at once depending upon the inputs to it
jsonData, err = json.Marshal(data)
if err != nil {
// Handle Error
}
err = ioutil.WriteFile("filename.json", jsonData, 0777)
if err != nil {
// Handle Error
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
jsonData, err = json.Marshal(data)
if err != nil {
// Handle Error
}
_, err = w.Write(jsonData)
if err != nil {
// Handle Error
}
})
var jsonData []byte
jsonData, err = json.Marshal(data)
if err != nil {
// Handle Error
}
fmt.Println(jsonData) // JSON data in bytes
fmt.Println(string(jsonData)) // String for format verification
@vaguecoder
vaguecoder / Sample-Data-For-JSON-Operations.go
Last active December 16, 2021 18:00
JSON Marshal-Unmarshal vs Encoding-Decoding in Go (golang)
type Character struct {
Name string `json:"name"`
Designation []string `json:"designation"`
Age int `json:"age"`
Moves []Move `json:"moves"`
}
type Move struct {
Name string `json:"move"`
Power int `json:"power"`
@vaguecoder
vaguecoder / FindGCD.go
Created August 23, 2021 10:11
Code to find the Greatest Common Divisor (GCD) of 2 or more numbers.
package main
import "fmt"
// PrimeGenerator creates a generator goroutine that returns prime numbers, least first
func PrimeGenerator(close <-chan int) chan int {
var current int
var flag bool
ch := make(chan int)
@vaguecoder
vaguecoder / broadcast.go
Created July 4, 2021 23:11
Broadcasts a message (any type) continuously in another goroutine. For full package/program, check `GitHub.com/VagueCoder/Random-Go-Snippets`.
// Channel is a custom type to hold channel and context variables.
type Channel struct {
// Only unexported fields
ch chan interface{}
ctx context.Context
cancelFunc context.CancelFunc
}
// runProducer is creates a separate goroutine that continuously produces given message.
package main
import (
"fmt"
"net/http"
"os"
)
// HashMap is the implementation of a hash map in Go
type HashMap map[interface{}]interface{}
@vaguecoder
vaguecoder / randomStrings.go
Created May 22, 2021 13:32
This creates a channel which continuously processes and returns random strings of fixed (given) size. And processing can be cancelled at will.
package randomStrings
import (
"context"
"math/rand"
"time"
)
// randInt is local function that returns a random integer between provided min, max.
func randInt(min int, max int) int {