Tag Archives: stupid

Donkeypunching Ruby Koans

Do you want instant enlightenment? Sure, we all do.

And now you can have it!

Tonight I presented Ruby Koans at URUG. It started out simple enough, but then we got on a weird quirk about trying to make the Koan tests pass without actually satisfying the test requirements. We monkeypatched Fixnum, then started playing with patching Object#to_s… basically we were looking for TMAETTCPW: The Most AEvil Thing That Could Possibly Work. I spelled Evil AEvil because it’s extra evilly.

Mike Moore had the bright idea to just break off all the assert methods in Test::Unit; after that it just became a challenge to discover how to get the rest of the koans to run at all.

With sincere apologies to Matz, Jim and Joe, here is the result:

https://gist.github.com/1108850

Palindromic Powers of Two

Huh, that’s odd.

Just farting around, reducing the set of Integers to their prime factors—typically Friday-night stuff, amirite?—and I noticed an interesting pattern.

  1. Start with a keyspace of 0 and all of the integers in order as your available keys.
  2. Write a 0.
  3. You’ve exhausted the keyspace, so get another key, 1.
  4. Write the entire series so far, add the new key, and write the series (minus the new key) again. You should have 010.
  5. You’ve exhausted the keyspace again, so get another key, 2.
  6. Repeat: 010, 2, 010 -> 0102010.
  7. Repeat with 3: 010201030102010.
  8. Repeat with 4: 0102010301020104010201030102010.

And so on.

The “Huh!” moment is this: you’re writing the exponent of two in the prime factorization of the list of integers:

1 20
2 21
3 20 * 3
4 22
5 20 * 5
6 21 * 3
7 20
8 23
9 20 * 32
10 21 * 5
11 20
12 22 * 3
13 20
14 21 * 7
15 20 * 3 * 5
16 24

Twitterable Mandelbrot II: The Mandelbrottening

Yesterday I posted my Twitterable Mandelbrot, a ruby script that generates the Mandelbrot Set in 134 characters. A few of you took this as a challenge to shorten my code even further. I didn’t mind, and in fact was interested to see your results; I was sure that an extra character here or there could be shaved off.

What I didn’t expect at all was that somebody would shave fourteen characters off.

Reader brahbur on rubyflow came up with this:

a couple of these changes could be considered “cheating” (-:

80.times{|a|p (0..300).map{|b|x=y=i=0;(x,y,i=x*x-y*y+b/150.0-1.5,2*x*y+a/40.0-1,i+1)until(x*x+y*y>4||i>98);i>98?0:1}*''}

Brahbur’s solution does look different; there are quote marks on each line and it outputs 1s and 0s instead of #s and .s, but the mandelbrot is still clearly visible (Edit: I reduced the size from 300×80 to 240×60 just to keep the outputs roughly the same size):


Click for larger version (1400×800)

I think this is just awesome. Once we’re playing with 0’s and 1’s, I can see another optimization: i>98?0:1 can be replaced with 99i. This bring us down to 118:

60.times{|a|p (0..240).map{|b|x=y=i=0;(x,y,i=x*x-y*y+b/120.0-1.5,2*x*y+a/30.0-1,i+1)until(x*x+y*y>4||i>98);99i}*''}

I have to give most of the credit to brahbur, though–I just saw a tiny tweak, on top of the amazing rewrite they already did. So great. THANK YOU brahbur!

Now, the challenge continues: can you shorten this further? Brahbur was concerned about “cheating”, so let’s define the rules for clarity: Output should be 240×60 (extra quotes and padding are okay) and it should be visually recognizable as a Mandelbrot set. Other than that, go for it.

Twitterable Mandelbrot

As a kid I always thought fractals were neat, but every time I tried to learn how to do them, I got lost in the math. I guess 20 years makes all the difference: today I went and read up on the Mandelbrot set and had one of those “wait, that’s it?” moments.

It took me about 15 minutes to write the program. Here’s the output:


Click for larger version (1400×800)

The whole program was about 400 characters long. I got to thinking, “that’s *almost* small enough to fit into a single tweet…” and then I spent the next hour and a half refactoring my code for size.

Victory:


http://twitter.com/dbrady/status/12546255974

What do you mean I need a hobby? I have one. See?