Skip to content

Instantly share code, notes, and snippets.

@mdamien
mdamien / gist:6700684
Created September 25, 2013 14:44
Parse a wordpress shortcode into a PHP array
function get_atts_from_shortcode_with_a_good_old_regex($content) {
$regexp = '/(.?)\[(shop)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)/s';
$m = array();
preg_match($regexp, $content, $m);
$arr = array('content' => $m[5]);
$arr = array_merge(shortcode_parse_atts($m[3]),$arr);
return $arr;
}
@mdamien
mdamien / gist:6863909
Created October 7, 2013 07:39
Database in O(1) (thumbtack challenge)
from collections import Counter
class State(dict):
"""
A state contains the previous value of each modified key
and the previous Counter object
"""
def __init__(self,counter):
self.counter = counter
import sys
def execute(code):
code = ''.join([x for x in code if x in '.,+-<>[]'])
cells,ptr,i = [0]*3000,0,0
while i < len(code):
c = code[i]
if c == '>': ptr += 1
if c == '<': ptr -= 1
if c == '+': cells[ptr] += 1
@mdamien
mdamien / gist:9273931
Last active February 6, 2022 22:24
Android POST request with Basic Auth
Log.d(TAG, "API CALL: POST "+url);
OutputStream output = null;
try {
String query = "furnail_name=hell&hi=2";//use URLEncode here
URLConnection connection = new URL("https://your-url.com/action/save").openConnection();
String authString = "Basic " + Base64.encodeToString((username + ":" + password).getBytes(),Base64.NO_WRAP);
connection.setRequestProperty("Authorization", authString);
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + "UTF-8");
@mdamien
mdamien / gist:9399963
Created March 6, 2014 21:22
Quick and Dirty list section header for Android
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="false"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<View
android:id="@+id/item_separator"
android:layout_width="match_parent"
android:layout_height="1dp"
@mdamien
mdamien / comb.py
Last active August 29, 2015 14:00
the limit
def cross(X,Y):
if len(X) == 0:
for y in Y:
yield y
for x in X:
for y in Y:
yield (x,y)
def combination(n):
@mdamien
mdamien / rainbow.js
Created April 29, 2014 20:04
Rainbow
function rainbow(step) {
var r, g, b;
var h = step / 70;
var i = ~~(h * 6);
var f = h * 6 - i;
var q = 1 - f;
switch (i % 6) {
case 0:
r = 1, g = f, b = 0;
break;
@mdamien
mdamien / dico.py
Created May 13, 2014 18:55
Basic dictionnary with WordNet
defs = {}
for i, line in enumerate(open('data.noun')):
if not line.startswith('0'):
continue
splits = line.split(' ')
word = splits[4]
seps = line.split('|')
bla = seps[1].split(';')
definition = bla[0]
@mdamien
mdamien / a_little_experiment.md
Last active August 29, 2015 14:01
Direct circular dependencies in the dictionnary

Because every word in a dictionnary is referenced by another word, there must be a lot of circular dependency

Example:

  • Definition of the: denoting one or more people or things already mentioned or assumed to be common knowledge
  • Definition of one: the lowest cardinal number; half of two; 1.

Using the Wordnet dictionnary, I looked for circular dependency. But I found so many direct dependency, I'm now looking for a better dictionnary (Wiktionnary most probably).

So, here is the result of the direct dependency lookup and the script to obtain the result.

@mdamien
mdamien / ai.json
Last active February 25, 2022 13:46
Google publi
{"A Discriminative Latent Variable Model for Online Clustering":["Rajhans Samdani"],"Affinity Weighted Embedding":["Ron Weiss","Hector Yee"],"Applications of Maximum Entropy Rankers to Problems in Spoken Language Processing":["Richard Sproat","Keith Hall"],"Asynchronous Stochastic Optimization for Sequence Training of Deep Neural\n Networks":["Georg Heigold","Erik McDermott","Vincent Vanhoucke","Andrew Senior","Michiel Bacchiani"],"Autoregressive Product of Multi-frame Predictions Can Improve the Accuracy of\n Hybrid Models":["Vincent Vanhoucke"],"Bayesian Sampling Using Stochastic Gradient Thermostats":[],"Bridging Text and Knowledge with Frames":["Srini Narayanan"],"Cicada: Predictive Guarantees for Cloud Network Bandwidth":[],"Corporate learning at scale: Lessons from a large online course at Google":["Mehryar Mohri","Afshin Rostamizadeh","Umar Syed"],"DaMN – Discriminative and Mutually Nearest: Exploiting Pairwise Category\n Proximity for Video Action Recognition":["Rahul\n Sukthankar"],"Deep Convolutiona