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 / otel-deno.js
Last active April 9, 2024 16:01
Exporting traces via OTLP/HTTP in Deno
import opentelemetry from "npm:@opentelemetry/api";
import { context, trace } from "npm:@opentelemetry/api";
import {
BasicTracerProvider,
BatchSpanProcessor,
ConsoleSpanExporter,
SimpleSpanProcessor,
} from "npm:@opentelemetry/sdk-trace-base";
import { Resource } from "npm:@opentelemetry/resources";
import { OTLPTraceExporter } from "npm:@opentelemetry/exporter-trace-otlp-proto";
@wperron
wperron / main.rs
Created November 7, 2023 13:56
Given a list of words and a dictionary of letter scores, find the word with the highest score
fn main() {
let highest = vec!["apple", "banana", "cherry", "date", "fig"]
.into_iter()
.map(|w| (w, score(w.to_owned())))
.reduce(|acc, b| {
if b.1 > acc.1 {
return b;
}
acc
})
@wperron
wperron / main.js
Created October 16, 2023 14:57
Isomorphic Strings
// Given two strings s and t, determine if they are isomorphic.
// Two strings are isomorphic if there is a one-to-one mapping
// possible for every character of the first string to every
// character of the second string.
function isIsomorphic(a, b) {
if (a.length !== b.length) return false;
let mapping = {};
for (let i = 0; i < a.length; i++) {
@wperron
wperron / main.go
Created October 11, 2023 14:25
Parse a csv file and export a Markdown table
// md-fmt takes a csv or tsv input file and outputs a formatted markdown table
// with the data.
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"os"
@wperron
wperron / main.rs
Created August 8, 2023 13:16
Rust implementation of Luhn's algorithm
fn main() {
println!("{}", validate(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]));
}
fn validate(card: Vec<u8>) -> bool {
let mut card = card;
let given_check = card.pop().unwrap();
let mut check = 0;
for (i, n) in card.iter().rev().enumerate() {
@wperron
wperron / main.js
Created June 27, 2023 20:17
Find missing elements in sorted set of characters
/**
* Write a function that takes an array of consecutive, increasing letters as
* input, and returns any missing letters in the array between the first and
* last letter.
*
* Example:
* ```
* > missingLetters(['a','b','c','d','f'])
* > ['e']
*
const rows = [
"qwertyuiop",
"asdfghjkl",
"zxcvbnm",
].reduce(
(acc, curr, i) => {
curr.split('').forEach(c => acc[c] = i);
return acc;
},
{},
@wperron
wperron / prom-compare.js
Created May 11, 2023 13:24
Compare two different sets of prometheus metrics based on the output of the `/metrics` endpoint
#!/usr/bin/env deno run -A
import { readStringDelim } from "https://deno.land/std@0.186.0/io/read_string_delim.ts";
import * as path from "https://deno.land/std@0.186.0/path/mod.ts";
async function extractMetricNames(f) {
const filename = path.join(Deno.cwd(), f);
let fileReader = await Deno.open(filename);
let metrics = new Set();
@wperron
wperron / main.rs
Created May 3, 2023 19:47
Remove leading and trailing zeroes from a list of numbers
/// Given a non-empty array containing only non-negative integers, return the
/// list with trailing and leading zeroes removed.
///
/// Example:
///
/// ```
/// > removeZeroes([0, 0, 0, 3, 1, 4, 1, 5, 9, 0, 0, 0, 0])
/// > [3, 1, 4, 1, 5, 9]
///
/// > removeZeroes([0, 0, 0])
@wperron
wperron / main.rs
Created March 15, 2023 19:38
Doing math with fractions
use std::{collections::BTreeSet, fmt::Display, str::FromStr};
/// Write a function that can do the 4 basic operations (add, subtract, multiply
/// and divide) on two fractions. Return the most simplified form of the result.
/// You can assume a non-zero denominator in the input, and don’t use any
/// built-in implementations in your language of choice, if you can!
///
/// Example:
///
/// ```