Calculating single-digit summands

Discussion in 'Ruby' started by draq, Dec 11, 2005.

  1. draq

    draq Guest

    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-part-i.html
     
    draq, Dec 11, 2005
    #1
    1. Advertising

  2. 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-part-i.html


    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
     
    James Edward Gray II, Dec 12, 2005
    #2
    1. Advertising

  3. draq

    Dan Diebolt Guest

    --0-1690795590-1134357246=:46771
    Content-Type: text/plain; charset=iso-8859-1
    Content-Transfer-Encoding: quoted-printable

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

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

    =09
    ---------------------------------
    Yahoo! Shopping
    Find Great Deals on Holiday Gifts at Yahoo! Shopping=20
    --0-1690795590-1134357246=:46771--
     
    Dan Diebolt, Dec 12, 2005
    #3
  4. 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
     
    James Edward Gray II, Dec 12, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,863
    darshana
    Oct 26, 2008
  2. Replies:
    2
    Views:
    976
    Martin Honnen
    Apr 12, 2008
  3. draq
    Replies:
    10
    Views:
    194
    Christer Nilsson
    Dec 13, 2005
  4. April
    Replies:
    16
    Views:
    205
    Ted Zlatanov
    Jul 7, 2008
  5. Thomas Andersson
    Replies:
    2
    Views:
    115
    Dr.Ruud
    Aug 14, 2010
Loading...

Share This Page