Skip to content

Instantly share code, notes, and snippets.

View juanplopes's full-sized avatar
🌲
Is that a Segment Tree problem?

Juan Lopes juanplopes

🌲
Is that a Segment Tree problem?
View GitHub Profile
@juanplopes
juanplopes / rsa.py
Last active July 14, 2023 17:35
RSA by example
from random import randint
#----Step 1
# First, choose two random primes.
# In real world, they should be really big primes (hundreds of digits).
p, q = 41, 47
#----Step 2
# From them we have n=p*q and phi(n)=(p-1)*(q-1).
#!/bin/bash
set -e
mkdir -p ~/Library/KeyBindings
cat > ~/Library/KeyBindings/DefaultKeyBinding.dict << EOF
{
/* Remap Home / End keys to be correct */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
@juanplopes
juanplopes / dfa.py
Last active September 28, 2021 16:37
simple dfa in python
class Automaton:
def __init__(self, nstates):
self.transitions = [{} for i in range(nstates)]
self.accept_states = [False] * nstates
def register(self, source_state, char, target_state):
self.transitions[source_state][char] = target_state
def register_accept(self, state):
self.accept_states[state] = True
@juanplopes
juanplopes / schedule.py
Created September 30, 2014 13:13
Simple scheduling algorithm in python (2nd version)
import heapq
def schedule(tasks):
heap = []
def offer(begin, end):
while heap and heap[0] <= begin:
heapq.heappop(heap)
heapq.heappush(heap, end)
return len(heap)
#include "stdio.h"
typedef struct tree tree;
struct tree{
int value;
tree *left ;
tree *right;
};
tree *tree_first_bigger(tree *t) {
@juanplopes
juanplopes / TimeoutString.java
Last active February 4, 2021 16:52
CharSequence with timeout (useful to timeout a backtracking regex)
import java.util.Objects;
public class TimeoutString implements CharSequence {
private final CharSequence child;
private final long target;
private TimeoutString(CharSequence child, long target) {
this.child = child;
this.target = target;
}
# -*- coding: utf-8 -*-
from collections import deque
from functools import reduce
def rex(pattern):
tokens = deque(pattern)
def walk(chars):
while tokens and tokens[0] in chars:
yield tokens.popleft()
Crescimento por dia: 36.84%
2020-02-26 0 1
2020-02-27 0 1
2020-02-28 1 1
2020-02-29 1 2
2020-03-01 2 2
2020-03-02 2 2
2020-03-03 3 2
2020-03-04 5 4
2020-03-05 7 4
import datetime, numpy as np
first_day = datetime.date(2020, 2, 26)
y = np.array([1, 1, 1, 2, 2, 2, 2, 4, 4, 13, 13, 20, 25, 31, 38, 52, 151, 151, 162, 200, 321, 372, 621, 793])
x = np.arange(1, len(y)+1)
curve = np.exp(np.polyfit(x, np.log(y), 1))
#y = b*a^x
#log(y) = xlog(a)+log(b)
var performanceReport = function(perf) {
if (perf == null)
return console.log("No storage query was run yet!");
var perc = n => isNaN(n) ? 0 : Math.round(n*1000.0)/10;
var expectedEvents = 0, actualEvents = 0, cacheReads=0, cacheWrites=0, metrics = {};
var count = {'init': 0, 'read': 0, 'flow': 0, 'misc': 0, 'filter': 0 };
var time = {'init': 0, 'read': 0, 'flow': 0, 'misc': 0, 'filter': 0 };