Skip to content

Instantly share code, notes, and snippets.

@umcconnell
umcconnell / gram_schmidt.py
Created December 2, 2022 12:21
Gram-Schmidt process with sympy using a custom scalar product.
import sympy as sp
x = sp.Symbol("x")
def gs(vecs, scalar_product):
new_base = [
None for _ in range(len(vecs))
]
new_base[0] = vecs[0] / norm(vecs[0], scalar_product = scalar_product)
let csv = `
year,amount,n
2016,123,1
2017,124,2
2018,125,3
2019,126,4
2020,127,5
2021,128,6
`;
@umcconnell
umcconnell / profile.rs
Created December 23, 2020 17:48
Rust macro to profile a short snippet of code and return the evaluated result. Inspired by the `dbg!` macro.
/// Prints and returns the value of a given expression for quick and dirty code
/// profiling.
///
/// Example usage:
///
/// ```rust
/// let s = "hello world";
/// let all_caps = profile!(s.repeat(2).to_uppercase());
/// ^-- prints: [src/main.rs:2] execution time: 2μs
///
@umcconnell
umcconnell / fibonacci_spiral.py
Created August 3, 2020 11:55
Draw the fibonacci spiral with Python turtle
import argparse
import turtle
parser = argparse.ArgumentParser(description='Draw the fibonacci spiral')
parser.add_argument('n', type=int, help='Length of fibonacci sequence')
parser.add_argument('-s', '--scale', type=int, default=1)
parser.add_argument('--speed', type=int, default=6, help='Turtle speed')
args = parser.parse_args()
@umcconnell
umcconnell / example.py
Created July 30, 2020 19:47
A simple python decorator to measure function execution time
# Example usage
from time import sleep
@timer(repeat=10)
def my_function():
sleep(1)
return True
@umcconnell
umcconnell / spiralling_shapes.py
Created July 16, 2020 10:26
Spiralling shapes with Python turtle
import turtle
turtle.speed(0)
def spiralling_shape(sides: int = 4, iterations: int = 200, scale: int = 1):
for i in range(iterations):
turtle.forward(i*scale)
turtle.left(360//sides + 1)
# Make a spiralling square
@umcconnell
umcconnell / fizzbuzz.rs
Created July 7, 2020 13:48
Fizzbuzz in Rust
fn main() {
for x in 1..101 {
let mut out = String::new();
if x % 3 == 0 {
out += "fizz";
}
if x % 5 == 0 {
out += "buzz";
@umcconnell
umcconnell / fib.rs
Last active August 18, 2020 14:17
Generate the Fibonacci Sequence in Rust.
pub struct Fib {
a: u32,
b: u32,
count: i32,
}
impl Fib {
pub fn new(count: i32) -> Fib {
Fib { a: 1, b: 1, count }
}
@umcconnell
umcconnell / loader.js
Created November 26, 2019 18:00
Loading animation for node console
// Adapted from https://stackoverflow.com/questions/34848505/how-to-make-a-loading-animation-in-console-application-written-in-javascript-or
/**
* Create and display a loader in the console.
*
* @param {string} [text=""] Text to display after loader
* @param {array.<string>} [chars=["⠙", "⠘", "⠰", "⠴", "⠤", "⠦", "⠆", "⠃", "⠋", "⠉"]]
* Array of characters representing loader steps
* @param {number} [delay=100] Delay in ms between loader steps
* @example
@umcconnell
umcconnell / fib.js
Last active August 20, 2019 14:52
Recursive fibonacci sequence
/**
* Generate the fibonaccie sequence recursively
* @param {number} num amount of recursion
* @returns {array} fibonacci sequence
*/
let fib = num => {
if (num == 1) return [1];
else {
let prev = fib(num - 1);
return prev.concat((prev[prev.length - 2] || 0) + prev[prev.length - 1])