[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calculating single-digit summands

draq

12/11/2005 9:53:00 PM

I have tried to make an algorithm that finds all possible combinations
of single-digit summands for a number. After an afternoon of hard and
desperate work I got something inefficient (although it works). Maybe
someone has a better idea. The link to the algorithm:
http://secam.blogspot.com/2005/12/solving-kakuro-p...

4 Answers

Ronald E Jeffries

12/12/2005 12:20:00 AM

0

On 11 Dec 2005 13:53:28 -0800, "draq" <boyang.xia@gmail.com> wrote:

>I have tried to make an algorithm that finds all possible combinations
>of single-digit summands for a number. After an afternoon of hard and
>desperate work I got something inefficient (although it works). Maybe
>someone has a better idea. The link to the algorithm:
>http://secam.blogspot.com/2005/12/solving-kakuro-p...

I don't understand the rules of the game from your writeup.

--
Ron Jeffries
www.XProgramming.com
I'm giving the best advice I have. You get to decide if it's true for you.

James Gray

12/12/2005 2:50:00 AM

0

On Dec 11, 2005, at 3:57 PM, draq wrote:

> I have tried to make an algorithm that finds all possible combinations
> of single-digit summands for a number. After an afternoon of hard and
> desperate work I got something inefficient (although it works). Maybe
> someone has a better idea. The link to the algorithm:
> http://secam.blogspot.com/2005/12/solving-kakuro-p...

One thing that your code could really use is better names. Variables
like "arr", "t", and even "calc" tell me very little, as I'm trying
to read you code.

Next, there's a lot more iterators than just each() and using them
can make your code more expressive. Some examples:

def sum (arr)
i = 0
arr.each do |k| i += k end
i
end

# ... or ...

def sum( enum )
inject { |sum, n| sum + n }
end

######

arrj.each do |a|
arri.delete(a) if sum(a) != number
end

# ... or ...

arrj.delete_if { |a| sum(a) != number }

You can also simplify lines like the following by using destructive
method calls:

arri = arri.uniq

# ... or ...

arri.uniq!

Hope that gives you some ideas.

James Edward Gray II



Dan Diebolt

12/12/2005 3:14:00 AM

0

># ... or ...
>def sum( enum )
> inject { |sum, n| sum + n }
>end

Don't you need enum. ?

def sum(enum)
enum.inject {|sum,n| sum+n}
end


---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping

James Gray

12/12/2005 3:19:00 AM

0

On Dec 11, 2005, at 9:14 PM, Dan Diebolt wrote:

>> # ... or ...
>> def sum( enum )
>> inject { |sum, n| sum + n }
>> end
>
> Don't you need enum. ?
>
> def sum(enum)
> enum.inject {|sum,n| sum+n}
> end

Oops. Yes. Thank you.

James Edward Gray II