Skip to content

Instantly share code, notes, and snippets.

View diezguerra's full-sized avatar

Saúl diezguerra

View GitHub Profile
int equi ( int[] arr ) {
long left = 0;
long right = 0;
/* sumamos todos a la izquierda */
for(int i : arr) {
left += i;
}
/* vamos comprobando y restando a la izquierda y sumando a la dcha */
set nocompatible
filetype off
set rtp+=~/.vim/bundle/vundle
call vundle#rc()
Bundle 'gmarik/vundle'
Bundle 'tpope/vim-fugitive'
Bundle 'kevinw/pyflakes'
Bundle 'kevinw/pyflakes-vim'
Bundle 'nvie/vim-flake8'
Bundle 'kien/ctrlp.vim'
# Snippet setting git branch and git dirty helper
# Commit at the end kept from original snippet, I added the rev-parse stuff and the proper redirection.
set fish_git_dirty_color red
set fish_git_clean_color brown
function parse_git_dirty
if test (git status 2> /dev/null ^&1 | tail -n1) != "nothing to commit (working directory clean)"
echo (set_color $fish_git_dirty_color)
else
@diezguerra
diezguerra / filterdict.py
Last active December 14, 2015 00:39
How to filter dicts in python, the right way.
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <rawcell>
# How to filter Python dictionaries, the right way.
#
# Expected output:
# Original list lookup => 100 loops, best of 3: 9.82 ms per loop
# Dict trick lookup => 1000 loops, best of 3: 315 us per loop
@diezguerra
diezguerra / slice_upsert.py
Last active December 15, 2015 18:29
Python Slice Upsert
a_range = range(12)
a_range[-2:] = range(10,20)
assert a_range == range(20)
a_range[10:11] = range(10)[::-1]
a_range[20:] = []
assert a_range == range(10) + range(10)[::-1]
keywords = "apple, pear, banana, lemon and orange"
keywords = keywords.split(', ')
@diezguerra
diezguerra / dict_optimization.py
Created May 2, 2013 06:08
Dict initialization optimization
"""
Result:
Using original function 1000 loops, best of 3: 200 us per loop
Using itertools.chain 10000 loops, best of 3: 130 us per loop
Using joined sets 10000 loops, best of 3: 142 us per loop
Using double the tags and modulo 1000 loops, best of 3: 184 us per loop
"""
@diezguerra
diezguerra / noleaks.py
Last active December 17, 2015 07:19
Closure saving and GC example. # needs memory_profiler module! $ python -m memory_profiler noleaks.py
#!/usr/bin/env python2.7
import gc
def iterjam():
import functools as ft
def iterjam2():
import requests
import json
r = requests.get('https://graph.facebook.com/search?q=&type=adcountry&limit=500')
countries = {country['name']: country['country_code'] for country in r.json()['data']}
assert countries['Spain'] == 'ES'
assert len(countries) == 216
import dateutil.parser
import pytz
date_string = '2013-10-14T12:47:12.776270'
date_object = pytz.timezone('America/Los_Angeles').localize(dateutil.parser.parse(date_string))
print date_object.isoformat()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.