Ruby数组在子数组中查找重复元素并添加元素

时间:2016-10-14 20:21:00

标签: arrays ruby duplicates

对于不太明确的问题感到抱歉,但我无法找到更好的询问方式......

我有一个数组

a=[[1,2], [3,4], [4,2], [2,1]]

我想找到具有重复元素的子数组,在本例中为[1,2]和[4,2]

我想将重复的元素添加到一起并创建一个新条目并删除2个原始子数组。

结果...

a=[[5,2], [3,4], [2,1]]

这可能吗?

编辑:添加了一些代码和更多关于我试图完成的内容的详细信息

我对编程非常陌生,喜欢挑战自己,所以我尝试编写一个二维垃圾箱算法来为厨柜创建切片......我是一个橱柜制造商。

这是我到目前为止所提出的......我所问的是在split_sheet方法中。基本上在某些情况下,我想要组合具有相同宽度或相同长度的剩余空片段。可能会有更多情况需要类似的东西但到目前为止我已经找到了所有类似的东西。

我尝试使用地图,但我无法使用地图,因此我正在做的是将新的工作表部分与现有部分进行比较,看看我是否找到匹配。

    def cut_sheet_layout_algorithm#(length, width)
        sort_by_length_width=@cabinet_cut_sheet.sort{|a,b| [b[3], b[2]]<=>[a[3], a[2]]}     # sort by length then width     
        parts_group_by_thickness=sort_by_length_width.group_by{|thickness| thickness[1]}.values # group by thickness

        def create_sheet(length, width, thick)#, @new_sheet, sheet_full)                # Creates sheet
        @sheet=[]; @sheet_free_space=[]; @sheet_coords=[]; @new_sheet=false; sheet_full=false
        @sheet.push(length, width, thick)
        @sheet_free_space.push([length, width])
        end

        def split_sheet(section_number, sheet_section, part_dimensions)
        if part_dimensions[0]<=sheet_section[0] && part_dimensions[1]<=sheet_section[1]
            new_section_a_width=sheet_section[0]-part_dimensions[0]
            new_section_b_length=sheet_section[1]-part_dimensions[1]

            new_section_a_dimensions=[part_dimensions[0], new_section_b_length]
            new_section_b_dimensions=[new_section_a_width, sheet_section[1]]

            @sheet_free_space.delete_at(section_number)

            @section_a_combined=@section_b_combined=false

            @sheet_free_space.each{|section|
            @section_a_0=new_section_a_dimensions[0]==section[0]
            @section_a_1=new_section_a_dimensions[1]==section[1]

            @section_b_0=new_section_b_dimensions[0]==section[0]
            @section_b_1=new_section_b_dimensions[1]==section[1]

            if @section_a_1==true || @section_b_1==true
                section[0]=new_section_a_dimensions[0]+section[0]
                @section_a_combined=true
            end

            if @section_a_0==true || @section_b_0==true
                section[1]=new_section_a_dimensions[1]+section[1]
                @section_b_combined=true
            end
            }

            @sheet_free_space << new_section_a_dimensions if new_section_b_length!=0 && @section_a_combined==false 
            @sheet_free_space << new_section_b_dimensions if new_section_a_width!=0 && @section_b_combined==false
        end
        end

        def does_part_fit(part_dimensions)
        section_number=0
        @sheet_free_space.each{|section|
            if part_dimensions[0]<=section[0] && part_dimensions[1]<=section[1]
            @part_fits=true
            sheet_section=@sheet_free_space[section_number]
            split_sheet(section_number, sheet_section, part_dimensions)

            break
            else
            @part_fits=false
            end

            section_number+=1               
        }
        end

        parts_group_by_thickness.each{|grouped_by_thickness|                    # iterate groups to create cut sheet lists according to sheet thickness
        sheet_thick=grouped_by_thickness[0][1]
        @new_sheet=true                                     # variable to check if new sheet , used to determine startx and starty
        number_of_parts=grouped_by_thickness.length
        grouped_by_thickness.delete_if do |part|                        # iterate parts of each sheet thickness group
            create_sheet(@sheet_size_length, @sheet_size_width, sheet_thick) if @new_sheet==true # create new sheet only if preceeding sheet is full
            part_dimensions=[part_length=part[2].to_l+@blade_kerf, part_width=part[3].to_l+@blade_kerf]

            does_part_fit(part_dimensions)

            if @part_fits==true     # delete part if placed
            true
            else
            @new_sheet=true                                     # variable to check if new sheet , used to determine startx and starty
            false
            end
        end
        }
    end # end cut_sheet_layout_algorithm

随意批评我的代码,这是我的学习方式。

2 个答案:

答案 0 :(得分:3)

这是一件非常棘手的事情,但是Enumerable你可以使用很多工具帮助破解它。

这是一种方法:

beaconpoc@cqlsh:test> describe TYPES;

list[i] not a string for i in 0

答案 1 :(得分:0)

我不确定我是否理解您的问题,但以下代码比较了两个数组并删除了重复数据。

a=[[1], [2], [3], [4]]

b=[[1], [3]]

c = a - b

puts c

所以你想要做类似的事情。

相关问题