Skip to content

Instantly share code, notes, and snippets.

View xeoncross's full-sized avatar

David Pennington xeoncross

View GitHub Profile
@xeoncross
xeoncross / gencert.go
Created January 27, 2024 17:14
Generate a self-signed certificate in Go
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
@xeoncross
xeoncross / kindle.md
Created July 8, 2023 19:24
Moving books and PDF's onto your Amazon Kindle

I'm not sure if who might need this information, but man it's hard to find. Kindles aren't as user-friendly as one would expect.

If you need to move books and documents onto the kindle using your computer or mac (via USB) you need to put the .epub .mobi or .pdf files into the "documents" folder.

The kindle will create a ton of "[book name]_[hash].sdr" folders to match and store metadata like your place in the book.

@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!)
// 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.
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)
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
# 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
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

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
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
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)