OLW: #Python Subsets of Set

The following nifty #oneliner uses #functional #programming concepts available in Python to return all subsets of an array argument:

f = lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])

That is, f([1, 1, 1, 4, 1, 10, 4, 1, 4, 10]) will return [[],[1],[4],[10],[1,4],[1,10],[4,10],[1,4,10]]

“I was born at a very young age.”  -Groucho Marx

OLW: #RegularExpression Joke

Here’s a #regex (for languages like #Go, #Java, #Lua, #Javascript, #ObjectiveC, #Perl, #Python, #Ruby – any language that uses regular expressions) that is both code and joke rolled in to one:

question = /(bb|[^b]{2})/

In case you still do not get it, think of Hamlet…

“I saw a dog in a cage. And that cage had a sign on it that said, ‘I bite.’ And I was like, ‘That is good to know doggy, but that’s not the most important thing about you. You should make a sign that says, ‘I make signs.’’” – Doug Benson

OLW: Detect Browser Version

#Javascript to detect browsers like #Firefox, #Chrome, #Safari, #InternetExplorer or #Opera can be incredibly useful. We looked around and saw inconsistent results with many implementations, but this one line native implementation slightly modified from StackOverflow proved to be both accurate and fast.

var u=navigator.userAgent, t, m=u.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if(/trident/i.test(m[1])){t=/\brv[ :]+(\d+)/g.exec(u) || []; 'IE '+(t[1] || '')} else if(m[1]==='Chrome' && (t=u.match(/\bOPR\/(\d+)/))!=null){'Opera '+t[1]} else { m=m[2]? [m[1],m[2]]: [navigator.appName, navigator.appVersion, '-?']; if((t= u.match(/version\/(\d+)/i))!= null) m.splice(1, 1, t[1]); m.join(' ')}

Use it when you need to handle browser-specific bugs in any way, or if you want to provide a different experience to users on different browsers.

“Getting on a plane, I told the ticket lady, ‘Send one of my bags to New York, send one to Los Angeles, and send one to Miami.’ She said, ‘We can’t do that!’ I told her, ‘You did it last week!’” – Henny Youngman

OLW: #Integral #Calculus Approximation

This handy #Python #One-liner can help you impress your high school cousin who is just starting to learn the calculus that you have forgotten years ago! This uses the mid-point rectangle approximation method. You can specify the number of rectangles to use in order to improve accuracy.

def integrate(function, n=1000, start=0, stop=100): return (float(stop-start)/n) * sum([eval(function) for x in [start+((i+0.5)*float(stop-start)/n) for i in range(n)]])

Use it as follows:

integrate(‘sin(x)’,1000,0,1) is equivalent to 

“Never under any circumstances take a sleeping pill and a laxative on the same night.”

OLW: #Copy to #Clipboard #Javascript

In #Firefox #Firebug or the #Chrome dev console, there is a neat shorthand to copy an object to the clipboard and paste it in a neat, structured order. The function is called “copy.” Unfortunately, you can’t use it anywhere outside of this console, but it does make your life a lot easier when trying to copy complex nested data structures between windows.

Using it is as simple as you would imagine: copy(obj) will copy the object obj to your clipboard. Then just ctrl+v to paste it in another window!

“The telephone is a good way to talk to people without having to offer them a drink.” – Fran Lebowitz

OLW: #War #Card #Game

Here is a #ruby #one-liner to perform the decision portion when solving for the winner of the popular card game “War.”

def calc_war(deck1,deck2,pot)
  winner=[];var1=deck1.shift.to_i;var2=deck2.shift.to_i;
    pot+=[var1] if (var1>0);pot+=[var2] if (var2>0);
    (var1>var2)?(deck1+=pot;pot.clear):((var1<var2)?
    (deck2+=pot;pot.clear):(pot+=deck1.shift(3)+deck2.shift(3);
  return calc_war(deck1,deck2,pot)))

  return calc_war(deck1,deck2,pot) if (!(winner[2] = deck1.empty?) &&
    !(winner[1] = deck2.empty?))

  return winner.index(true)
end

You can use it like this:
full_deck = (2..14).to_a*4
full_deck.sort! {|a,b| rand <=> rand}
calc_war(full_deck.values_at(0..25),full_deck.values_at(26..51),[])

“The first guy who persuaded a blind person they needed sunglasses – he must have been a hell of a salesmen.” – Jimmy Carr

OLW: #Array to #Hash Chain

This is a #ruby #one-liner that lets you convert an array into a chain of hashes. For example, a set of progressively more specific terms like [“Author”,”Book”,”Chapter”,”Paragraph”,”Sentence”,”Word”] will convert to a representative hash {“Author”=>{“Book”=>{“Chapter”=>{“Paragraph”=>{“Sentence”=>”Word”}}}}}. This is useful if you want to store nested data as a flat array or array string but then later parse it out as a nested hash to more easily work with.

The code is quite simple to apply to array arr:

arr.reverse.inject{|y,x|{x=>y}}

“Sooner or later, I’ll be punctual.” – Ashleigh Brilliant