-
-
Notifications
You must be signed in to change notification settings - Fork 525
[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
[WIP] add dominoes #424
Changes from all commits
0aae26a
38175f3
8081c48
647b021
5e17310
5470ce0
070a66d
bf2da69
d0d9fb4
50278bc
90b8e4f
795051e
16853cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 |
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 | ||
|
||
def initialize(left, right) | ||
@left = left | ||
@right = right | ||
end | ||
|
||
def double? | ||
left == right | ||
end | ||
end |
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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rubyism: if you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is much cleaner, thanks.
|
||
if !already_dominoes | ||
@list << Domino.new(item[0], item[1]) | ||
else | ||
@list << Domino.deep_copy(item) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this different than always calling There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the same as returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: Ruby defines the % irb
irb(main):001:0> 1.zero?
=> false
irb(main):002:0> 0.zero?
=> true
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! 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!
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! 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!
|
||
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 |
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll need to swap your solution from |
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 |
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 |
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.