Skip to content

Instantly share code, notes, and snippets.

View wperron's full-sized avatar
🚀
Launching into orbit

William Perron wperron

🚀
Launching into orbit
View GitHub Profile
@wperron
wperron / main.rs
Created August 1, 2022 13:17
Get the number of ones in all positive integers less than or equal to N
fn main() {
println!("{}", number_of_ones(14));
}
/// Given an integer n, count the total number of 1 digits appearing in all non-negative integers less than or equal to n.
///
/// Example:
///
/// ```bash
/// > numberOfOnes(14)
@wperron
wperron / main.rs
Created June 13, 2022 13:16
Create a looooong teeeeext generator
use std::iter::repeat;
fn main() {
println!("{}", long_string(String::from("hello world"), 3));
println!("{}", long_string(String::from("lol"), 10));
}
/// Create a loooong teeeext generator that takes in a string and an integer `n`,
/// and multiplies the vowels in the string by `n`.
///
@wperron
wperron / main.go
Created May 4, 2022 14:33
quick and dirty autocomplete feature
package main
import (
"database/sql"
"fmt"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
)
@wperron
wperron / main.rs
Last active March 30, 2022 12:41
Find elements in container within given bounds
fn main() {
println!(
"{:?}",
elements_in_container(String::from("|**|*|*|"), 0, 5)
);
println!(
"{:?}",
elements_in_container(String::from("|**|*|*|"), 0, 6)
);
println!(
@wperron
wperron / main.go
Created March 22, 2022 15:30
Find the shortest interval in a list by *sleeping*
package main
import (
"fmt"
"time"
)
var (
input = []string{"01h00m", "08h15m", "11h30m", "13h45m", "14h10m", "20h05m"}
)
# echo -n '1,9,87,3,10,4,20,2,45' | tr ',' '\n' | sort -g | tr '\n' ' ' | awk -f longest-seq.awk
BEGIN {getline;
i = 2
j = 1
while (i < NF) {
if ($(i) - $(j) != i - j) {
j++
}
i++
}
@wperron
wperron / main.go
Created February 17, 2022 21:13
Finding the shortest path between two keys on a keyboard
package main
import (
"fmt"
"strings"
)
type Key struct {
val rune
left, right, up, down *Key
@wperron
wperron / main.go
Created February 1, 2022 15:15
Manually setting Accept-Encoding on a request
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"github.com/klauspost/compress/gzhttp"
@wperron
wperron / find_time.sh
Created January 31, 2022 20:43
Find local time for someone in the "database"
#!/bin/bash
TZ=$(awk -F ',' "NR>1 && tolower(\$0) ~ /$1/ {print \$2}" people.csv) date \
"+%H:%M"
@wperron
wperron / main.go
Created January 28, 2022 01:39
Find all functions and methods in a Go source file
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
)