Is there an elegant way to find and replace any integers superior to 3 (as example) in a multidimensional array? The array may have the dimension 1, 2, 3, or more. Just an example of a such array:
[ [ [ 3, 3, 5 ],
[ 4, 3, 3 ] ],
[ [ 3, 2, 3 ],
[ 0, 3, 8 ] ] ]
I would like to do so without flatten the array.
Following sepp2k idea, here is a possible implementation:
class Object
def deep_map(&block)
if self.respond_to? :each
result = []
self.each do |e|
result << e.deep_map(&block)
end
return result
else
return block.call(self)
end
end
end
Then apply deep_map as you wish on the array:
> [[[3, 3, 5], [4, 3, 3]], [[3, 2, 3], [0, 3, 8]]].deep_map { |e| e > 3 ? 0 : e }
=> [[[3, 3, 0], [0, 3, 3]], [[3, 2, 3], [0, 3, 0]]]
Or, more briefly:
class Object
def deep_map(&block)
respond_to?(:map) ? map{|e| e.deep_map(&block)} : block.call(self)
end
end
Or, polymorphically:
class Object
def deep_map(&block); block.call(self) end
end
class Array
def deep_map(&block); map{|e| e.deep_map(&block)} end
end
You can write a deep_map method, which calls map on the array and then for each element test whether it's a sub-array. If it is, call deep_map recursively with the sub-array, otherwise yield the element.
You can then use that deep_map method to transform the inner elements of your multi-dimensional array without affecting its structure.
So, if I've done this right, f(x) will traverse a multidimensional Array until it finds one containing something that isn't an Array or subclass of Array, at which point it will yield the innermost Array to the block and replace it with the block's return value.
def f x, &block
x.map do |a|
if a.first.class.ancestors.include? Array
f a, &block
else
yield a
end
end
end
p(f [ [ [ 3, 3, 5 ],
[ 4, 3, 3 ] ],
[ [ 3, 2, 3 ],
[ 0, 3, 8 ] ] ] do |e|
e.reject { |x| x > 3 }
end
)
Related
Below is a piece of code (credit to Rafael Rivera) which plots points at the vertices of a model in SketchUp.
def pointplot
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
edges = selection.grep(Sketchup::Edge)
if edges.empty?
msg = 'Select one or more edges before using this tool.'
UI.messagebox(msg)
return
end
vertices = []
edges.each { |edge| vertices << edge.vertices }
vertices.flatten!
vertices.uniq!
vertices.each { |vertex| entities.add_cpoint vertex.position }
end
def check_line
sel = Sketchup.active_model.selection
ok = sel.find { |e| e.typename == "Edge" }
ok ? MF_ENABLED : MF_GRAYED
end
UI.add_context_menu_handler do |menu|
menu.add_separator
item = menu.add_item("Point Plot") { pointplot }
menu.set_validation_proc(item) {check_line}
end
Could someone please explain to me this line of code, what it actually does and why it's necessary for the code to work.
vertices.flatten!
I am aware what ".flatten!" does under normal circumstances. I understand this example perfectly from the rubyapi.org
a = [ 0, [ 1, [2, 3], 4 ], 5 ]
a.flatten!(1) # => [0, 1, [2, 3], 4, 5]
a = [ 0, [ 1, [2, 3], 4 ], 5 ]
a.flatten!(2) # => [0, 1, 2, 3, 4, 5]
a = [ 0, [ 1, [2, 3], 4 ], 5 ]
a.flatten!(3) # => [0, 1, 2, 3, 4, 5]
[0, 1, 2].flatten!(1) # => nil
But in the world of SketchUp, what does ".flatten!" actually do?
I 'put' the vertices array to my console and I see this as output.
#<Sketchup::Vertex:0x00000180a0788440>
#<Sketchup::Vertex:0x00000180a0788418>
#<Sketchup::Vertex:0x00000180a07883c8>
#<Sketchup::Vertex:0x00000180a07883a0>
#<Sketchup::Vertex:0x00000180a0788440>
#<Sketchup::Vertex:0x00000180a0788418>
#<Sketchup::Vertex:0x00000180a07883c8>
So what am I 'flattening' exactly?
Thanks!
It does exactly the same as the behavior you already observed with flatten with the only difference that it changes the object on which it is called instead of returning a changed object.
Let's have a look at these three lines:
vertices = []
edges.each { |edge| vertices << edge.vertices }
vertices.flatten!
First, there is an empty array created. Then by iterating over all edges the edges' vertices (which are very likely are stored in an array) are added to the array. That means after this line you have a nested array of vertices that looks like this (pseudo-code):
[[vertice_1, vertice_2], [vertice_3, vertice_4], [vertice_1, vertice_4]]
vertices.flatten! will then flatten the vertices to:
[vertice_1, vertice_2, vertice_3, vertice_4, vertice_1, vertice_4]
I want my function to return the longest Array within a nested array (including the array itself) so
nested_ary = [[1,2],[[1,2,[[1,2,3,4,[5],6,7,11]]]],[1,[2]]
deep_max(nested_ary)
=> [1,2,3,4,[5],6,7,11]
simple_ary = [1,2,3,4,5]
deep_max(simple_ary)
=> returns: [1,2,3,4,5]
I created a function to collect all arrays. I have to get the max value in another function.
my code:
def deep_max(ary)
ary.inject([ary]) { |memo, elem|
if elem.is_a?(Array)
memo.concat(deep_max(elem))
else
memo
end }
end
This gives me what I want:
deep_max(nested_ary).max_by{ |elem| elem.size }
Is there a way to get this max inside of the function?
def deep_max(arr)
biggest_so_far = arr
arr.each do |e|
if e.is_a?(Array)
candidate = deep_max(e)
biggest_so_far = candidate if candidate.size > biggest_so_far.size
end
end
biggest_so_far
end
deep_max [[1, 2], [[1, 2, [[1, 2, 3, 4, [5], 6, 7, 11]]]], [1, [2]]]
#=> [1, 2, 3, 4, [5], 6, 7, 11]
You can unroll it:
def deep_max(ary)
arys = []
ary = [ary]
until ary.empty?
elem = ary.pop
if elem.is_a?(Array)
ary.push(*elem)
arys.push(elem)
end
end
arys.max_by(&:size)
end
Or you can cheat, by introducing an optional parameter that changes how your recursion works on top level vs how it behaves down the rabbit hole.
I have hundreds of arrays that am normalizing for a CSV.
[
["foo", "tom", nil, 1, 4, "cheese"],
["foo", "tom", "fluffy",nil, 4],
["foo", "tom", "fluffy",1, nil],
...
]
Currently to make them all equal length i am finding the max length and setting to a value.
rows.each { |row| row[max_index] ||= nil }
this is cool because it makes the array length equal to the new length.
Instead of appending a bunch of nils at the end I needed to append COLUMN_N where N is the index (1-based).
table_rows.each do |row|
last_index = row.length - 1
(last_index..max_index).to_a.each { |index| row[index] ||= "COLUMN_#{index+1}" }
end
Which seemed like an awkward way to have a default value that is a function of the index.
You can't choose a default value for filling elements with []= method. But you can easily do something like this if there aren't other nils that you don't want to replace.
row.each_with_index.map { |item, index| item.nil? ? "column_#{index}": item }
To get a default value instead of nil you can use fetch:
row = ["foo", "tom", "fluffy", 1, 4]
row.fetch(7) { |i| "COLUMN_#{i + 1}" }
=> "COLUMN_8"
But it won't fill the array for you.
Also see: Can I create an array in Ruby with default values?
This seems like it could work for you.
class Array
def push_with_default(item, index, &block)
new_arr = Array.new([self.size + 1, index].max, &block)
self[index] = item
self.map!.with_index { |n, i| n.nil? ? new_arr[i] : n }
end
end
>> array = [1,2,5,9]
[
[0] 1,
[1] 2,
[2] 5,
[3] 9
]
>> array.push_with_default(2, 10) { |i| "column_#{i}" }
[
[ 0] 1,
[ 1] 2,
[ 2] 5,
[ 3] 9,
[ 4] "column_4",
[ 5] "column_5",
[ 6] "column_6",
[ 7] "column_7",
[ 8] "column_8",
[ 9] "column_9",
[10] 2
]
I don't believe a method like this exists on Array already though.
I am creating a map for my roguelike game and already I stumbled on a problem. I want to create a two dimensional array of objects. In my previous C++ game I did this:
class tile; //found in another file.
tile theMap[MAP_WIDTH][MAP_HEIGHT];
I can't figure out how I should do this with Ruby.
theMap = Array.new(MAP_HEIGHT) { Array.new(MAP_WIDTH) { Tile.new } }
Use arrays of arrays.
board = [
[ 1, 2, 3 ],
[ 4, 5, 6 ]
]
x = Array.new(3){|i| Array.new(3){|j| i+j}}
Also look into the Matrix class:
require 'matrix'
Matrix.build(3,3){|i, j| i+j}
2D arrays are no sweat
array = [[1,2],[3,4],[5,6]]
=> [[1, 2], [3, 4], [5, 6]]
array[0][0]
=> 1
array.flatten
=> [1, 2, 3, 4, 5, 6]
array.transpose
=> [[1, 3, 5], [2, 4, 6]]
For loading 2D arrays try something like:
rows, cols = 2,3
mat = Array.new(rows) { Array.new(cols) }
# Let's define some class
class Foo
# constructor
def initialize(smthng)
#print_me = smthng
end
def print
puts #print_me
end
# Now let's create 2×2 array with Foo objects
the_map = [
[Foo.new("Dark"), Foo.new("side")],
[Foo.new("of the"), Foo.new("spoon")] ]
# Now to call one of the object's methods just do something like
the_map[0][0].print # will print "Dark"
the_map[1][1].print # will print "spoon"
I've got two arrays of Tasks - created and assigned.
I want to remove all assigned tasks from the array of created tasks.
Here's my working, but messy, code:
#assigned_tasks = #user.assigned_tasks
#created_tasks = #user.created_tasks
#Do not show created tasks assigned to self
#created_not_doing_tasks = Array.new
#created_tasks.each do |task|
unless #assigned_tasks.include?(task)
#created_not_doing_tasks << task
end
end
I'm sure there's a better way. What is it?
Thanks :-)
You can subtract arrays in Ruby:
[1,2,3,4,5] - [1,3,4] #=> [2,5]
ary - other_ary → new_ary Array Difference
Returns a new array that is a copy of the original array, removing any
items that also appear in other_ary. The order is preserved from the
original array.
It compares elements using their hash and eql? methods for efficiency.
[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
If you need
set-like behavior, see the library class Set.
See the Array documentation.
The above solution
a - b
deletes all instances of elements in array b from array a.
[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
In some cases, you want the result to be [1, 2, 3, 3, 5]. That is, you don't want to delete all duplicates, but only the elements individually.
You could achieve this by
class Array
def delete_elements_in(ary)
ary.each do |x|
if index = index(x)
delete_at(index)
end
end
end
end
test
irb(main):198:0> a = [ 1, 1, 2, 2, 3, 3, 4, 5 ]
=> [1, 1, 2, 2, 3, 3, 4, 5]
irb(main):199:0> b = [ 1, 2, 4 ]
=> [1, 2, 4]
irb(main):200:0> a.delete_elements_in(b)
=> [1, 2, 4]
irb(main):201:0> a
=> [1, 2, 3, 3, 5]
The code works even when the two arrays are not sorted. In the example, the arrays are sorted, but this is not required.