Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

part2 spremembe

  • Loading branch information...
commit bf551eef7e1f1d7021c0f06512fcdee8b10abbc5 1 parent 2809de1
@jurera authored
View
58 part2.rb
@@ -18,45 +18,35 @@ def rps_game_winner(game)
end
-def rps_tournament_winner(tournament)
-
- arr_2 = Array.new
- arr_1 = Array.new
-
- tournament.each_with_index {|t_1,i_1|
-
- t_1.each_with_index {|t_2,i_2|
- print t_2, "\n"
- #arr_2[i_2] = rps_game_winner(t_2)
- }
- #arr_1[i_1] = rps_game_winner(arr_2)
-
- }
-
- #rps_game_winner(arr_1)
-
+def rps_tournament_winner(game)
+ if game[0][1].class==String
+ rps_game_winner(game)
+ else
+ a1=rps_tournament_winner(game[0])
+ a2=rps_tournament_winner(game[1])
+ rps_tournament_winner([a1,a2])
+ end
end
#tournament = [[["Dave", "S"], ["Richard", "R"]], [["Allen", "S"], ["Richard X.", "P"]]]
tournament = [
- [
- [ ["Armando", "P"], ["Dave", "S"] ],
- [ ["Richard", "R"], ["Michael", "S"] ],
- ],
- [
- [ ["Allen", "S"], ["Omer", "P"] ],
- [ ["David E.", "R"], ["Richard X.", "P"] ]
- ],
- [
- [ ["Armando", "P"], ["Dave", "S"] ],
- [ ["Richard", "R"], ["Michael", "S"] ],
- ],
- [
- [ ["Allen", "S"], ["Omer", "P"] ],
- [ ["David E.", "R"], ["Richard X.", "P"] ]
- ]
+ [
+ [ ["A1", "P"], ["A2", "S"] ],
+ [ ["A3", "R"], ["A4", "S"] ],
+ ],
+ [
+ [ ["Armando", "P"], ["Dave", "S"] ],
+ [ ["Richard", "R"], ["Michael", "S"] ],
+ ],
+ [
+ [ ["Armando", "P"], ["Dave", "S"] ],
+ [ ["Richard", "R"], ["Michael", "S"] ],
+ ],
+ [
+ [ ["Armando", "P"], ["Dave", "S"] ],
+ [ ["Richard", "R"], ["Michael", "S"] ],
+ ]
]
-
print rps_tournament_winner(tournament)
View
43 part2_test.rb
@@ -0,0 +1,43 @@
+require './part2.rb'
+require 'test/unit'
+
+class TestRockPaperScissors < Test::Unit::TestCase
+
+ def test_rps_game_winner_wrong_number_of_players
+ assert_raises WrongNumberOfPlayersError do
+ rps_game_winner []
+ end
+ assert_raises WrongNumberOfPlayersError do
+ rps_game_winner [ ['Armando', 'P'] ]
+ end
+ assert_raises WrongNumberOfPlayersError do
+ rps_game_winner [ ['Armando', 'P'], ['Dave', 'S'], ['Paul', 'R'] ]
+ end
+ end
+
+ def test_rps_game_winner_no_such_strategy
+ assert_raises NoSuchStrategyError do
+ rps_game_winner [ ['Armando', 'P'], ['Dave', 'Q'] ]
+ end
+ assert_nothing_raised NoSuchStrategyError do
+ rps_game_winner [ ['Armando', 'P'], ['Dave', 's'] ]
+ end
+ end
+
+ def test_rps_game_winner
+ assert_equal ['Armando', 'R'], rps_game_winner([ ['Armando', 'R'], ['Dave', 'S'] ])
+ assert_equal ['Armando', 'S'], rps_game_winner([ ['Armando', 'S'], ['Dave', 'P'] ])
+ assert_equal ['Dave', 'P'], rps_game_winner([ ['Armando', 'R'], ['Dave', 'P'] ])
+ assert_equal ['Armando', 'P'], rps_game_winner([ ['Armando', 'P'], ['Dave', 'P'] ])
+ end
+
+ def test_rps_tournament_winner
+ tournament = [
+ [ [['Armando', 'P'], ['Dave', 'S']], [['Richard', 'R'], ['Michael', 'S']] ],
+ [ [['Allen', 'S'], ['Omer', 'P']], [['David E.', 'R'], ['Richard X.', 'P']] ]
+ ]
+
+ assert_equal ['Richard', 'R'], rps_tournament_winner(tournament)
+ end
+
+end
View
17 part3.rb
@@ -1,11 +1,24 @@
+class Array
+
+ def find_dups
+ inject(Hash.new(0)) { |h,e| h[e] += 1; h }.select { |k,v| v > 1 }.collect { |x| x.first }
+ end
+
+
+ # Based on hungryblank's version in the comments
+ # see http://www.ruby-forum.com/topic/122008
+
+ def find_dups2
+ uniq.select{ |e| (self-[e]).size < self.size - 1 }
+ end
def combine_anagrams(words)
- print anagrams = words.group_by { |word| word.downcase.split(//).sort }.values
+ print anagrams = words.group_by { |word| word.chars.sort }.values
end
-words = ['Cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams','scream']
+words = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams','scream']
combine_anagrams(words)
View
86 part5.rb
@@ -1,46 +1,60 @@
class Class
- def attr_accessor_with_history(attr_name)
- attr_name = attr_name.to_s # make sure it's a string
- attr_reader attr_name # create the attribute's getter
- attr_reader attr_name+"_history" # create bar_history getter
+ def attr_accessor_with_history(*attrs)
+
+
+ attrs.each do |attr|
- class_eval %Q{
-
- def #{attr_name}_history
- @#{attr_name}_history
- end
-
- def #{attr_name}_history=(value)
-
-
- @#{attr_name}_history = value
- end
-
- def #{attr_name}
- @#{attr_name}
- end
-
- def #{attr_name}=(value)
-
- @#{attr_name}_history << value
- @#{attr_name} = value
- end
-
-
-
- }
+ attr_name = attr.to_s # make sure it's a string
+ attr_reader attr_name # create the attribute's getter
+ attr_reader attr_name+"_history" # create bar_history getter
+
+
+ class_eval %Q{
+
+ @#{attr_name}
+ @#{attr_name}_history = Array.new
+ @#{attr_name}_history << @#{attr_name}
+
+
+ def #{attr_name}_history
+ @#{attr_name}_history
+ end
+
+ def #{attr_name}_history=(value)
+
+
+ @#{attr_name}_history = value
+ end
+
+ def #{attr_name}
+ @#{attr_name}
+ end
+
+ def #{attr_name}=(value)
+
+ @#{attr_name}_history << value
+ @#{attr_name} = value
+ end
+
+
+
+ }
+ end
end
-end
+ end
class Foo
- def initialize()
- @bar
- @bar_history = Array.new
- @bar_history << @bar
- end
+ attr_accessor_with_history :bar1
+ attr_accessor_with_history :bar
+
- attr_accessor_with_history :bar
end
+
+f = Foo.new
+f.bar = 1
+f.bar = 1
+#~ f.bar1 =1
+print f.bar_history
View
7 part5.rb~
@@ -9,7 +9,7 @@ class Class
class_eval %Q{
def #{attr_name}_history
- @#{attr_name}_history
+ @#{attr_name}_history
end
def #{attr_name}_history=(value)
@@ -46,6 +46,5 @@ class Foo
end
f = Foo.new
-f.bar = 1, "\n"
-f.bar = 2, "\n"
-f.bar_history, "\n" # => if your code works, should be [nil,1,2]
+
+print f.bar_history
View
52 part5_test.rb
@@ -2,16 +2,19 @@
require './part5.rb'
class FooTest
- def initialize()
- @teste
- @teste_history = Array.new
- @teste_history << @teste
- end
-
attr_accessor_with_history :teste
end
+
+ class FooMultiple
+ attr_accessor_with_history :bar1
+ attr_accessor_with_history :bar2
+ attr_accessor_with_history :bar3
+ end
class AccesorHistoryTest < Test::Unit::TestCase
+
+
+
def test_attr_accessor_with_history
f = Foo.new
f.bar = 1
@@ -79,7 +82,7 @@ def test_attr_accessor_with_new_class
end
- def test_attr_accessor_with_nil
+ def test_attr_accessor_with_nil
f = FooTest.new
@@ -89,6 +92,41 @@ def test_attr_accessor_with_nil
end
+ def test_attr_accessor_with_multiple_1
+
+ f = Foo.new
+ f.bar = "qwe"
+ f.bar = 1
+ f.bar = 4
+ f = Foo.new
+
+
+
+ assert_equal [nil], f.bar_history
+
+
+ end
+ def test_attr_accessor_with_multiple_attr_in_class
+
+ f = FooMultiple.new
+ f.bar1 = 1
+ f.bar2 = 2
+ f.bar3 = 1
+ f.bar1 = 1
+ f.bar2 = 2
+ f.bar3 = 1
+ f.bar1 = 1
+ f.bar2 = 2
+ f.bar3 = 1
+ f.bar1 = 1
+ f.bar2 = 2
+ f.bar3 = 1
+
+ assert_equal [nil,1,1,1], f.bar1_history
+ assert_equal [nil,2,2,2], f.bar2_history
+ assert_equal [nil,1,1,1], f.bar3_history
+
+ end
end
View
38 part5_test.rb~
@@ -2,14 +2,14 @@ require 'test/unit'
require './part5.rb'
class FooTest
- def initialize()
- @teste
- @teste_history = Array.new
- @teste_history << @teste
- end
-
attr_accessor_with_history :teste
end
+
+ class FooMultiple
+ attr_accessor_with_history :bar1
+ attr_accessor_with_history :bar2
+ attr_accessor_with_history :bar3
+ end
class AccesorHistoryTest < Test::Unit::TestCase
def test_attr_accessor_with_history
@@ -40,13 +40,18 @@ class AccesorHistoryTest < Test::Unit::TestCase
def test_attr_accessor_with_history_multiple
f = Foo.new
+ g = Foo.new
+
f.bar = 1
- #f.nombre = 'armando'
+ g.bar = "qwe"
+
f. bar = 4
+ g.bar = 3
f.bar_history
assert_equal [nil,1,4], f.bar_history
- #assert_equal [nil,'armando'], f.nombre_history
+ assert_equal [nil,"qwe",3], g.bar_history
+
end
@@ -74,7 +79,7 @@ class AccesorHistoryTest < Test::Unit::TestCase
end
- def test_attr_accessor_with_nil
+ def test_attr_accessor_with_nil
f = FooTest.new
@@ -84,6 +89,21 @@ class AccesorHistoryTest < Test::Unit::TestCase
end
+ def test_attr_accessor_with_multiple_1
+
+ f = Foo.new
+ f.bar = "qwe"
+ f.bar = 1
+ f.bar = 4
+ f = Foo.new
+
+
+
+ assert_equal [nil], f.bar_history
+
+
+ end
+
end
View
22 test.rb
@@ -0,0 +1,22 @@
+class Talker
+ def self.say(*args)
+ puts "Inside self.say"
+ puts "self = #{self}"
+ args.each do |arg|
+ method_name = ("say_" + arg.to_s).to_sym
+ send :define_method, method_name do
+ puts arg
+ end
+ end
+ end
+end
+
+class MyTalker < Talker
+ say :hello
+ say :qwe
+end
+
+m = MyTalker.new
+
+m.say_hello
+m.say_qwe
Please sign in to comment.
Something went wrong with that request. Please try again.