From: Bill Kelly Date: 2008-09-20T11:32:42+09:00 Subject: Re: [QUIZ] One-Liners Mashup (#177 again) From: "Matthew Moss" > > Ready? Here goes. First problem... > You should know this pattern well: > > > [:one, "two", 4] * 3 > => [:one, "two", 4, :one, "two", 4, :one, "two", 4] > > Write a single line method on Array that does this instead: > > > [:one, "two", 4].repeat(3) > => [:one, :one, :one, "two", "two", "two", 4, 4, 4] # SOLUTION 1: # Partial solution, we would need a flatten(1) to prevent it from # messing up nested arrays like [:one, "two", [3]] -- since flatten # unarrays recursively. class Array; def repeat(n); ([self]*n).transpose.flatten; end; end # SOLUTION 2: # Avoids flatten, so won't break nested arrays: class Array; def repeat(n); ([self]*n).transpose.inject([]){|a,e| a += e}; end; end -------------- NEW CHALLENGE: -------------- # Given one or more input filenames on the command line, # report the number of unique IP addresses found in all the # data. # # (For our purposes, an IP address may simply be considered # four integerers separated by dots, e.g.: 6.54.123.9 ) # # Optionally, the solution should read stdin if no filenames # were specified. # # Preferably, the solution should be expressed in the form of # a ruby command-line invocation. (Optional.) Regards, Bill