Skip to content

Instantly share code, notes, and snippets.

/*
An improvement of the Disjoint-Set Forest is to control tree heights storing in each node
its rank, which is an upper bound for its height. When a node is initialized, its rank is
set to zero. To merge trees with roots x and y, first compare their ranks. If the ranks
are different, then the larger rank tree becomes the parent, and the ranks of x and y do
not change. If the ranks are the same, then either one can become the parent, but the new
parent's rank is incremented by one. While the rank of a node is clearly related to its
height, storing ranks is more efficient than storing heights. The height of a node can
change during a Find operation, so storing ranks avoids the extra effort of keeping the
height correct. This method ensures that trees do not become too deep.
#!/usr/bin/env python3
"""
Returns the total stopping time of positive number n in the Collatz sequence.
"""
def collatz(n: int) -> bool:
def get_next(n: int) -> int:
return n % 2 == 0 and n//2 or (3*n + 1)//2
"""
Shortcut: 3n + 1 is even if n is odd, so we can divide it by 2;
@lcpz
lcpz / euler0347.cc
Last active February 26, 2022 03:14
// https://projecteuler.net/problem=347
#include <vector>
#include <cmath>
#include <iostream>
std::vector getSieveOfEratosthenes(const int& n)
{
if (n <= 0) return {};
#include <iostream>
struct Point { // 2-D
double x, y;
Point(double x, double y) : x(x), y(y) {}
};
// Barycentric method - https://stackoverflow.com/a/9755252
bool isInsideTriangle(const Point& a, const Point& b, const Point& c, const Point& s)
{
@lcpz
lcpz / revolut-gma.bash
Last active August 4, 2023 17:04
Giacenza Media Annua for Revolut accounts
#!/bin/bash
# Compute the Giacenza Media Annua (GMA) of a Revolut account, required by the Italian INPS.
# Dependencies: awk, GNU date, getopts, xargs
# Computation method: https://bit.ly/3avDLu3
# Thread on Revolut forum: https://bit.ly/3511e5h
# Assumptions:
@lcpz
lcpz / eduroam.conf
Created July 29, 2019 10:19
wpa_supplicant eduroam minimal configuration
network={
ssid="eduroam"
scan_ssid=1
key_mgmt=WPA-EAP
eap=TTLS
#anonymous_identity="[email protected]"
#ca_cert="/etc/ssl/certs/AddTrust_External_Root.pem"
phase2="auth=MSCHAPV2"
identity="<email>"
password="<pwd>"
@lcpz
lcpz / get_secret.lua
Created November 24, 2018 14:04
Example of password retrieval function for lain IMAP widget - https://github.com/lcpz/lain/wiki/imap/#using-a-password-manager
-- Source: https://bitbucket.org/seregaxvm/awesome-wm-configs/src/master/get_secret.lua
local Gio = require("lgi").Gio
local GLib = require("lgi").GLib
local function get_secret(attrs)
local bus = Gio.bus_get_sync(Gio.BusType.SESSION, nil)
local name = "org.freedesktop.secrets"
local object = "/org/freedesktop/secrets"
local interface = "org.freedesktop.Secret.Service"
@lcpz
lcpz / sp.py
Last active November 11, 2023 23:31
Sardinas-Patterson algorithm
#! /usr/bin/env python
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
@lcpz
lcpz / mpd-fade
Created February 26, 2017 13:30
A script to fade volume when toggling MPD via mpc
#!/bin/sh
# A script to fade volume when toggling MPD via mpc
mpc=`which mpc`
# volume commands, customize here
decrease="amixer -q set Master 1%-"
increase="amixer -q set Master 1%+"
@lcpz
lcpz / fbOntology.owl
Last active June 5, 2016 07:44
A Facebook ontology. Written in Protégé 5.0.0 and tested consistent with FaCT++ 1.6.4 reasoner.
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/ontologies/2016/facebook-ontology#"
xml:base="http://www.semanticweb.org/ontologies/2016/facebook-ontology"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.semanticweb.org/ontologies/2016/facebook-ontology"/>