[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Programmer Ping-Pong (#150

James Gray

12/14/2007 1:14:00 PM

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rub...

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This is a non-traditional Ruby Quiz that changes the rules of the contest.
Please read the entire message before playing along. We will be back to normal
quizzes next time, for those that end up missing them.

The Game

Eric Hodel described Programmer Ping-Pong in his RubyConf 2007 presentation. I
wasn't familiar with the concept before that and it sounds like fun, so let's
all try it out together.

The rules are:

* This quiz does not have a no-spoiler period so you may
submit at anytime after reading this message
* I'll make the initial serve, starting the quiz off with
a single failing test
* Anyone can return the ball at anytime by doing exactly
two things, in order: make all tests pass including the
recently added failure and then add a new failing test
of your own

I want to see if we can build an entire library using just that process.

The Task

Let's build a pure Ruby binary AVL tree. An AVL tree is a self-balancing binary
tree data structure where insertion, deletion, and lookup all take O(log n) time
to execute. This is handy to have for many search problems that must run
quickly. It can also be used to build constructs like an OrderedHash.

You can read more about AVL trees on Wikipedia:

http://en.wikipedia.org/wik...

There's also a handy document describing their rotations in detail:

http://fortheloot.com/public/AVLTreeTu...

What features our AVL tree will support will be decided by you as you write
tests. Here are some suggestions of things we might try though, just to get you
thinking:

* Support for custom Ruby objects as nodes of the tree
* The ability to customize the comparison code, perhaps with a block
* A String output visualizer, possibly for the inspect() method
* Any other great features you can think of to add

The Details

We will have two files: avl_tree.rb and test_avl_tree.rb. Please pass both
files in each submission email you make for this quiz. Let's not complicate
this with directory structures or zip files.

Please don't add any external dependencies, unless it's a standard library. We
want everyone to be able to easily run this code and play along.

We are using Test::Unit instead of RSpec, or any other tool, for similar
reasons.

Please keep your tests short. Under 10 lines is preferred, but don't go over
24.

Also try to test just one aspect of the implementation with each test. I did
purposely say "aspect" and not "method." I do test more than one method in the
serve and I can imagine other scenarios where it could be useful, like checking
support for a handful of the standard Enumerator methods.

You can refactor any code as needed provided you do not change its function and
all tests still pass after you do so.

Adds comments if you need to, but writing code that needs no comment would be
even better.

Let's use some simple spacing conventions to keep all of us on the same page.
Indent two space and do not use tabs. Break up long lines so they do not exceed
80 characters.

Finally, this quiz has the potential to be very chaotic. Take pity on your
quizmaster who must track this process and on the rest of the community who may
be bothered by a highly active thread. I suggest good email manners:

* Use your client's "Reply" feature and make sure you are replying to
the message that contains the test you made pass
* Trim any unneeded context from the reply, including the previous
version of the code since you will be including the current copy
of the whole thing
* Kindness to your fellow programmers trumps any listed guidelines

The Serve

The initial contents of avl_tree.rb are:

#!/usr/bin/env ruby -wKU

class AVLTree

end

The test file, test_avl_tree.rb, begins as:

#!/usr/bin/env ruby -wKU

require "test/unit"

require "avl_tree"

class TestAVLTree < Test::Unit::TestCase
def setup
@tree = AVLTree.new
end

def test_tree_membership
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))

@tree << 3

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(3))
end
end

43 Answers

Rick DeNatale

12/14/2007 1:48:00 PM

0

On 12/14/07, Ruby Quiz <james@grayproductions.net> wrote:

> The Game
>
> Eric Hodel described Programmer Ping-Pong in his RubyConf 2007 presentation. I
> wasn't familiar with the concept before that and it sounds like fun, so let's
> all try it out together.
>
> The rules are:
>
> * This quiz does not have a no-spoiler period so you may
> submit at anytime after reading this message
> * I'll make the initial serve, starting the quiz off with
> a single failing test
> * Anyone can return the ball at anytime by doing exactly
> two things, in order: make all tests pass including the
> recently added failure and then add a new failing test
> of your own

We're doing this in the true tdd spirit of making very small steps right.

Okay, first return.

avl_tree.rb
#!/usr/bin/env ruby -wKU

class AVLTree

def empty?
!@contents
end

def include?(obj)
return @contents == obj
end

def <<(obj)
@contents = obj
end

end

test_avl_tree.rb
#!/usr/bin/env ruby -wKU

require "test/unit"

require "avl_tree"

class TestAVLTree < Test::Unit::TestCase
def setup
@tree = AVLTree.new
end

def test_tree_membership
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))

@tree << 3

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(3))
end

def test_tree_should_allow_more_than_one_element
@tree << 3
@tree << 4

assert(@tree.include?(4))
assert(@tree.include?(3))
end

end
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Paul Irofti

12/14/2007 2:59:00 PM

0

On 2007-12-14, Ruby Quiz <james@grayproductions.net> wrote:
[--snip--]
> The Serve
The Pong

> The initial contents of avl_tree.rb are:
The altered contents of avl_tree.rb are:

#!/usr/bin/env ruby -wKU

class AVLTree
attr_accessor :head
def initialize
@head = nil
end
def empty?
@head.nil?
end
def << (thing)
@head = thing if empty?
end
def include?(value)
@head == value
end
end

> The test file, test_avl_tree.rb, begins as:
The test file was altered as:

#!/usr/bin/env ruby -wKU

require "test/unit"

require "avl_tree"

class TestAVLTree < Test::Unit::TestCase
def setup
@tree = AVLTree.new
end

def test_tree_membership
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))

@tree << 3

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(3))
end

def test_tree_insertion
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))
assert_equal(false, @tree.include?(5))

@tree << 3
@tree << 5

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(5))
assert_equal(true, @tree.include?(3))
end
end

Hope this follows the rules as the insertion is not actually implemented
nor is the include?. But I guess in less than 20 lines that's the best a
`Pong' can do (-:
--
everything is simple, we're stupid
contact at gmail

Paul Irofti

12/14/2007 3:14:00 PM

0

On 2007-12-14, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 12/14/07, Ruby Quiz <james@grayproductions.net> wrote:
>
>> The Game
>>
>> Eric Hodel described Programmer Ping-Pong in his RubyConf 2007 presentation. I
>> wasn't familiar with the concept before that and it sounds like fun, so let's
>> all try it out together.
>>
>> The rules are:
>>
>> * This quiz does not have a no-spoiler period so you may
>> submit at anytime after reading this message
>> * I'll make the initial serve, starting the quiz off with
>> a single failing test
>> * Anyone can return the ball at anytime by doing exactly
>> two things, in order: make all tests pass including the
>> recently added failure and then add a new failing test
>> of your own
>
> We're doing this in the true tdd spirit of making very small steps right.
>
> Okay, first return.

Hmm, when I posted no one answered, but I guess you got there first.
Does your version count as the next? Or how is this settled? We seem to
have written similar things anyways.

--
everything is simple, we're stupid
contact at gmail

Rob Biedenharn

12/14/2007 3:25:00 PM

0


On Dec 14, 2007, at 10:14 AM, Paul Irofti wrote:

> On 2007-12-14, Rick DeNatale <rick.denatale@gmail.com> wrote:
>> On 12/14/07, Ruby Quiz <james@grayproductions.net> wrote:
>>
>>> The Game
>>>
>>> Eric Hodel described Programmer Ping-Pong in his RubyConf 2007
>>> presentation. I
>>> wasn't familiar with the concept before that and it sounds like
>>> fun, so let's
>>> all try it out together.
>>>
>>> The rules are:
>>>
>>> * This quiz does not have a no-spoiler period so you may
>>> submit at anytime after reading this message
>>> * I'll make the initial serve, starting the quiz off with
>>> a single failing test
>>> * Anyone can return the ball at anytime by doing exactly
>>> two things, in order: make all tests pass including the
>>> recently added failure and then add a new failing test
>>> of your own
>>
>> We're doing this in the true tdd spirit of making very small steps
>> right.
>>
>> Okay, first return.
>
> Hmm, when I posted no one answered, but I guess you got there first.
> Does your version count as the next? Or how is this settled? We seem
> to
> have written similar things anyways.
>
> --
> everything is simple, we're stupid
> contact at gmail

I'll settle that. Since your tests were equivalent, I went with
Rick's since I saw it first:

avl_tree.rb

#!/usr/bin/env ruby -wKU

class AVLTree

def initialize
@contents = []
end

def empty?
@contents.empty?
end

def include?(obj)
@contents.include?(obj)
end

def <<(obj)
@contents << obj
end

def height
end

end
__END__


test_avl_tree.rb

#!/usr/bin/env ruby -wKU

require "test/unit"

require "avl_tree"

class TestAVLTree < Test::Unit::TestCase
def setup
@tree = AVLTree.new
end

def test_tree_membership
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))

@tree << 3

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(3))
end

def test_tree_should_allow_more_than_one_element
@tree << 3
@tree << 4

assert(@tree.include?(4))
assert(@tree.include?(3))
end

def test_tree_height_of_one_or_two_nodes_is_one
@tree << 5
assert_equal 1, @tree.height
@tree << 6
assert_equal 1, @tree.height
end

end
__END__

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



John Joyce

12/14/2007 3:30:00 PM

0

question about this quiz:
How do you prevent or at least reign in the splintering/forking of
code and likely duplication of efforts here?
As with so many quizzes, I'm going to sit and watch the ping pong
battle royale. I don't know much about binary trees or other such
data structures...

Eivind Eklund

12/14/2007 3:32:00 PM

0

I ended up responding to Paul because it looked like a later
submission (and response to the first pong).

On Dec 14, 2007 4:00 PM, Paul Irofti <bulibuta@fakehost.net> wrote:
> On 2007-12-14, Ruby Quiz <james@grayproductions.net> wrote:
> [--snip--]
> > The Serve
> The Pong
>
> > The initial contents of avl_tree.rb are:
> The altered contents of avl_tree.rb are:
>
> #!/usr/bin/env ruby -wKU
>
> class AVLTree
> attr_accessor :head
> def initialize
> @head = nil
> end
> def empty?
> @head.nil?
> end
> def << (thing)
> @head = thing if empty?
> end
> def include?(value)
> @head == value
> end
> end
>
> > The test file, test_avl_tree.rb, begins as:
> The test file was altered as:
>
> #!/usr/bin/env ruby -wKU
>
> require "test/unit"
>
> require "avl_tree"
>
> class TestAVLTree < Test::Unit::TestCase
> def setup
> @tree = AVLTree.new
> end
>
> def test_tree_membership
> assert_equal(true, @tree.empty?)
> assert_equal(false, @tree.include?(3))
>
> @tree << 3
>
> assert_equal(false, @tree.empty?)
> assert_equal(true, @tree.include?(3))
> end
>
> def test_tree_insertion
> assert_equal(true, @tree.empty?)
> assert_equal(false, @tree.include?(3))
> assert_equal(false, @tree.include?(5))
>
> @tree << 3
> @tree << 5
>
> assert_equal(false, @tree.empty?)
> assert_equal(true, @tree.include?(5))
> assert_equal(true, @tree.include?(3))
> end
> end
>
> Hope this follows the rules as the insertion is not actually implemented
> nor is the include?. But I guess in less than 20 lines that's the best a
> `Pong' can do (-:

Here's a ping (or is that now a poing?)

Note that I've joined up the test and tree files using the customary
$0 == __FILE__ hack, to make participation as simple as cut/pasting to
a single file. You run the tests by "ruby avltree.rb" (or
/avltree.rb if you change modes), and use the library by "require
'avltree'". The require will NOT run the tests.


#!/usr/bin/env ruby -wKU

class AVLTree
attr_accessor :head, :left
def initialize
@head = nil
@left = nil
end
def empty?
@head.nil?
end
def << (thing)
if empty?
@head = thing
else
@left = AVLTree.new
@left << thing
end
end
def include?(value)
@head == value || (@left != nil && @left.include?(value))
end
end

if $0 == __FILE__
require "test/unit"

class TestAVLTree < Test::Unit::TestCase
def setup
@tree = AVLTree.new
end

def test_tree_membership
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))

@tree << 3

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(3))
end

def test_tree_insertion
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))
assert_equal(false, @tree.include?(5))

@tree << 3
@tree << 5

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(5))
assert_equal(true, @tree.include?(3))
end

def test_tree_include_many
0.upto(10) do |i|
assert(false, @tree.include?(i))
@tree << i
0.upto(i) do |j|
assert(true, @tree.include?(j))
end
end
end
end
end

Ken Bloom

12/14/2007 3:58:00 PM

0


#!/usr/bin/env ruby -wKU

class AVLTree

def initialize
@contents = []
end

def empty?
@contents.empty?
end

def include?(obj)
@contents.include?(obj)
end

def <<(obj)
@contents << obj
end

def height
1
end

end
__END__


test_avl_tree.rb

#!/usr/bin/env ruby -wKU

require "test/unit"

require "avl_tree"

class TestAVLTree < Test::Unit::TestCase
def setup
@tree = AVLTree.new
end

def test_tree_membership
assert_equal(true, @tree.empty?)
assert_equal(false, @tree.include?(3))

@tree << 3

assert_equal(false, @tree.empty?)
assert_equal(true, @tree.include?(3))
end

def test_tree_should_allow_more_than_one_element
@tree << 3
@tree << 4

assert(@tree.include?(4))
assert(@tree.include?(3))
end

def test_tree_height_of_one_or_two_nodes_is_one
@tree << 5
assert_equal 1, @tree.height
@tree << 6
assert_equal 1, @tree.height
end
def test_tree_height_of_three_nodes_is_two
@tree << 5
@tree << 6
@tree << 7
assert_equal 2, @tree.height
end

end
__END__


--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Phrogz

12/14/2007 4:14:00 PM

0

On Dec 14, 8:30 am, John Joyce <dangerwillrobinsondan...@gmail.com>
wrote:
> question about this quiz:
> How do you prevent or at least reign in the splintering/forking of
> code and likely duplication of efforts here?
> As with so many quizzes, I'm going to sit and watch the ping pong
> battle royale. I don't know much about binary trees or other such
> data structures...

Obviously we need to create a tree simply to track to the bifurcating
responses :)

Paul Irofti

12/14/2007 4:51:00 PM

0

On 2007-12-14, Ken Bloom <kbloom@gmail.com> wrote:
>
> #!/usr/bin/env ruby -wKU
>
> class AVLTree
>
> def initialize
> @contents = []
> end
>
> def empty?
> @contents.empty?
> end
>
> def include?(obj)
> @contents.include?(obj)
> end
>
> def <<(obj)
> @contents << obj
> end
>
> def height
> 1
> end
I think that the second assert against 1 was a typo, instead of 2 he
typed 1, the idea being to implement height.

I see there was another answer to my pong instead this one. Who's the
arbiter here? Or referee? Or jury? (-: I think we'll need one.

--
everything is simple, we're stupid
contact at gmail

Eric I.

12/14/2007 5:13:00 PM

0

On Dec 14, 10:58 am, Ken Bloom <kbl...@gmail.com> wrote:
> def test_tree_height_of_one_or_two_nodes_is_one
> @tree << 5
> assert_equal 1, @tree.height
> @tree << 6
> assert_equal 1, @tree.height
> end
> def test_tree_height_of_three_nodes_is_two
> @tree << 5
> @tree << 6
> @tree << 7
> assert_equal 2, @tree.height
> end

Ken,

I think you've led us astray. Since all the examples of AVL trees
cited by James show that the data is stored in the interior nodes (and
not just the leaf nodes), it seems to me that the height of a tree
with two or three pieces of data should be larger (by one) than that
of a tree with only one piece of data.

The only issue is whether height of a tree with a single piece of data
is 0 or 1 (i.e., are we measuring the number of nodes from root to
leaf or the number of "edges"). Given that the Wikipedia article
says, "an AVL tree's height is limited to 1.44 * lg n", I think we
should measure height by nodes.

I figured a discussion would be better than simply modifying your code
to fit my conceptions. So what do you think?

Eric