Skip to content

Instantly share code, notes, and snippets.

View pedropedruzzi's full-sized avatar

Pedro Pedruzzi pedropedruzzi

View GitHub Profile
@pedropedruzzi
pedropedruzzi / runWithLimitedConcurrency.ts
Created March 2, 2023 17:35
TypeScript Experiment: run async functions with limited concurrency
// Copyright (c) 2023 Vendah Atividades de Internet LTDA. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// Get a copy at https://opensource.org/licenses/MIT.
export async function runWithLimitedConcurrency<T>(concurrency: number, jobs: (() => Promise<T>)[]): Promise<T[]> {
const setsOfJobs = batchify(jobs, Math.ceil(jobs.length / concurrency));
const promises: Promise<T[]>[] = [];
for (const [setIndex, setOfJobs] of setsOfJobs.entries()) {
const instrumentedSetOfJobs = setOfJobs.map((job, jobIndex) => () => {
// https://noticias.uol.com.br/eleicoes/2022/apuracao/2turno/votos-por-estado/presidente/
function compute() {
const states = document.querySelectorAll('.president-by-state')
const estimates = {};
let computedVotes = 0, minComputePercentPerState = 100, minComputePercentState;
for (const state of states) {
const stateName = state.querySelector('.president-by-state-label').innerText;
const stateVoters = Number(state.querySelector('.voters').innerText.replace(/\D/g, ''));
const computedPercent = parseFloat(state.querySelector('.president-by-state-apuration .percentage').innerText.replace(',', '.'));
@pedropedruzzi
pedropedruzzi / generic-type-bug.ts
Created February 2, 2022 17:47
Generic Type Bug in TypeScript
interface Shape {
name: string;
}
interface Square extends Shape {
length: number;
}
interface Circle extends Shape {
radius: number;
@pedropedruzzi
pedropedruzzi / MiBACT-Antenati-direct-image-links.user.js
Last active September 18, 2020 12:31
Greasemonkey script that replaces links on image galleries with direct (faster) image links on MiBACT Antenati website. Instructions: 1. Install Greasemonkey (Firefox) or Tampermonkey (Chrome/Firefox). 2. Click https://gist.github.com/pedrox/365fd278393b651a9e6780e7866c469d/raw/MiBACT-Antenati-direct-image-links.user.js
@pedropedruzzi
pedropedruzzi / QuickSort.java
Created March 5, 2019 15:49
Iterative QuickSort
package algorithms;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
public class QuickSort {
private static class Interval {
int low;
@pedropedruzzi
pedropedruzzi / array.py
Created April 12, 2018 22:17
Array Problem
import random
def solve(a):
n = len(a)
# get inside the loop by moving n steps (better approach is part 1 of Floyds's below)
x = 0
for i in range(n):
x = a[x]
# measure the period
period = 1
@pedropedruzzi
pedropedruzzi / download-na-busca-do-legendas-tv.user.js
Last active April 7, 2021 03:10
Greasemonkey script que permite download de legendas a partir dos resultados de busca do site legendas.tv. Instruções: 1. Instale o Greasemonkey (Firefox) ou Tampermonkey (Chrome ou Firefox). 2. Clique em https://gist.githubusercontent.com/pedropedruzzi/f25a010f8a787352575d8f312433979b/raw/download-na-busca-do-legendas-tv.user.js
// ==UserScript==
// @name Download na busca do legendas.tv
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Permite download de legendas a partir dos resultados de busca do site legendas.tv
// @author Pedro Pedruzzi
// @match http://legendas.tv/busca/*
// @match https://legendas.tv/busca/*
// @grant none
// @downloadURL https://gist.githubusercontent.com/pedropedruzzi/f25a010f8a787352575d8f312433979b/raw/download-na-busca-do-legendas-tv.user.js
@pedropedruzzi
pedropedruzzi / rnaa.rs
Created May 29, 2017 13:16
Experimenting with Rust
use std::cmp;
// Note: this is a suboptimal solution made to exercise the Rust language.
// Approach A: using a struct/class
// Gotcha: Had to add lifetime specifier to store a reference to the byte array of a external
// string inside my struct.
struct Rnaa<'a> {
chain: &'a [u8], // 'a is to get rid of "missing lifetype specifier"
@pedropedruzzi
pedropedruzzi / watertanks.py
Created April 25, 2017 04:29
Water Tanks Problem
# gets list of key walls from a sorted list of walls
def _keywalls(walls):
prevheight = pos = highest = 0
keywalls = []
for pos, height in walls:
if height > highest: # raises globally
highest = height
elif height >= prevheight: # raises locally
while keywalls[-1][1] < height:
keywalls.pop() # discard recent walls until we see with one higher than height (always occurs)
#include <iostream>
#define STRONG_TYPEDEF(original, newtype) \
class newtype : public original \
{ \
using original::original; \
};
using Age = unsigned int;