Skip to content

[WIP] add dominoes #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/generate-dominoes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

require_relative '../lib/helper'
require 'generator'
require 'dominoes_cases'

Generator.new('dominoes', DominoesCases).generate
1 change: 1 addition & 0 deletions exercises/dominoes/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
16 changes: 16 additions & 0 deletions exercises/dominoes/domino.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Domino
attr_reader :left, :right

def self.deep_copy(domino)
Domino.new(domino.left, domino.right)
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as https://ruby-doc.org/core-2.3.1/Object.html#method-i-clone? If so, can we use the Ruby method?

Copy link
Author

@seanreed1111 seanreed1111 Sep 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe I can use clone method. Need to investigate differences between using clone and dup in this context when I have a minute.


def initialize(left, right)
@left = left
@right = right
end

def double?
left == right
end
end
53 changes: 53 additions & 0 deletions exercises/dominoes/dominoes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Dominoes
require_relative 'domino'
attr_accessor :list

#[ArrayTuples], false => [Domino] or [Domino], true => [Domino]
def initialize( arr=[], already_dominoes= nil)
@list = []
arr.each do |item|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubyism: if you use map instead of each, you don't need the @list variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is much cleaner, thanks.
I'd still actually like that @list because I believe I'm going to need to construct separate sublists and keep track of them for the ultimate n-domino solution.

On Sep 1, 2016, at 1:56 PM, Tute Costa notifications@github.com wrote:

In exercises/dominoes/dominoes.rb:

@@ -0,0 +1,53 @@
+class Dominoes

  • require_relative 'domino'
  • attr_accessor :list
  • #[ArrayTuples], false => [Domino] or [Domino], true => [Domino]
  • def initialize( arr=[], already_dominoes= nil)
  • @list = []
  • arr.each do |item|
    Rubyism: if you use map instead of each, you don't need the @list variable.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

if !already_dominoes
@list << Domino.new(item[0], item[1])
else
@list << Domino.deep_copy(item)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this different than always calling Domino.new(item[0], item[1])?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fully admit to putting machinery in here that I am not 100% sure that I'll need! I've been sketching out a solution on paper during free moments on my commute, and that (unseen) material is sort of driving this code.

end
end
end


# [Domino] => Bool
def can_chain?
result = false

return true if list.length.zero? # empty list
return (list.first.double?) if list.length == 1 #singletons

result
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as returning dominoes_list.length == 0?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Ruby defines the zero? method on numbers:

% irb
irb(main):001:0> 1.zero?
=> false
irb(main):002:0> 0.zero?
=> true

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is true, but I do not believe and of the solutions beyond the base cases are dependent on length. I just wanted to get the base cases out of the way.

My suspicion is that I need to frame finding the generic solution as a graph, and then figure out how to traverse the graph to see if at least one solution exists..

"Serena Sees Her Footprints on the Moon" is on sale now @ iTunes!
https://itun.es/us/CnX6bb.l

It's the story of a girl who imagines a trip to the Moon, where she interacts with some of the objects left behind up there by the astronauts from the Apollo missions.

No iPad or iPhone? No problem!
Get the book here: https://gum.co/softcover

On Aug 28, 2016, at 6:10 PM, Tute Costa notifications@github.com wrote:

In exercises/dominoes/dominoes.rb:

@@ -0,0 +1,8 @@
+class Dominoes
+

  • def can_chain?(dominoes_list = [])
  • result = false
  • return true if dominoes_list.length == 0
  • result
    Note: Ruby defines the zero? method on numbers:

% irb
irb(main):001:0> 1.zero?
=> false
irb(main):002:0> 0.zero?
=> true

You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are correct of course. Will do that as I get into refactoring..

"Serena Sees Her Footprints on the Moon" is on sale now @ iTunes!
https://itun.es/us/CnX6bb.l

It's the story of a girl who imagines a trip to the Moon, where she interacts with some of the objects left behind up there by the astronauts from the Apollo missions.

No iPad or iPhone? No problem!
Get the book here: https://gum.co/softcover

On Aug 28, 2016, at 6:10 PM, Tute Costa notifications@github.com wrote:

In exercises/dominoes/dominoes.rb:

@@ -0,0 +1,8 @@
+class Dominoes
+

  • def can_chain?(dominoes_list = [])
  • result = false
  • return true if dominoes_list.length == 0
  • result
    Is this the same as returning dominoes_list.length == 0?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

end

def first
@list[0]
end

# # [ArrayTuples] => Bool
# def can_chain_list?(arr_form_of_dominoes)
# result = false

# return true if arr_list.length.zero? # empty list
# return (arr_list[0][0] == arr_list[0][1]) if arr_list.length == 1 #singletons

# result
# end



# def self.make_dominoes!(arr_form_of_dominoes =[])
# @list = []
# if !arr_form_of_dominoes.empty?
# arr_form_of_dominoes.each do |item|
# @list << Domino.new(item[0], item[1])
# end
# end
# end

end
99 changes: 99 additions & 0 deletions exercises/dominoes/dominoes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env ruby
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'dominoes'

# Test data version:
# 08a0cda
class DominoesTest < Minitest::Test
def test_empty_input
actual = Dominoes.new([]).can_chain?
assert(actual)
end

def test_singleton_input_chainable
actual = Dominoes.new([[1, 1]]).can_chain?
assert(actual)
end

def test_singleton_input_not_chainable
actual = Dominoes.new([[1, 2]]).can_chain?
refute(actual)
end

def test_three_elements
skip
actual = Dominoes.new.can_chain?([[1, 2], [3, 1], [2, 3]])
assert(actual)
end

def test_can_reverse_dominoes
skip
actual = Dominoes.new.can_chain?([[1, 2], [1, 3], [2, 3]])
assert(actual)
end

def test_cant_be_chained
skip
actual = Dominoes.new.can_chain?([[1, 2], [4, 1], [2, 3]])
refute(actual)
end

def test_disconnected_simple
skip
actual = Dominoes.new.can_chain?([[1, 1], [2, 2]])
refute(actual)
end

def test_disconnected_double_loop
skip
actual = Dominoes.new.can_chain?([[1, 2], [2, 1], [3, 4], [4, 3]])
refute(actual)
end

def test_disconnected_single_isolated
skip
actual = Dominoes.new.can_chain?([[1, 2], [2, 3], [3, 1], [4, 4]])
refute(actual)
end

def test_need_backtrack
skip
actual = Dominoes.new.can_chain?([[1, 2], [2, 3], [3, 1], [2, 4], [2, 4]])
assert(actual)
end

def test_separate_loops
skip
actual = Dominoes.new.can_chain?([[1, 2], [2, 3], [3, 1], [1, 1], [2, 2], [3, 3]])
assert(actual)
end

def test_ten_elements
skip
actual = Dominoes.new.can_chain?([[1, 2], [5, 3], [3, 1], [1, 2], [2, 4], [1, 6], [2, 3], [3, 4], [5, 6]])
assert(actual)
end

# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_equal 2, BookKeeping::VERSION
end
end
14 changes: 14 additions & 0 deletions exercises/dominoes/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module BookKeeping
VERSION = 2
end

class Dominoes

def can_chain?(dominoes_list = [])
false
end

def solve(dominoes_list = [])

end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need to swap your solution from dominoes.rb into here, and put this code into dominoes.rb, which is what exercism users will download to work on.

21 changes: 21 additions & 0 deletions exercises/dominoes/example.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env ruby
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'dominoes'

# Test data version:
# <%= sha1 %>
class DominoesTest < Minitest::Test<% test_cases.each do |test_case| %>
def <%= test_case.test_name %>
<%= test_case.skipped %>
expect = <%= test_case.expect %>
actual = <%= test_case.work_load %>
assert_equal(expect, actual)
end
<% end %>
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>
def test_bookkeeping
skip
assert_equal <%= version.next %>, BookKeeping::VERSION
end
end
11 changes: 11 additions & 0 deletions lib/dominoes_cases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class DominoesCase < OpenStruct


end

DominoesCases = proc do |data|
JSON.parse(data)['cases'].map.with_index do |row, i|
row = row.merge('index' => i)
DominoesCase.new(row)
end
end