Skip to content

Instantly share code, notes, and snippets.

View Melvillian's full-sized avatar

Alex Melville Melvillian

View GitHub Profile
@Melvillian
Melvillian / big-o-runtimes.js
Created November 18, 2024 19:45
Simple Node.js script for testing yourself on the Big O runtimes of popular data structures (Arrays, Linked List, Queues, AVL Trees, etc...)
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const dataStructureNames = [
"Array", "Hashmap", "Linked List", "Queue", "Stack", "Graph", "Binary Tree", "AVL Tree", "Min-Max Heap",
"Skip List", "B Tree"
@Melvillian
Melvillian / add-caption.py
Created September 4, 2024 19:15
short and sweet code for downloading a jpg from the internet and making a meme with text from it. Requires you download impact.ttf font (which is free)
import requests
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
def draw_caption_internal(image, caption, where, is_bottom):
text_bbox = draw.textbbox((0, 0), caption, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
@Melvillian
Melvillian / ArraySortingExample.sol
Created January 13, 2022 16:46
A Solidity contract comparing the O(n) algorithm for removing an element from an array and preserving sortedness, and a O(1) algorithm that does not keep the array sorted
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.10;
contract ArraySortingExample {
address[] public users;
// simple function to add an address to the users array
pragma solidity ^0.5.0;
import "./PaymentSharer.sol";
contract Attacker {
address private victim;
address payable owner;
constructor() public {
owner = msg.sender;
@Melvillian
Melvillian / PaymentSharer.sol
Last active November 5, 2021 16:04
A contract that seems to follow checks-effects-interaction pattern, but is still vulnerable to re-entrancy, taken from https://gist.github.com/ritzdorf/a00d68d015d2834e25df54d710ebfb3b#file-attacker-js
pragma solidity 0.8.4;
contract PaymentSharer {
mapping(uint => uint) splits;
mapping(uint => uint) deposits;
mapping(uint => address payable) first;
mapping(uint => address payable) second;
function init(uint id, address payable _first, address payable _second) public {
require(first[id] == address(0) && second[id] == address(0));
@Melvillian
Melvillian / Lightning Network installation script
Last active August 18, 2018 04:14
Bash commands for getting lnd (The Lightning Network) *testnet* setup on a Linux machine
#### Steps for downloading btcd: ####
# Run this script on a machine with at least 2 Gb of RAM and 200 GB of disk space
wget https://redirector.gvt1.com/edgedl/go/go1.9.2.linux-amd64.tar.gz
sudo tar -C /usr/local/ -xzf go1.9.2.linux-amd64.tar.gz
cd
mkdir go
export PATH=$PATH:/usr/local/go/bin
@Melvillian
Melvillian / gist:17d10138f616893eeb88525987c6f7d5
Last active August 12, 2023 12:05
Solutions to "move_semantics2.rs" of Rustlings
/// given this uncompilable code
pub fn main() {
let vec0 = Vec::new();
let mut vec1 = fill_vec(vec0);
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
vec1.push(88);