177 One-Liners Mashup


This week is going to be very informal, and without any particular task or submission. It’s hunting season, and we’re hunting one-liners.

Basically, we’ll start with the simple problem I’ve presented below. Your solution must fit in one line. (Golfing is okay, but not necessary. One line generally means about 80 chars wide, but we’re flexible here.) If you are writing a method, the def foo(args) and end (and class Whatever and end for adding methods to a class) doesn’t count… the body of the method will.

Of course, your solutions should be generally useful, and not hard-coded to solve any particular example used to illustrate what the solution should do.

Post your solution AND a followup problem for others to solve. Repeat ad nauseum (or until about Wed/Thu).


See the One-Liners Mashup thread on the Ruby-Talk Mailing List for the full set of problems and discussion.


Thanks to everyone who joined in on the one-liners mashup. I think part of the challenge to the mashup was coming up with new problems. It seems that, unless you’ve already solved the problem, it takes a bit of intuition to come up with a problem that isn’t trivial, but also that can be done within a typical line or two. All the problems presented seemed to fit right in.

I’m not going to do a regular summary here: the discussion on Ruby Talk pretty much is the summary. I will run through all the problems very quickly, just pulling out the solution that appealed to me the most in each case.

The problem:

Write a single line method for Array that does this:
  > [:one, "two", 4].repeat(3)
  => [:one, :one, :one, "two", "two", "two", 4, 4, 4]

“The Nice” solution (I agree):

def repeat(n)
  inject([]) { |a,e| a.concat [e]*n }

The problem:

Print out a Sierpinski carpet.

The shorter (but still kinda long) solution:

def carpet(n)
  n==0?"#\n":carpet(n-1).map{|l| ['\0\0\0','\0 \0','\0\0\0'].map{|c| l.gsub(/#| /,c)}}.join

The problem:

Given the class: 

  class Data2D
    def initialize
      @data = [ ]  # in row major form

    def add_row(*row)
      @data << row

And this setup for an object:

  data = Data2D.new
  data.add_row(1, 2, 3, 4)
  data.add_row(5, 6, 7, 8)

define a [] method that makes this form of access possible:
  data[2][1]  # => 7

The tricksy call-you-later solution:

class Data2D
  def [](x) lambda { |y| @data[y][x] }

The problem:

Write an []= method to solve the following:
  obj = YourClass.new
  obj['answer'] = 42
  obj.answer  # => 42

The what-the-heck-with-all-the-eval solution:

class YourClass
  def []=(f, v)
    class << self; self end.instance_eval{ attr_accessor f }; instance_eval "@#{f}=v"

The problem:

Given one or more input filenames on the command line, report the number of unique IP addresses
found in all the data.

The command-line solution:

ruby -e 'p ARGF.read.scan(/\d{1,3}(\.\d{1,3}){3}/).uniq.size'

The problem:

Here's something simple: define method roll(n, s) to roll a s-sided
die n times and display a bar graph of the results. So roll(20, 4)
might look like this:

A random solution:

def roll(n,s)
  (1..n).map { |x| "#{x}|#{'#' * (1 + rand(s))}" } * "\n"

The problem:

Provide a one-liner that can wrap a body of text at a requested maximum length.

The “Didn’t we do this before?” solution:

text.gsub(/(.{1,80})\s|(\w{80})/, "\\1\\2\n") 

The problem:

Sort an array of words by the words' values where a word's value is the
sum of the values of its letters and a letter's value is its position in
the alphabet. So the value of "Hello" is 8+5+12+12+15.

The “I mutated two solutions into one I like the bestest” solution:

class Array
  def sortval()
    sort_by { |x| x.upcase.sum - x.length * ?@ }

The problem:

Write a function per(n) which returns the periodicity of 1/n, i.e.
  per(3) => 1
  per(4) => 0
  per(7) => 6
  per(11) => 2

The “Ummm… yeah… how do these work?” solution:

def per(n, b=10)
  i=1;x=b;h={};loop {x=x%n*b;break 0 if x==0;h[x]?(break i-h[x]):h[x]=i; i+=1}

The problem:

Return the power set of an Array's elements.

The “Don’t get up, I’ll take care of it” solution:

class Array
  def powerset
    inject([[]]) { |a,e| a + a.map { |b| b+[e] } }

The problem:

Assuming you have an array of numeric data, write a method that returns an array of progressive
sums. That is:
  prog_sum( [1, 5, 13, -6, 20] ) =>  [1, 6, 19, 13, 33] 

The obligatory inject solution:

def prog_sum(ary)
  ary.inject([0, []]) {|(s, a), i| [s+i, a<<(s+i)]}.last

The problem:

Given an s-expression, print it out as a tree, where [:a,
:b, :c, :d] is the node with parent a and children b, c and d

[:foo, [:bar, [:baz, :quux], :hello, :world], :done] #=>

| -- bar
|    | -- baz
|    |    | -- quux
|    | -- hello
|    | -- world
| -- done

The last solution of the summary… solution:

class Array
  def to_s
    collect{|x| x.to_s.gsub(/\n/,"\n|   ")}.join("\n|-- ")

Any unsolved problems? You bet! Here are the problems that didn’t get answered, in case you’ve got an itch for more.

1. Given a text from STDIN and a regexp, print each line which (in part)
matches regexp with two preceding and two successional lines. Do not output
a line more than once.

2. Starting with an array, find the first permutation of the elements of
that array that is lexicographically greater than (i.e. sorts after)
the given array.

3. Write a oneliner next_fib(n) which gives the smallest Fibonacci number
greater than n.

4. Given an epsilon, compute PI to that precision.

Wednesday, February 04, 2009