Clicky

馃敳 Magic Squares and Big-Ms

Note: This post was updated to work with Python 3 and PySCIPOpt. The original version used Python 2 and python-zibopt. It has also been edited for clarity. Back in October of 2011, I started toying with a model for finding magic squares using SCIP. This is a fun modeling exercise and a challenging problem. First one constructs a square matrix of integer-valued variables. from pyscipopt import Model # [...snip...] m = Model() matrix = [] for i in range(size): row = [m....

January 12, 2012 路 Ryan O'Neil

鈴筹笍 Know Your Time Complexities - Part 2

In response to this post, Ben Bitdiddle inquires: I understand the concept of using a companion set to remove duplicates from a list while preserving the order of its elements. But what should I do if these elements are composed of smaller pieces? For instance, say I am generating combinations of numbers in which order is unimportant. How do I make a set recognize that [1,2,3] is the same as [3,2,1] in this case?...

November 25, 2011 路 Ryan O'Neil

鈴筹笍 Know Your Time Complexities

This is based on a lightning talk I gave at the LA PyLadies October Hackathon. I鈥檓 actually not going to go into anything much resembling algorithmic complexity here. What I鈥檇 like to do is present a common performance anti-pattern that I see from novice programmers about once every year or so. If I can prevent one person from committing this error, this post will have achieved its goal. I鈥檇 also like to show how an intuitive understanding of time required by operations in relation to the size of data they operate on can be helpful....

October 25, 2011 路 Ryan O'Neil

馃幇 Deterministic vs. Stochastic Simulation

I find I have to build simulations with increasing frequency in my work and life. Usually this indicates I鈥檓 faced with one of the following situations: The need for a quick estimate regarding the quantitative behavior of some situation. The desire to verify the result of a computation or assumption. A situation which is too complex or random to effectively model or understand. Anyone familiar at all with simulation will recognize the last item as the motivating force of the entire field....

June 11, 2011 路 Ryan O'Neil

馃敭 NetworkX and Python Futures

Note: This post was updated to work with NetworkX and for clarity. It鈥檚 possible this will turn out like the day when Python 2.5 introduced [coroutines][coroutines]. At the time I was very excited. I spent several hours trying to convince my coworkers we should immediately abandon all our existing Java infrastructure and port it to finite state machines implemented using Python coroutines. After a day of hand waving over a proof of concept, we put that idea aside and went about our lives....

May 19, 2011 路 Ryan O'Neil

馃惇 Reformed JAPHs: Transpiler

Note: This post was edited for clarity. For the final JAPH in this series, I implemented a simple transpiler that converts a small subset of Scheme programs to equivalent Python programs. It starts with a Scheme program that prints 'just another scheme hacker'. (define (output x) (if (null? x) "" (begin (display (car x)) (if (null? (cdr x)) (display "\n") (begin (display " ") (output (cdr x))))))) (output (list "just" "another" "scheme" "hacker")) The program then tokenizes that Scheme source, parses the token stream, and converts that into Python 3....

April 20, 2011 路 Ryan O'Neil

馃惇 Reformed JAPHs: Turing Machine

Note: This post was edited for clarity. This JAPH uses a Turing machine. The machine accepts any string that ends in '\n' and allows side effects. This lets us print the value of the tape as it encounters each character. While the idea of using lambda functions as side effects in a Turing machine is a little bizarre on many levels, we work with what we have. And Python is multi-paradigmatic, so what the heck....

April 18, 2011 路 Ryan O'Neil

馃惇 Reformed JAPHs: Huffman Coding

Note: This post was edited for clarity. At this point, tricking python into printing strings via indirect means got a little boring. So I switched to obfuscating fundamental computer science algorithms. Here鈥檚 a JAPH that takes in a Huffman coded version of 'just another python hacker', decodes, and prints it. # Build coding tree def build_tree(scheme): if scheme.startswith('*'): left, scheme = build_tree(scheme[1:]) right, scheme = build_tree(scheme) return (left, right), scheme else: return scheme[0], scheme[1:] def decode(tree, encoded): ret = '' node = tree for direction in encoded: if direction == '0': node = node[0] else: node = node[1] if isinstance(node, str): ret += node node = tree return ret tree = build_tree('*****ju*sp*er***yct* h**ka*no')[0] print( decode(tree, bin(10627344201836243859174935587)....

April 14, 2011 路 Ryan O'Neil

馃惇 Reformed JAPHs: Rolling Effect

Note: This post was updated to work with Python 3.12. It may not work with different versions. Here鈥檚 a JAPH composed solely for effect. For each letter in 'just another python hacker' it loops over each the characters ' abcdefghijklmnopqrstuvwxyz', printing each. Between characters it pauses for 0.05 seconds, backing up and moving on to the next if it hasn鈥檛 reached the desired one yet. This achieves a sort of rolling effect by which the final string appears on our screen over time....

April 11, 2011 路 Ryan O'Neil

馃惇 Reformed JAPHs: ROT13

Note: This post was updated to work with Python 3.12. It may not work with different versions. No series of JAPHs would be complete without ROT13. This is the example through which aspiring Perl programmers learn to use tr and its synonym y. In Perl the basic ROT13 JAPH starts as: $foo = 'whfg nabgure crey unpxre'; $foo =~ y/a-z/n-za-m/; print $foo; Python has nothing quite so elegant in its default namespace....

April 6, 2011 路 Ryan O'Neil