Skip to content

Commit

Permalink
update distributed_merge method for cases with single element arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
msuliq committed Apr 15, 2023
1 parent 9dd5cc3 commit a8a91ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ Metrics/MethodLength:
Max: 20

Metrics/AbcSize:
Max: 30
Max: 35

Metrics/Metrics/CyclomaticComplexity:
Max: 10

Metrics/PerceivedComplexity:
Max: 10
8 changes: 4 additions & 4 deletions distributed_merge.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Gem::Specification.new do |spec|
spec.authors = ["Suleyman Musayev"]
spec.email = ["slmusayev@gmail.com"]

spec.summary = "Gem that adds the method to merge arrays of unequal size with equalized
distribution of elements."
spec.summary = "Gem that adds the method to merge arrays of unequal size with
distribution of elements."
spec.description = "This gem extends Array class with the distributed_merge method that
accepts a two-dimensional array containing an arbitrary number of arrays of varying
length and returns a one-dimensional merged array with the equally distributed
elements."
length and returns a one-dimensional merged array with the elements equally interleaved
and distributed."
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"
spec.homepage = "https://github.com/msuliq/distributed_merge"
Expand Down
25 changes: 19 additions & 6 deletions lib/distributed_merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
require_relative "distributed_merge/version"

module DistributedMerge
class Array # :nodoc:
module DistributedMergeArray # :nodoc:
# Accepts a two-dimensional array with subarrays of varying size and returns
# merged array with the elements distributed evenly.
# merged array with the elements distributed across.

def distributed_merge
return self unless two_dimensional?
return self unless all_arrays? && two_dimensional?

data = sort_by { |ary| ary.flatten.count }.reverse
data = sort_arrays_by_size
largest = data.first

data.drop(1).each do |ary|
offset = largest.length / (ary.length + 1)

ary.each_with_index do |el, i|
if i == 1
if ary.length == 1
largest.insert((largest.length / 2), el)
elsif i == 1
largest.insert(((largest.length / 2) - offset), el)
elsif i.even?
largest.insert(((largest.length / 2) + (offset + i)), el)
Expand All @@ -29,8 +32,18 @@ def distributed_merge
largest
end

def all_arrays?
all? { |arg| arg.instance_of?(Array) }
end

def two_dimensional?
all? { |arg| arg.is_a?(Array) } && flatten(1).count > 1
flatten(1).none? { |arg| arg.instance_of?(Array) } && flatten(1).count > 1
end

def sort_arrays_by_size
sort_by { |ary| ary.flatten.count }.reverse
end
end
end

Array.include DistributedMerge::DistributedMergeArray

0 comments on commit a8a91ae

Please sign in to comment.