Two Dimensional Array

J

Justin To

class Tda

def initialize(width=0, height=0)
if((width==0) && width<height)
raise NotImplementedError,
"\(#{width}\) width Tda cannot have a height."
else
@tda = Array.new(width).map!{ Array.new(height) }
end
end

def width?
return @tda.size
end

def height?
if(width?>0)
return @tda[0].size
else
return nil
end
end

#--Returns the dimensional size (x x y) as a string
def size
[email protected]

if(width?>0)
h = @tda[0].size
else
h = 0
end

return "#{w}x#{h}"
end

#--Returns true if both Tda's have equal widths and heights
def eql_size?(tda)
bool=false

lh=size.split('x')
rh=tda.size.split('x')

if(lh[0]==rh[0])
bool=true
end

if(lh[1]==rh[1])
bool=true
else
bool=false
end

return bool
end

end

This is what I've got so far, but I can't seem to figure out how to
write methods for: copying a Tda to another Tda, and indexing the Tda.
Thanks for the help!
 
S

Shashank Agarwal

Justin said:
class Tda

def initialize(width=0, height=0)
if((width==0) && width<height)
raise NotImplementedError,
"\(#{width}\) width Tda cannot have a height."
else
@tda = Array.new(width).map!{ Array.new(height) }
end
end

def width?
return @tda.size
end

def height?
if(width?>0)
return @tda[0].size
else
return nil
end
end

#--Returns the dimensional size (x x y) as a string
def size
[email protected]

if(width?>0)
h = @tda[0].size
else
h = 0
end

return "#{w}x#{h}"
end

#--Returns true if both Tda's have equal widths and heights
def eql_size?(tda)
bool=false

lh=size.split('x')
rh=tda.size.split('x')

if(lh[0]==rh[0])
bool=true
end

if(lh[1]==rh[1])
bool=true
else
bool=false
end

return bool
end

end

This is what I've got so far, but I can't seem to figure out how to
write methods for: copying a Tda to another Tda, and indexing the Tda.
Thanks for the help!

I haven't tried this, but this method might help -

def copy_tda(original_array)
new_tda = []
original_array.each_index do |i|
new_a = []
original_array.each_index { |j| new_a <<
original_array[j].clone }
new_tda << new_a
end
return new_tda
end
 
T

Todd Benson

This is what I've got so far, but I can't seem to figure out how to
write methods for: copying a Tda to another Tda, and indexing the Tda.

Marshal.dump and Marshal.load might help you.

Todd
 
A

ara.t.howard

class Tda

def initialize(width=0, height=0)
if((width==0) && width<height)
raise NotImplementedError,
"\(#{width}\) width Tda cannot have a height."
else
@tda = Array.new(width).map!{ Array.new(height) }
end
end

def width?
return @tda.size
end

def height?
if(width?>0)
return @tda[0].size
else
return nil
end
end

#--Returns the dimensional size (x x y) as a string
def size
[email protected]

if(width?>0)
h = @tda[0].size
else
h = 0
end

return "#{w}x#{h}"
end

#--Returns true if both Tda's have equal widths and heights
def eql_size?(tda)
bool=false

lh=size.split('x')
rh=tda.size.split('x')

if(lh[0]==rh[0])
bool=true
end

if(lh[1]==rh[1])
bool=true
else
bool=false
end

return bool
end

end

This is what I've got so far, but I can't seem to figure out how to
write methods for: copying a Tda to another Tda, and indexing the Tda.
Thanks for the help!


gem install narray

it's this and *much* *much* more - not to mention insanely fast.


a @ http://codeforpeople.com/
 
D

David A. Black

Hi --

#--Returns true if both Tda's have equal widths and heights
def eql_size?(tda)
bool=false

lh=size.split('x')
rh=tda.size.split('x')

if(lh[0]==rh[0])
bool=true
end

if(lh[1]==rh[1])
bool=true
else
bool=false
end

return bool
end

See Ara Howard's advice about using narray. Still -- I just wanted to
suggest one or two things to make this method more concise and clear.

One thing you could do is:

def eql_size?(tda)
lh=size.split('x')
rh=tda.size.split('x')

return lh[0] == rh[0] && lh[1] == rh[1]
end

In general, it's good to let the comparison methods, like ==, do their
thing, which is to produce true and false. There's usually no need to
store and test the results separately.

Moreover, in this particular case, I think you could just do:

def eql_size?(tda)
size == tda.size
end

:)


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top