# \[SOLUTION\] One-Liners (#113)

**URL:** <https://rubytalk.org/t/solution-one-liners-113/35261>\
**Category:** ruby-talk\
**Created:** [11 February 2007 14:35 UTC](https://rubytalk.org/t/solution-one-liners-113/35261 "2007-02-11T14:35:09Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [11 February 2007 14:35 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/1 "2007-02-11T14:35:09Z")

</div>

Here are my solutions to Quiz #113. For some of them I just couldn't  
help but to provide a couple variations.

Because I'm a terrible golfer, most strive for elegance (in some form)  
over terseness.

# 1 - Commafy Numerics  
i,f=quiz.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
('.'+f) : '')

# 2 - Flatten\_Once  
a=[]; quiz.each{ |x| Array===x ? a.concat(x) : a\<\<x }; a

# 3 - Shuffle Array  
quiz.sort\_by{ rand }

# 4 - Resolve class (and other constants) from string  
quiz.split( '::' ).inject( Module ){ |r,o| r.const\_get(o) }  
#...or, by cheating  
eval(quiz)

#5 - Paragraph Wrapping - extra work to not put a new line on the last  
line  
quiz.gsub( /^(.{1,40})($|[\t]+)/ ){ $2.empty? ? $1 : "#{$1}\n" }

#6 - Anagrams - assuming that the original word shouldn't be in the  
output...  
a=[]; r=quiz.shift.split('').sort; quiz.each{|w|a\<\<w if  
w.split('').sort==r}; a  
#...or, if the original word should be included  
a=[]; r=quiz[0].split('').sort; quiz.each{ |w| a\<\<w if  
w.split('').sort==r }; a

#7 - String to Binary String, the geeky way  
o=''; quiz.each\_byte{|b| o \<\< ( b==32 ? "\n" : ('%b' % b) ) }; o  
#...or slightly more 'rubyish'...  
quiz.split(' ').map{|s| o=''; s.each\_byte{|b| o \<\< b.to\_s(2) };  
o }.join("\n")  
#...but what's more rubyish than nested #maps and pulling bytes from  
strings? 😉  
quiz.split(' ').map{|s| s.scan(/./).map{|c| '%b' %  
c[0] }.join }.join("\n")

# By the way, I have to say that if the Think Geek t-shirts are really  
in the  
# form provided, they are terrible #representations of geekiness. What  
geek  
# would strip the leading zeros from the bits in a byte? I'd replace  
"%b" with  
# "%08b" above for a better representation (and use it instead of  
to\_s(2)).

#8 - Random line from file - if you run out of memory, go buy more  
RAM 😉  
all=quiz.readlines; all[rand(all.length)]

#9 - Wondrous number path  
a=[n=quiz]; while n\>1; a \<\< ( n%2==0 ? n/=2 : n=(n\*3)+1 ); end; a

#10 - Array to Nested Hash, direct indexing...  
a=quiz; h={a[-2]=\>a[-1]}; (a.size-3).downto(0){ |i| h={a[i]=\>h} }; h  
#...or a slightly different way...  
a=quiz; y,z=a[-2..-1]; h={y=\>z}; a[0..-3].reverse.each{ |o|  
h={o=\>h} }; h  
#...or poppin' values for a tighter format...  
a=quiz; z,y=a.pop,a.pop; h={y=\>z}; a.reverse.each{ |o| h={o=\>h} }; h  
#...and one last, just because I love Hash.[]  
a=quiz.reverse; h=Hash[a.shift,a.shift].invert; a.each{ |o|  
h={o=\>h} }; h

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [11 February 2007 14:45 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/2 "2007-02-11T14:45:08Z")

</div>

Bah, of course the 80-char lines wrapped and got confusing with my  
comments. Here's my solution again, with syntax highlighting:  
[http://pastie.caboo.se/39500](http://pastie.caboo.se/39500)

And here is the actual code I originally wrote to test most of the  
solutions:  
[http://pastie.caboo.se/39499](http://pastie.caboo.se/39499)

> **···**
>
> On Feb 11, 7:30 am, "Phrogz" \<g...@refinery.com\> wrote:
> 
> > Here are my solutions to Quiz #113. For some of them I just couldn't  
> > help but to provide a couple variations.

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [11 February 2007 19:20 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/3 "2007-02-11T19:20:08Z")

</div>

> Here are my solutions to Quiz #113. For some of them I just couldn't  
> help but to provide a couple variations.
> 
> Because I'm a terrible golfer, most strive for elegance (in some form)  
> over terseness.
> 
> # 1 - Commafy Numerics  
> i,f=quiz.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
> ('.'+f) : '')

Good answer. you helped me golf mine down a little bit by getting rid of  
array indexing:

i,f=i.to\_s.split('.');"#{i.reverse.scan(/.{1,3}/).join(',').reverse}.#{f}"

> # 3 - Shuffle Array  
> quiz.sort\_by{ rand }

I feel stupid for taking such a longer answer. And I've used sort\_by{rand}  
too to do this, if not in Ruby then in SQL, so I feel stupid for not  
thinking of it in this context.]

mine, as I posted elsewhere was:  
i.inject(){|cur,val| cur.insert(rand(cur.length+1),val)}

> #5 - Paragraph Wrapping - extra work to not put a new line on the last  
> line  
> quiz.gsub( /^(.{1,40})($|[\t]+)/ ){ $2.empty? ? $1 : "#{$1}\n" }

I like this one a lot. My only answer was way too long, and far more  
complicated.

> **···**
>
> On Sun, 11 Feb 2007 06:30:52 -0800, Phrogz wrote:
> 
> --  
> Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [11 February 2007 23:40 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/4 "2007-02-11T23:40:57Z")

</div>

When I built the quiz, I used the following solutions to reality-check myself. (Making sure I could find a viable answer.)

> **···**
>
> On Feb 11, 2007, at 8:35 AM, Phrogz wrote:
> 
> > Here are my solutions to Quiz #113.
> 
> #  
> # 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".  
> #  
> quiz.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\\1,").reverse
> 
> #  
> # 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(Array.new) { |arr, a| arr.push(\*a) }
> 
> # Shuffle the contents of a provided Array.  
> quiz.sort\_by { rand }
> 
> #  
> # Given a Ruby class name in String form (like  
> # "GhostWheel::Expression::LookAhead"), fetch the actual class object.  
> #  
> quiz.split("::").inject(Object) { |par, const| par.const\_get(const) }
> 
> #  
> # Insert newlines into a paragraph of prose (provided in a String) so lines will  
> # wrap at 40 characters.  
> #  
> quiz.gsub!(/(.{1,40}|\S{41,})(?: +|$\n?)/, "\\1\n")
> 
> #  
> # 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 { |w| w.split("").sort == quiz.first.split("").sort }
> 
> #  
> # 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 { |c| c == " " ? "\n" : c[0].to\_s(2) }.join
> 
> # Provided with an open File object, select a random line of content.  
> quiz.inject { |choice, line| rand \< 1/quiz.lineno.to\_f ? line : choice }
> 
> #  
> # 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].  
> #  
> Hash.new { |h, n| n == 1 ? [1] : [n] + h[n % 2 == 0 ? n/2 : n\*3+1] }[quiz]
> 
> #  
> # Convert an Array of objects to nested Hashes such that %w[one two three four  
> # five] becomes {"one" =\> {"two" =\> {"three" =\> {"four" =\> "five"}}}}.  
> #  
> quiz.reverse.inject { |res, wrap| {wrap =\> res} }
> 
> James Edward Gray II

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [11 February 2007 21:10 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/5 "2007-02-11T21:10:09Z")

</div>

Another answer, based on yours, this one is exactly one expression (no  
semicolons):  
quiz.to\_s.gsub(/(\d)(?=\d{3}+#{quiz.to\_s=~/\./?/\./:/$/})/,'\\1,')

> **···**
>
> On Sun, 11 Feb 2007 19:15:44 +0000, Ken Bloom wrote:
> 
> > On Sun, 11 Feb 2007 06:30:52 -0800, Phrogz wrote:
> > 
> > > Here are my solutions to Quiz #113. For some of them I just couldn't  
> > > help but to provide a couple variations.
> > > 
> > > Because I'm a terrible golfer, most strive for elegance (in some form)  
> > > over terseness.
> > > 
> > > # 1 - Commafy Numerics  
> > > i,f=quiz.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
> > > ('.'+f) : '')
> > 
> > Good answer. you helped me golf mine down a little bit by getting rid of  
> > array indexing:
> > 
> > i,f=i.to\_s.split('.');"#{i.reverse.scan(/.{1,3}/).join(',').reverse}.#{f}"
> 
> --  
> Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [12 February 2007 06:00 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/6 "2007-02-12T06:00:55Z")

</div>

What I particularly admire about your solutions is that they are all  
single- or chained-statements. None of the "a=;...;a" nonsense that  
I mucked about with. I wish I had been sure that it was possible to do  
this for all solutions, so that I would have looked harder.

I can't tell if I think the reverse/regexp/reverse technique you (and  
many others) used for the first problem is more or less elegant than a  
single regexp on the integer portion. I suspect that mine is faster at  
runtime...but speed is rarely an appropriate measure of elegance.

Kudos on the memoizing wondrous number, btw. 🙂

> **···**
>
> On Feb 11, 4:40 pm, James Edward Gray II \<j...@grayproductions.net\> wrote:
> 
> > quiz.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\\1,").reverse  
> > quiz.inject(Array.new) { |arr, a| arr.push(\*a) }  
> > quiz.sort\_by { rand }  
> > quiz.split("::").inject(Object) { |par, const| par.const\_get(const) }  
> > quiz.gsub!(/(.{1,40}|\S{41,})(?: +|$\n?)/, "\\1\n")  
> > quiz.select { |w| w.split("").sort == quiz.first.split("").sort }  
> > quiz.split("").map { |c| c == " " ? "\n" : c[0].to\_s(2) }.join  
> > quiz.inject { |choice, line| rand \< 1/quiz.lineno.to\_f ? line : choice }  
> > Hash.new{|h, n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] }[quiz]  
> > quiz.reverse.inject { |res, wrap| {wrap =\> res} }

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [12 February 2007 09:49 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/7 "2007-02-12T09:49:33Z")

</div>

This quiz - as I tried to say with the wrong verb in my solution - is a  
great teacher.  
Well your solutions are, may I unpost mine, they feel clumsy now ;)?  
Really nice. especially the commas and hash builder I am impressed.

Robert

> **···**
>
> On 2/12/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Feb 11, 2007, at 8:35 AM, Phrogz wrote:
> > 
> > \> Here are my solutions to Quiz #113.
> 
> --  
> 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

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [12 February 2007 12:23 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/8 "2007-02-12T12:23:52Z")

</div>

> When I built the quiz, I used the following solutions to reality-  
> check myself. (Making sure I could find a viable answer.)  
> \<snip\>

quiz.inject(Array.new) { |arr, a| arr.push(\*a) }

> \<snip\>  
> #  
> # Convert an Array of objects to nested Hashes such that %w[one two  
> three four  
> # five] becomes {"one" =\> {"two" =\> {"three" =\> {"four" =\> "five"}}}}.  
> #  
> quiz.reverse.inject { |res, wrap| {wrap =\> res} }
> 
> James Edward Gray II

Applying your genius to your genius we get  
for the flat\_flatten problem

quiz.inject{|arr,ele| [\*arr].push(\*ele)}

maybe you prefer your original solution but I feel the second one is just  
great too.

Cheers  
Robert

> **···**
>
> On 2/12/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> --  
> 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

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [12 February 2007 17:29 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/9 "2007-02-12T17:29:55Z")

</div>

> What I particularly admire about your solutions is that they are all  
> single- or chained-statements.

Thank you.

> I can't tell if I think the reverse/regexp/reverse technique you (and  
> many others) used for the first problem is more or less elegant than a  
> single regexp on the integer portion. I suspect that mine is faster at  
> runtime...but speed is rarely an appropriate measure of elegance.

I'm pretty sure I learned that reverse(), gsub(), and reverse() trick from Perl's FAQ years ago. I just checked now though and the answer is not what I recall, so maybe I am misremembering that.

> Kudos on the memoizing wondrous number, btw. 🙂

It's not actually. I never assign the Hash value. 😉

James Edward Gray II

> **···**
>
> On Feb 12, 2007, at 12:00 AM, Phrogz wrote:

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [12 February 2007 18:00 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/10 "2007-02-12T18:00:07Z")

</div>

Interesting point. (And, of course, it wouldn't be useful if it  
memoized the result for a single call to the function.) I think it's  
interesting because this pattern really allows you to (ab)use the  
block form of Hash as a lambda that passes a reference to itself as  
one of its arguments. Very convenient for one-liners.

> **···**
>
> On Feb 12, 10:29 am, James Edward Gray II \<j...@grayproductions.net\> wrote:
> 
> > On Feb 12, 2007, at 12:00 AM, Phrogz wrote:  
> > \> \> Hash.new{|h, n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] }[quiz]  
> > \> Kudos on the memoizing wondrous number, btw. 🙂
> > 
> > It's not actually. I never assign the Hash value. 😉

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [12 February 2007 18:12 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/11 "2007-02-12T18:12:18Z")

</div>

> \<snip\>
> 
> \> I can't tell if I think the reverse/regexp/reverse technique you (and  
> \> many others) used for the first problem is more or less elegant than a  
> \> single regexp on the integer portion. I suspect that mine is faster at  
> \> runtime...but speed is rarely an appropriate measure of elegance.

Now I just could benchmark this and be smart afterwards, but I love to take  
chances.  
I guess your positive lookahead assertion will be slower than James'  
reverse\*\*2 technique.  
I'll benchmark it right now...  
BTW I prefer reverse \*\* 2 because I did not figure out the lookahead  
solution 😉

Robert  
\<snip\>

> **···**
>
> On 2/12/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Feb 12, 2007, at 12:00 AM, Phrogz wrote:
> 
> --  
> 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

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [13 February 2007 17:10 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/12 "2007-02-13T17:10:05Z")

</div>

> > > Hash.new{|h, n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] }[quiz]
> > 
> > Kudos on the memoizing wondrous number, btw. 🙂
> 
> It's not actually. I never assign the Hash value. 😉

So it's equivalent to:  
(h=lambda {|n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] })[23]

--Ken

> **···**
>
> --  
> Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [12 February 2007 18:26 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/13 "2007-02-12T18:26:07Z")

</div>

here goes the benchmark ( I was right, too bad you'll never know if I  
cheated;)

the difference is only small. I think that I was wrong 'cause I really  
thought that look ahead will be a killer for backtracking purpose (but I  
guess it will backtrack about 1.5 times on average). Yet the reverse\*\*2  
seems a little bit faster but no need to chose one of the two algorithms for  
speed 🙂

robert@PC:~/log/ruby/quiz/113\_bis 19:20:41  
521/21 \> cat bench.rb  
&nbsp;&nbsp;require 'benchmark'

&nbsp;&nbsp;&nbsp;&nbsp;array = (1..100000).map { rand \* (ARGV.first||1\_000\_000).to\_f }

def phrogz quiz  
&nbsp;&nbsp;i,f=quiz.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
('.'+f) : '')  
end  
def james quiz  
&nbsp;&nbsp;quiz.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\\1,").reverse  
end

&nbsp;&nbsp;&nbsp;&nbsp;Benchmark.bmbm do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.report("Phrogz") {array.map{ |e| phrogz(e) }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.report("James") {array.map{ |e| james(e) }}  
&nbsp;&nbsp;&nbsp;&nbsp;end

robert@PC:~/log/ruby/quiz/113\_bis 19:21:07  
522/22 \> ruby bench.rb 1\_000\_000  
Rehearsal ------------------------------------------  
Phrogz 7.140000 0.000000 7.140000 ( 7.148000)  
James 6.422000 0.000000 6.422000 ( 6.452000)  
-------------------------------- total: 13.562000sec

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
Phrogz 7.062000 0.000000 7.062000 ( 7.056000)  
James 6.359000 0.000000 6.359000 ( 6.385000)  
robert@PC:~/log/ruby/quiz/113\_bis 19:21:47  
523/23 \> ruby bench.rb 1\_000\_000\_000  
Rehearsal ------------------------------------------  
Phrogz 7.609000 0.000000 7.609000 ( 7.614000)  
James 6.312000 0.000000 6.312000 ( 6.308000)  
-------------------------------- total: 13.921000sec

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
Phrogz 7.484000 0.000000 7.484000 ( 7.506000)  
James 6.219000 0.000000 6.219000 ( 6.268000)

Cheers  
Robert

> **···**
>
> On 2/12/07, Robert Dober \<[robert.dober@gmail.com](mailto:robert.dober@gmail.com)\> wrote:
> 
> --  
> 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

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [13 February 2007 17:41 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/14 "2007-02-13T17:41:35Z")

</div>

Exactly.

James Edward Gray II

> **···**
>
> On Feb 13, 2007, at 11:10 AM, Ken Bloom wrote:
> 
> > > > > Hash.new{|h, n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] }[quiz]
> > > > 
> > > > Kudos on the memoizing wondrous number, btw. 🙂
> > > 
> > > It's not actually. I never assign the Hash value. 😉
> > 
> > So it's equivalent to:  
> > (h=lambda {|n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] })[23]

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [12 February 2007 19:04 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/15 "2007-02-12T19:04:12Z")

</div>

How does this code work? You pass arguments to methods we don't see. The ones we do see don't even accept arguments.

The results look right though:

#!/usr/bin/env ruby -w

require "benchmark"

def phrogz(num)  
&nbsp;&nbsp;&nbsp;i,f=num.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ? ('.'+f) : '')  
end

def james(num)  
&nbsp;&nbsp;&nbsp;num.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\\1,").reverse  
end

TESTS = Array.new(100\_000) { rand(1\_000\_000) + 1.to\_f / (rand(1\_000) + 1) }  
Benchmark.bmbm do |results|  
&nbsp;&nbsp;&nbsp;results.report("Phrogz:") { TESTS.each { |n| phrogz(n) } }  
&nbsp;&nbsp;&nbsp;results.report("James:") { TESTS.each { |n| james(n) } }  
end  
# \>\> Rehearsal -------------------------------------------  
# \>\> Phrogz: 1.690000 0.010000 1.700000 ( 1.700985)  
# \>\> James: 1.550000 0.010000 1.560000 ( 1.557882)  
# \>\> ---------------------------------- total: 3.260000sec  
# \>\>  
# \>\> user system total real  
# \>\> Phrogz: 1.690000 0.010000 1.700000 ( 1.703213)  
# \>\> James: 1.520000 0.000000 1.520000 ( 1.528621)

\_\_END\_\_

James Edward Gray II

> **···**
>
> On Feb 12, 2007, at 12:26 PM, Robert Dober wrote:
> 
> > 521/21 \> cat bench.rb  
> > require 'benchmark'
> > 
> > &nbsp;&nbsp;&nbsp;array = (1..100000).map { rand \* (ARGV.first||1\_000\_000).to\_f }
> > 
> > def phrogz quiz  
> > i,f=quiz.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
> > ('.'+f) : '')  
> > end  
> > def james quiz  
> > quiz.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\\1,").reverse  
> > end
> > 
> > &nbsp;&nbsp;&nbsp;Benchmark.bmbm do |x|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.report("Phrogz") {array.map{ |e| phrogz(e) }}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.report("James") {array.map{ |e| james(e) }}  
> > &nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [13 February 2007 17:56 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/16 "2007-02-13T17:56:11Z")

</div>

That really puts the spotlight on it, this will go away in 2.0 though if  
I understand correctly 🙂 and one would need to write:

(h=lambda {|n,lmb| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1,lmb] })[23,h]

is that right?

Robert

> **···**
>
> On 2/13/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Feb 13, 2007, at 11:10 AM, Ken Bloom wrote:
> > 
> > \>\>\>\> Hash.new{|h, n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] }  
> > \>\>\>\> [quiz]  
> > \>\>\> Kudos on the memoizing wondrous number, btw. 🙂  
> > \>\> It's not actually. I never assign the Hash value. 😉  
> > \>  
> > \> So it's equivalent to:  
> > \> (h=lambda {|n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] })[23]
> > 
> > Exactly.
> > 
> > James Edward Gray II
> 
> --  
> 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

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [12 February 2007 21:38 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/17 "2007-02-12T21:38:51Z")

</div>

> \> 521/21 \> cat bench.rb  
> \> require 'benchmark'  
> \>  
> \> array = (1..100000).map { rand \* (ARGV.first||1\_000\_000).to\_f }  
> \>  
> \> def phrogz quiz  
> \> i,f=quiz.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
> \> ('.'+f) : '')  
> \> end  
> \> def james quiz  
> \> quiz.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\\1,").reverse  
> \> end  
> \>  
> \> Benchmark.bmbm do |x|  
> \> x.report("Phrogz") {array.map{ |e| phrogz(e) }}  
> \> x.report("James") {array.map{ |e| james(e) }}  
> \> end
> 
> How does this code work? You pass arguments to methods we don't  
> see. The ones we do see don't even accept arguments.
> 
> The results look right though:

I am a magician 😉  
well maybe not, I do not really understand, I have had the impression before  
that gmail does not work correctly all the time  
nahh it's probably just me I just steal any second on the list from my work  
as sysadmin and IP/Sec officer and maybe I should not ☹

Nevertheless, I can see the complete code with the two methods in your  
reply to the post!  
Anyway here goes the definition of the two methods(again).

def phrogz quiz

> i,f=quiz.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
> ('.'+f) : '')  
> end  
> def james quiz  
> quiz.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\
> 
> \1,").reverse  
> \> end

and again?

#!/usr/bin/env ruby -w

> require "benchmark"
> 
> def phrogz(num)  
> &nbsp;&nbsp;&nbsp;i,f=num.to\_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
> ('.'+f) : '')  
> end
> 
> def james(num)  
> &nbsp;&nbsp;&nbsp;num.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,"\\1,").reverse  
> end
> 
> TESTS = Array.new(100\_000) { rand(1\_000\_000) + 1.to\_f / (rand(1\_000)  
> + 1) }  
> Benchmark.bmbm do |results|  
> &nbsp;&nbsp;&nbsp;results.report("Phrogz:") { TESTS.each { |n| phrogz(n) } }  
> &nbsp;&nbsp;&nbsp;results.report("James:") { TESTS.each { |n| james(n) } }  
> end  
> # \>\> Rehearsal -------------------------------------------  
> # \>\> Phrogz: 1.690000 0.010000 1.700000 ( 1.700985)  
> # \>\> James: 1.550000 0.010000 1.560000 ( 1.557882)  
> # \>\> ---------------------------------- total: 3.260000sec  
> # \>\>  
> # \>\> user system total real  
> # \>\> Phrogz: 1.690000 0.010000 1.700000 ( 1.703213)  
> # \>\> James: 1.520000 0.000000 1.520000 ( 1.528621)
> 
> \_\_END\_\_
> 
> James Edward Gray II
> 
> Robert

I will change my signature soon, maybe it is its fault 🙂

> **···**
>
> On 2/12/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Feb 12, 2007, at 12:26 PM, Robert Dober wrote:
> 
> --  
> 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

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [13 February 2007 18:04 UTC](https://rubytalk.org/t/solution-one-liners-113/35261/18 "2007-02-13T18:04:40Z")

</div>

Hmm, I wasn't aware of this. What makes you say that?

James Edward Gray II

> **···**
>
> On Feb 13, 2007, at 11:56 AM, Robert Dober wrote:
> 
> > On 2/13/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> > 
> > > On Feb 13, 2007, at 11:10 AM, Ken Bloom wrote:
> > > 
> > > \>\>\>\> Hash.new{|h, n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] }  
> > > \>\>\>\> [quiz]  
> > > \>\>\> Kudos on the memoizing wondrous number, btw. 🙂  
> > > \>\> It's not actually. I never assign the Hash value. 😉  
> > > \>  
> > > \> So it's equivalent to:  
> > > \> (h=lambda {|n| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1] })[23]
> > > 
> > > Exactly.
> > > 
> > > James Edward Gray II
> > 
> > That really puts the spotlight on it, this will go away in 2.0 though if  
> > I understand correctly 🙂 and one would need to write:
> > 
> > (h=lambda {|n,lmb| n==1 ? [1] : [n] + h[n%2 == 0 ? n/2 : n\*3+1,lmb] })[23,h]
> > 
> > is that right?
