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.js
Created January 6, 2026 13:01
max score with one reset
// Given an array of numbers, sums the element in order. The sum is reset
// exactly once during this process. The function returns the maximum sum.
function maxScore(arr) {
let sum = 0;
let max = arr[arr.length - 1];
// for exactly one reset, i > 0
// for at most one reset, i >= 0
for (let i = arr.length - 1; i > 0; i--) {
sum += arr[i];
@wperron
wperron / main.js
Last active December 22, 2025 14:58
Does this array alternate?
function alternates(arr) {
if (arr.length <= 2) return true;
let a = arr[0];
let b = arr[1];
for (let i = 2; i < arr.length; i++) {
if (arr[i] != a) return false;
[a, b] = [b, arr[i]];
}
@wperron
wperron / main.js
Created May 27, 2025 14:18
Find pairs of numbers that sum to an odd number
/**
* @function oddSum
* @description
* Identifies and returns pairs of numbers (one from array 'a' and one from array 'b')
* such that the sum of the two numbers in each pair is an odd number.
*
* The function iterates through all possible combinations of elements from the two input arrays.
* It leverages the mathematical property that the sum of an odd number and an even number
* is always odd. The core logic checks if one number in the pair is odd and the other is even.
* If this condition is met (meaning their sum would be odd), the pair is added to an
@wperron
wperron / pantone
Created April 18, 2025 17:52
Deno script that spits out Pantone™️ colors in alphabetical order, for when you just need to quickly name something and lack inspiration
#!/usr/bin/env -S deno run -A --unstable-kv
// This script keeps track of the last beginning letter used, and selects
// a word from the colors.txt that start with the next one. It also keeps
// track of the colors used so that they never come up again until the list
// is completely exhausted. It keeps it's data in the Deno.KV database.
const kv = await Deno.openKv();
const LAST_LETTER_KEY = "last_letter";
const USED_COLORS_KEY = "used_colors";
@wperron
wperron / incr
Last active March 27, 2025 19:45
Keep a persisted sequence of numbers
#!/usr/bin/env deno run --unstable-kv --allow-read --allow-write
// deno-lint-ignore-file prefer-const
import { parseArgs } from "jsr:@std/cli";
let db = await Deno.openKv("~/.incr/incr.db");
if (import.meta.main) {
let args = parseArgs(Deno.args, {boolean: ["list"]});
if (args["list"]) {
let iter = db.list({ prefix: [] });
@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() {