Skip to content

Instantly share code, notes, and snippets.

@ealmansi
ealmansi / visibility.sol
Created October 19, 2017 16:57
Solidity - Visibility.
pragma solidity ^0.4.17;
contract Base {
event Called(string name);
function public_fn() public { Called("Base.public_fn"); }
function private_fn() private { Called("Base.private_fn"); }
function external_fn() external { Called("Base.external_fn"); }
function internal_fn() internal { Called("Base.internal_fn"); }
function () public payable { Called("Base.fallback"); }
@ealmansi
ealmansi / reverse-a-singly-linked-list.cpp
Last active October 13, 2017 17:29
Algorithm to Reverse a Singly-Linked List in C++
#include <iostream>
using namespace std;
struct node;
node * const NIL = (node * const) 0;
struct node {
int value;
node *next;
# https://leetcode.com/problems/word-search-ii/
def makeTrie():
newTrie = {}
return newTrie
def addWord(word, trie):
currentTrie = trie
for letter in word:
if not letter in currentTrie:
@ealmansi
ealmansi / gist:6736d076646fefc3a976
Created October 16, 2015 17:30
Generate Wikipedia Revision Timestamps
// Command in Bash to run Spark shell.
spark-shell --master yarn --driver-memory 50G --num-executors 5 --executor-cores 2 --executor-memory 4G --packages com.databricks:spark-csv_2.10:1.2.0
// Commands in Scala to run within the Spark shell.
val pageMetadataDF = sqlContext.load("/user/ealmansi/data/enwiki-20150901/parquet/page_metadata", "parquet")
pageMetadataDF.registerTempTable("page_metadata")
sql("""SELECT
page_id,
title,
ns,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from itertools import permutations
C = ["white", "blue", "yellow", "green", "red"] # colors
G = ["ps3", "xbox-one", "ps4", "pc", "xbox-360"] # game
P = ["galaxys5", "mslumia", "galaxys6", "iphone6", "iphone5"] # phone
N = ["brit", "american", "swedish", "german", "french"] # nationality
B = ["coca-cola", "coffee", "water", "beer", "pepsi"] # beverage
@ealmansi
ealmansi / romanos.cr
Created December 29, 2014 22:10
numerales romanos a decimal (en crystal)
class String
def from_roman
ans = -1
if self.length == 0
ans = 0
elsif self[0..0] == "M"
ans = 1000 + self[1..self.length].from_roman
elsif self[0..1] == "CM"
ans = 900 + self[2..self.length].from_roman
elsif self[0..0] == "D"
@ealmansi
ealmansi / coloreo.cpp
Created October 13, 2014 00:35
PLP coloreo
#include <iostream>
#include <iomanip>
using namespace std;
// fuerza bruta
int main(int argc, char const *argv[])
{
string cs[] = {"rojo", "azul", "amarillo", "verde"};
int vs[][2] = {
{1, 2}, {1, 3}, {1, 4}, {1, 5},
@ealmansi
ealmansi / word-scrambler.py
Created September 17, 2014 19:08
Word scrambler (so called "Cambridge Effect")
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import random
def myshuffle(xs):
for i in range(len(xs)):
ind = random.randint(i, len(xs) - 1)
xs[i], xs[ind] = xs[ind], xs[i]
@ealmansi
ealmansi / teg.py
Last active December 28, 2017 01:07
TEG: probabilidad de conquistar otro país mediante ataques reiterados
#!/usr/bin/env python3
import math;
##########################################################################################
# Basado en: http://openpool.com.ar/reglamento/T.E.G.-REGLAMENTO.pdf
#
# Probabilidad de conquistar otro pais si atacamos sin frenar hasta que:
# i) conquistamos el otro pais, o
# ii) nos quedamos con una sola ficha y no podemos atacar más
@ealmansi
ealmansi / txt2num.py
Last active August 29, 2015 14:06
Spanish cardinal number to integer conversion
# -*- coding: utf-8 -*-
import re;
class Tokenizer:
def __init__(self, inp):
self.tokens = inp.lower().encode('utf8')
self.substitutions = [
("á", "a"), ("é", "e"), ("í", "i"), ("ó", "o"), ("ú", "u"), ("[^\w]", " "),
("(^| )un( |$)", " uno "), ("(^| )veintiun( |$)", " veintiuno "), ("(^| )cien( |$)", " ciento "), ("(^| )millon( |$)", " millones "), ("(^| )y( |$)", " "),
("(^| )once( |$)", " diez uno "), ("(^| )doce( |$)", " diez dos "), ("(^| )trece( |$)", " diez tres "), ("(^| )catorce( |$)", " diez cuatro "),