[QUIZ] Countdown (#7) [Solution]

Discussion in 'Ruby' started by email55555 email55555, Nov 16, 2004.

  1. Here is my solution.
    It is not elegant at all, slow, not OO ... ( I could make it better ... )
    Anyway, it does find the solution.

    $source = [75,2,8,5,10,10]
    $target = 926

    $expr = nil
    $result = nil
    $min_error = nil

    def show_solution
    puts("source => " + $source * ", ")
    puts("target => #$target")
    puts("solution => #$expr = #$result")
    end

    def test(result, expr)
    error = ($target - result).abs
    if (error == 0)
    $expr = expr
    $result = result
    show_solution
    exit # comment out this line if want all solutions
    elsif (error < $min_error)
    $min_error = error
    $expr = expr
    $result = result
    end
    end

    def eval_op(op, val1, val2, expr1, expr2, source, source_expr)
    result = val1.send(op, val2)
    expr = "(#{expr1} #{op} #{expr2})"
    test(result, expr)
    source << result; source_expr << expr
    find(source, source_expr)
    source.pop; source_expr.pop
    end

    def find(source, source_expr)
    return if (source.size <= 1)
    (0...source.size).each {|i|
    (0...source.size).each {|j|
    next if (i==j)

    if (i < j)
    b = source.slice!(j); a = source.slice!(i)
    b_expr = source_expr.slice!(j); a_expr = source_expr.slice!(i)
    else
    a = source.slice!(i); b = source.slice!(j)
    a_expr = source_expr.slice!(i); b_expr = source_expr.slice!(j)
    end

    if (b != 0)
    #else skip because a+0==a, a-0==a, a*0==0, a/0 ...

    if (i < j) && (a != 0)
    #else skip because (1) '+' is commutative (2) 0+b==0
    eval_op:)+, a, b, a_expr, b_expr, source, source_expr)
    end

    eval_op:)-, a, b, a_expr, b_expr, source, source_expr)

    if (i < j) && (a != 0) && (a != 1) && (b != 1)
    #else skip because (1) '*' is commutative (2) 0*b==0 (3) n*1==n
    eval_op:)*, a, b, a_expr, b_expr, source, source_expr)
    end

    if (a != 0) && (b != 1) # else skip because (1) 0/b==0 (2) n/1==n
    eval_op:)/, a.to_f, b.to_f, a_expr, b_expr, source, source_expr)
    end
    end

    if (i < j)
    source[i...i] = a; source[j...j] = b
    source_expr[i...i] = a_expr; source_expr[j...j] = b_expr
    else
    source[j...j] = b; source[i...i] = a
    source_expr[j...j] = b_expr; source_expr[i...i] = a_expr
    end
    }
    }
    end

    $min_error = ($source[0] - $target).abs
    $expr = $source[0].to_s
    if ($min_error == 0)
    show_solution
    exit
    end

    (1...$source.size).each { |i| test($source, $source.to_s) }
    find($source.dup, $source.map{|e| e.to_s})
    show_solution
    email55555 email55555, Nov 16, 2004
    #1
    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. Ruby Quiz

    [QUIZ] Countdown (#7)

    Ruby Quiz, Nov 12, 2004, in forum: Ruby
    Replies:
    24
    Views:
    311
    James Edward Gray II
    Nov 14, 2004
  2. Dennis Ranke

    [SOLUTION] Countdown (#7)

    Dennis Ranke, Nov 14, 2004, in forum: Ruby
    Replies:
    12
    Views:
    292
    James Edward Gray II
    Nov 19, 2004
  3. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    16
    Views:
    281
    David Tran
    Jan 10, 2005
  4. David Tran
    Replies:
    9
    Views:
    187
    David Tran
    Jan 21, 2005
  5. MenTaLguY
    Replies:
    12
    Views:
    282
    MenTaLguY
    Jun 7, 2007
Loading...

Share This Page