Fixnum 在 Ruby 中被视为数组

发布于 2024-12-15 15:34:49 字数 1183 浏览 0 评论 0原文

我是 Ruby 新手,我正在尝试维基百科中给出的 合并排序算法

我得到在 merge 方法中比较左右数组的第一个元素时出现“Comparison of Fixnum with Array failed (ArgumentError)”失败错误。可能是什么原因以及如何解决这个问题?谢谢 :)

def mergeSort(array)
    if array.length == 1
        return array
    end

    middle = array.length/2 - 1
    left = array[0..middle]
    right = array[middle+1..array.length-1]

    left = mergeSort(left)
    right = mergeSort(right)
    merge(left,right)
end


def merge(left,right)
    result = []
    while left.length > 0 || right.length > 0
        if left.length > 0 && right.length > 0
            one = left[0]
            two = right[0]
            puts ("one's class is #{one.class} two's class is #{two.class} two is #{two}")
            if one <= two
                result << left.shift
            else
                result << right.shift
            end
        elsif left.length > 0
            result.push(left)
            left = []
        else
            result.push(right)
            right = []
        end 
    end
    puts "result is #{result}"
    result
end

I am new to Ruby and I am trying out the merge sort algorithm as given in Wikipedia

I am getting "comparison of Fixnum with Array failed (ArgumentError)" failed error when comparing the first elements of the left and right array in the merge method. What could be the reason and how do I resolve this problem? Thanks :)

def mergeSort(array)
    if array.length == 1
        return array
    end

    middle = array.length/2 - 1
    left = array[0..middle]
    right = array[middle+1..array.length-1]

    left = mergeSort(left)
    right = mergeSort(right)
    merge(left,right)
end


def merge(left,right)
    result = []
    while left.length > 0 || right.length > 0
        if left.length > 0 && right.length > 0
            one = left[0]
            two = right[0]
            puts ("one's class is #{one.class} two's class is #{two.class} two is #{two}")
            if one <= two
                result << left.shift
            else
                result << right.shift
            end
        elsif left.length > 0
            result.push(left)
            left = []
        else
            result.push(right)
            right = []
        end 
    end
    puts "result is #{result}"
    result
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2024-12-22 15:34:49

错误如下:

    elsif left.length > 0
        result.push(left)
        left = []
    else
        result.push(right)
        right = []
    end

一个简单的示例应该说明原因:

irb(main):067:0> a=[1,2]
=> [1, 2]
irb(main):068:0> b=[3,4]
=> [3, 4]
irb(main):069:0> a.push(b)
=> [1, 2, [3, 4]]

尝试使用 concat(),而不是 push()

The error is on these lines:

    elsif left.length > 0
        result.push(left)
        left = []
    else
        result.push(right)
        right = []
    end

A simple example should indicate why:

irb(main):067:0> a=[1,2]
=> [1, 2]
irb(main):068:0> b=[3,4]
=> [3, 4]
irb(main):069:0> a.push(b)
=> [1, 2, [3, 4]]

Instead of push(), try concat().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文