Skip to content

Instantly share code, notes, and snippets.

View PurpleMyst's full-sized avatar

Nicolò Francesco Maria Spingola PurpleMyst

  • Italy
  • 16:32 (UTC +02:00)
View GitHub Profile
# Emoji grabber
# Grabs emoji from the HTML of discord messages
import fileinput
from pathlib import Path
import bs4
import requests
from tqdm import tqdm
@PurpleMyst
PurpleMyst / show_error.rs
Created February 11, 2020 12:53
Rust function to display an error message with a format similiar to rustc's own
fn show_error(
code: &[u8],
filename: &str,
message: &str,
span: std::ops::Range<usize>,
) -> io::Result<()> {
const ARROW: &str = "-->";
let stdout = io::stdout();
let mut h = stdout.lock();
@PurpleMyst
PurpleMyst / gist.zsh
Created January 13, 2020 09:51
Create gists from the command line :octocat:
#!/usr/bin/env zsh
setopt ERR_EXIT NO_UNSET PIPE_FAIL
unsetopt FUNCTION_ARGZERO
if [[ -z ${GIST_API_TOKEN:-} ]]; then
if [[ -f ${XDG_CONFIG_HOME:-$HOME/.config}/gist-api-token.txt ]]; then
GIST_API_TOKEN=$(cat ${XDG_CONFIG_HOME:-$HOME/.config}/gist-api-token.txt)
else
printf 'No GIST API token provided!\n'
@PurpleMyst
PurpleMyst / make.sh
Created January 13, 2020 09:50
onlinegdb.com uploader
#!/usr/bin/env sh
uglifyjs onlinegdb.user.js --mangle --compress expression
@PurpleMyst
PurpleMyst / pip_updates.sh
Created October 16, 2019 22:52
Create a requirements.txt which updates all your local packages
#!/usr/bin/env bash
# shellcheck disable=SC2016
set -euo pipefail
python3 -m pip freeze \
| rg --invert-match '^-e' \
| cut -d'=' -f'1' \
| xargs python3 -m pip show \
| rg --multiline 'Name: (\w+)\nVersion: (.+)' --replace '$1==$2'
@PurpleMyst
PurpleMyst / pipe.py
Created October 11, 2019 15:56
Elixir's pipe operator as a Python NodeTransformer
#!/usr/bin/env python3
import ast
class RewritePipe(ast.NodeTransformer):
def visit_Call(self, node: ast.Call) -> ast.expr:
if not isinstance(node.func, ast.Name) or node.func.id != "pipe":
return node
args = iter(node.args)
@PurpleMyst
PurpleMyst / brainfuck.awk
Last active August 2, 2019 00:13
Brainfuck interpeter in GAWK
#!/usr/bin/awk -f
# Concatenate all of the input into one variable so that we can jump from
# anywhere and to anywhere
{ program = program $0 }
END {
# Due to awk not having a builtin ord() function, we have to create our own
# mapping from dict to ASCII value
for (i = 0; i <= 127; i++) ord[sprintf("%c", i)] = i
@PurpleMyst
PurpleMyst / minesweeper.py
Created July 15, 2019 17:58
Minesweeper solver in python
#!/usr/bin/env python3
# vim: foldmethod=indent
import random
import sys
import typing as t
import attr
GRID_SIDE = 8
MINE_DISTRIBUTION = 1 / 6
@PurpleMyst
PurpleMyst / trie.c
Last active February 10, 2024 00:24
Simple prefix tree (AKA Trie) implementation in C using 60SLOC (+ a C++ implementation of the same size for fun)
#include <assert.h> // assert
#include <stdlib.h> // calloc, free
/** A prefix tree
* An efficient key-value storage with string keys
*
* There exist two type of nodes:
* 1. node->symbol == 0 -> node where a key ended, has node->value
* 2. node->symbol != 0 -> node representing a character in a key, has node->child
*
@PurpleMyst
PurpleMyst / timed.rs
Last active August 25, 2018 15:58
Macro for timing an expression in Rust.
macro_rules! timed {
($expr:expr) => {{
let start = ::std::time::Instant::now();
let value = $expr;
let end = ::std::time::Instant::now();
let elapsed = end.duration_since(start);
println!(
"Ran {:?} in {} seconds and {} milliseconds",
stringify!($expr),
elapsed.as_secs(),