# Re: Need a 40 LOC (ignoring comments) to be shorter -- suggestions wanted

**URL:** <https://www.ruby-forum.com/t/re-need-a-40-loc-ignoring-comments-to-be-shorter-suggestions-wanted/147767>\
**Category:** Ruby\
**Created:** [September 5, 2008, 8:46am UTC](https://www.ruby-forum.com/t/re-need-a-40-loc-ignoring-comments-to-be-shorter-suggestions-wanted/147767 "2008-09-05T08:46:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [September 5, 2008, 8:46am UTC](https://www.ruby-forum.com/t/re-need-a-40-loc-ignoring-comments-to-be-shorter-suggestions-wanted/147767/1 "2008-09-05T08:46:00Z")

</div>

Verbose and inefficient, but there was a 10 minute limit 🙂

class Gen  
include Enumerable  
def each  
(3…7).each do |c1|  
(3…7).each do |c2| next if [c1].include? c2  
(3…7).each do |c3| next if [c1,c2].include? c3  
(3…7).each do |c4| next if [c1,c2,c3].include? c4  
(3…7).each do |c5| next if [c1,c2,c3,c4].include? c5  
yield “#{c1}#{c2}#{c3}”.to\_i, “#{c4}#{c5}”.to\_i  
end  
end  
end  
end  
end  
end  
end

puts Gen.new.min { |(a,b),(c,d)| a_b \<=\> c_d }

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [September 5, 2008, 9:13am UTC](https://www.ruby-forum.com/t/re-need-a-40-loc-ignoring-comments-to-be-shorter-suggestions-wanted/147767/2 "2008-09-05T09:13:37Z")

</div>

Damn this is annoying 🙂

OK here’s another one, this time with a recursive permutation generator.  
It  
should be easier to modify to other variations of this problem. (However  
it  
currently doesn’t permute arrays with duplicate elements)

class Permutations  
include Enumerable

def initialize(src)  
@data = src  
end

def each(&blk)  
permute([], @data.to\_a, &blk)  
end

def permute(base, rest, &blk)  
if rest.empty?  
yield base  
else  
rest.each { |elem| permute(base + [elem], rest - [elem], &blk) }  
end  
end  
end

p Permutations.new(3…7).  
map { |data| [data[0,3].join.to\_i, data[3,2].join.to\_i] }.  
min { |(a,b),(c,d)| a_b \<=\> c_d }

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [September 5, 2008, 9:55am UTC](https://www.ruby-forum.com/t/re-need-a-40-loc-ignoring-comments-to-be-shorter-suggestions-wanted/147767/3 "2008-09-05T09:55:50Z")

</div>

My final offering, inspired by Park H.'s solution.

p (34567…76543).map { |x| [x/100 \* (x%100), x] if x.to\_s !~  
/[^3-7]|(.).\*\1/ }.compact.min

=\> [product, nnnmm]

Regards,

Brian.
