From: Robert Dober Date: 2007-02-12T01:21:24+09:00 Subject: Re: [QUIZ] One-Liners (#113) ------=_Part_55079_3075190.1171210881380 Content-Type: multipart/alternative; boundary="----=_Part_55080_10905154.1171210881380" ------=_Part_55080_10905154.1171210881380 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I guess it is time now ;) This was a great quiz and it thought me that I did not *know anything about Regexps*. Now I know a little bit: I also got acquainted with an old enemy of mine, #inject :) # Given an Array of String words, build an Array of only those words that are # anagrams of the first word in the Array. quiz.select{ |ele| ele.split("").sort == quiz.first.split("").sort } # I left the first word in the list one could discard it within the 80 chars limit be deleting WS in my solution. # Given a Ruby class name in String form (like # "GhostWheel::Expression::LookAhead"), fetch the actual class object. # x=quiz.split("::").inject(self.class){|k,m|k.const_get(m)};Class===x ? x : nil # * Given a Numeric, provide a String representation with commas inserted between # each set of three digits in front of the decimal. For example, 1999995.99 # should become "1,999,995.99". m=/[^\.]*/.match(quiz.to_s );p=$';m[0].reverse.gsub(/\d{3}\B/,'\&,').reverse+p # I could not believe how tough this one was for me!!!! # * Given a nested Array of Arrays, perform a flatten()-like operation that # removes only the top level of nesting. For example, [1, [2, [3]]] would become # [1, 2, [3]]. # quiz.inject( [] ) { |a,e| a + ( Array === e ? e : [e] ) } # Convert a ThinkGeek t-shirt slogan (in String form) into a binary # representation (still a String). For example, the popular shirt "you are dumb" # is actually printed as: # # 111100111011111110101 # 110000111100101100101 # 1100100111010111011011100010 # quiz.split.map{ |w| s=""; w.each_byte{ |b| s< {"two" => {"three" => {"four" => "five"}}}}. quiz[0..-3].reverse.inject( { quiz[-2] => quiz[-1] } ){ |h,e| { e => h } } # I found a fitting solution with a recursive method in 80 chars but eventually # I was saved by #inject from ridiculing myself. # Provided with an open File object, select a random line of content. q=quiz.readlines; q[ rand( q.size ) ].chomp # * Shuffle the contents of a provided Array. # quiz.sort_by{ rand } # This was a great Quiz but this problem was somehow an anticlimax for me, did I miss something? # Given a wondrous number Integer, produce the sequence (in an Array). A # wondrous number is a number that eventually reaches one, if you apply the # following rules to build a sequence from it. If the current number in the # sequence is even, the next number is that number divided by two. When the # current number is odd, multiply that number by three and add one to get the next # number in the sequence. Therefore, if we start with the wondrous number 15, the # sequence is [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, # 1]. q=quiz; x=[q]; until x[-1]==1 do x << ( q= q%2==0 ? q/2 : 3 * q + 1 ); end; x # Maybe not very challenging but a good read on Google:) # Insert newlines into a paragraph of prose (provided in a String) so lines will # wrap at 40 characters. # quiz.gsub(/(.{1,40})(\b|\z)/){$1+"\n"}.gsub(/\s*\n\s*/,"\n").chomp # tougher than I thought I am not sure about all edge cases I attached a test suite and the solutions Cheers Robert -- We have not succeeded in answering all of our questions. In fact, in some ways, we are more confused than ever. But we feel we are confused on a higher level and about more important things. -Anonymous ------=_Part_55080_10905154.1171210881380 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline

I guess it is time now ;)


This was a great quiz and it thought me that I did not *know anything about Regexps*.
Now I know a little bit:
I also got acquainted with an old enemy of mine, #inject :)


# Given an Array of String words, build an Array of only those words that are
# anagrams of the first word in the Array.
quiz.select{ |ele| ele.split("").sort == quiz.first.split("").sort }
# I left the first word in the list one could discard it within the 80 chars limit be deleting WS in my solution.

# Given a Ruby class name in String form (like
# "GhostWheel::Expression::LookAhead"), fetch the actual class object.
#
x=quiz.split("::").inject(self.class){|k,m|k.const_get(m)};Class===x ? x : nil

# * Given a Numeric, provide a String representation with commas inserted between
# each set of three digits in front of the decimal.  For example, 1999995.99
# should become "1,999,995.99".
m=/[^\.]*/.match(quiz.to_s);p=$';m[0].reverse.gsub(/\d{3}\B/,'\&,').reverse+p
# I could not believe how tough this one was for me!!!!

# * Given a nested Array of Arrays, perform a flatten()-like operation that
# removes only the top level of nesting.  For example, [1, [2, [3]]] would become
# [1, 2, [3]].
#
quiz.inject( [] ) { |a,e| a + ( Array === e ? e : [e] ) }

# Convert a ThinkGeek t-shirt slogan (in String form) into a binary
# representation (still a String).  For example, the popular shirt "you are dumb"
# is actually printed as:
#
#        111100111011111110101
#        110000111100101100101
#        1100100111010111011011100010
#
quiz.split.map{ |w| s=""; w.each_byte{ |b| s<<b.to_s(2)}; s}.join("\n")

# * Convert an Array of objects to nested Hashes such that %w[one two three four
# five] becomes {"one" => {"two" => {"three" => {"four" => "five"}}}}.
quiz[0..-3].reverse.inject( { quiz[-2] => quiz[-1] } ){ |h,e| { e => h } }
# I found a fitting solution with a recursive method in 80 chars but eventually
# I was saved by #inject from ridiculing myself.

# Provided with an open File object, select a random line of content.
q=quiz.readlines; q[ rand( q.size ) ].chomp

# * Shuffle the contents of a provided Array.
#
quiz.sort_by{ rand }
# This was a great Quiz but this problem was somehow an anticlimax for me, did I miss something?

# Given a wondrous number Integer, produce the sequence (in an Array).  A
# wondrous number is a number that eventually reaches one, if you apply the
# following rules to build a sequence from it.  If the current number in the
# sequence is even, the next number is that number divided by two.  When the
# current number is odd, multiply that number by three and add one to get the next
# number in the sequence.  Therefore, if we start with the wondrous number 15, the
# sequence is [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,
# 1].
q=quiz; x=[q]; until x[-1]==1 do x << ( q= q%2==0 ? q/2 : 3 * q + 1 ); end; x
# Maybe not  very challenging but a good read on Google:)

# Insert newlines into a paragraph of prose (provided in a String) so lines will
# wrap at 40 characters.
#
quiz.gsub(/(.{1,40})(\b|\z)/){$1+"\n"}.gsub(/\s*\n\s*/,"\n").chomp
# tougher than I thought I am not sure about all edge cases

I attached a test suite and the solutions

Cheers
Robert
--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous ------=_Part_55080_10905154.1171210881380-- ------=_Part_55079_3075190.1171210881380 Content-Type: application/x-gzip; name="sol113.tgz" Content-Transfer-Encoding: base64 Content-Disposition: attachment; fil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