Skip to content

Instantly share code, notes, and snippets.

Avatar

David Pennington Xeoncross

View GitHub Profile
@Xeoncross
Xeoncross / validSudoku.leetcode.go
Created February 18, 2023 17:58
My first attempt at validating a Sukoku board. (Also my first introduction to Sudoku at all!)
View validSudoku.leetcode.go
// https://leetcode.com/problems/valid-sudoku/submissions/900490403/
func isValidSudoku(board [][]byte) bool {
// 9 spaces is > 8 bits so we use 2 bytes (an int16)
rowsMap := [9]int16{}
colsMap := [9]int16{}
gridMap := [9]int16{}
for row:=0; row<9; row++ {
for col:=0; col<9; col++ {
@Xeoncross
Xeoncross / http_get_request.go
Created December 31, 2022 21:16
Making a HTTP request in Go requires you check multiple things if you want to be safe. Below is an example of something that takes, timeouts, context deadlines, HTTP status codes, and a size limit on the response.
View http_get_request.go
package safehttp
// HttpGetRequest assumes the response fits in memory and you're not trying to
// decode a json payload which would be better using json.NewDecoder()
func HttpGetRequest(ctx context.Context, url string, timeout time.Duration, sizeLimit int64) ([]byte, error) {
// Create a new HTTP client with a timeout
client := http.Client{Timeout: timeout}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
@Xeoncross
Xeoncross / mergesort.go
Created December 1, 2022 02:29
Generics merge sort for ordered slices ([]int) inplace (WIP)
View mergesort.go
package mergesort
import (
"fmt"
"testing"
"golang.org/x/exp/constraints"
)
// based on https://github.com/TheAlgorithms/Go/blob/master/sort/mergesort.go
@Xeoncross
Xeoncross / java_install.sh
Created October 6, 2022 14:39
Install Java on mac
View java_install.sh
# Using homebrew
brew install java
>>>
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /usr/local,
because macOS provides similar software and installing this software in
parallel can cause all kinds of trouble.
@Xeoncross
Xeoncross / parse_wiki_titles.go
Last active August 18, 2022 18:13
Simple script to parse the lines in the wikipedia page title dump file and output a list of all the english page titles (deduplicated). See https://dumps.wikimedia.org/enwiki/20220801/ for more files
View parse_wiki_titles.go
package wikipediatitles
import (
_ "embed"
"sort"
"strings"
"unicode"
)
// https://dumps.wikimedia.org/enwiki/20220801/enwiki-20220801-all-titles-in-ns0.gz
@Xeoncross
Xeoncross / System Design.md
Created July 8, 2022 01:22 — forked from vasanthk/System Design.md
System Design Cheatsheet
View System Design.md

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@Xeoncross
Xeoncross / ranked_window.go
Created June 22, 2022 23:19
Keep the keys with the highest values in a map https://go.dev/play/p/E-PQldhgz4M
View ranked_window.go
package main
import "fmt"
func BenchmarkMapIteration(b *testing.B) {
size := 1000
ranked := make(map[int]uint8, size)
for i := 1; i < size; i++ {
ranked[i] = uint8(i % 8)
@Xeoncross
Xeoncross / int_bits.go
Created June 22, 2022 17:36
Examples of bit lengths of different integer values: https://go.dev/play/p/gLZKA9B233I
View int_bits.go
package main
import (
"encoding/binary"
"fmt"
)
func main() {
// subtract 1 from each answer since we're counting zero also
fmt.Printf("%d bits = %8b = %d\n", 2, byte(1<<2-1), 1<<2)
@Xeoncross
Xeoncross / delta_encoding_demo.go
Created June 22, 2022 02:13
Encoding a sorted sequence of integers using delta encoding can save 40%-60% on storage costs: https://go.dev/play/p/KdLslro7n1n
View delta_encoding_demo.go
package iiexperiments
import (
"fmt"
"testing"
)
// How much space will using a delta encoding save?
// If we assume we have 1 byte (0-255) to tell us how many bytes the delta is contained in, we can then only store the diff between the last and next int values. This difference is the "delta".
@Xeoncross
Xeoncross / README.md
Created June 20, 2022 20:00
Distributed rate limiting with non-whole time periods (max 3 requests every 5 seconds)
View README.md

Rate Limiting Redis with multiples of a time.Duration

In a distributed system, you want the API layer to be stateless. So the app can offload rate limiting to a redis instance. This way, you can use the INCR to both increment and return the number of requests a given IP/Client/API Key has made across any number of App / serverless instances.

However, what do you do if the limit isn't roundable to the current second/minute/hour/day? How does each instance agree which key to use? The answer is to modulo the previous and future keys to find the right one.

For example, 3 requests every 5 seconds is the desired limit. That means we can use -max-1:+max-1 as the range of values to scan to find the key we should use.

max := 5