Skip to content

Commit f10bcb3

Browse files
Sarah AslanifarSarah Aslanifar
authored andcommitted
Add initial set drills
1 parent 07756d7 commit f10bcb3

12 files changed

Lines changed: 305 additions & 1 deletion

lib/ruby_drills/set/add_drill.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'set'
2+
class AddDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@hints = ["equivalent to <<",
7+
"equivalent to 'mereg'",
8+
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-add"]
9+
end
10+
11+
def show
12+
puts %{
13+
@set = #{@set.inspect}
14+
15+
Add number 7 to the set.
16+
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
17+
to add 0, 1, 7, and 8 to see what happens!
18+
}
19+
end
20+
21+
def reference
22+
"@set.add(7)"
23+
end
24+
25+
def valid?(input)
26+
input.include?("add") ||
27+
input.include?("<<") ||
28+
input.include?("merge")
29+
end
30+
31+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'set'
2+
class AddIfDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@hints = ["very similar to add, except that if the object exists, it will return nil, therefore you can use if in 'if' condition",
7+
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-add-3F"]
8+
end
9+
10+
def show
11+
puts %{
12+
@set = #{@set.inspect}
13+
14+
Add number 6 to the set, if it doesn't exist already.
15+
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
16+
to add 0, 2, 7, and 8 to see what happens!
17+
}
18+
end
19+
20+
def reference
21+
"@set.add?(6)"
22+
end
23+
24+
def valid?(input)
25+
input.include?("add?")
26+
end
27+
28+
end

lib/ruby_drills/set/clear_drill.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'set'
2+
class ClearDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@hints = ["This method is mutable and therefore not recommended!",
7+
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-clear"]
8+
end
9+
10+
def show
11+
puts %{
12+
@set = #{@set.inspect}
13+
Use a method that removes all the elements from the set (this method is mutable and therefore not recomanded, yet exists!).
14+
}
15+
end
16+
17+
def reference
18+
"@set.clear"
19+
end
20+
21+
def valid?(input)
22+
input.include?("clear")
23+
end
24+
25+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'set'
2+
class DeleteDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@hints = ["This method is mutable and it will return self",
7+
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-delete"]
8+
end
9+
10+
def show
11+
puts %{
12+
@set = #{@set.inspect}
13+
14+
Use a method to delete 6 form the set.
15+
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
16+
to delete 6, and 7 (doesn't exist in the set) to see what happens!
17+
}
18+
end
19+
20+
def reference
21+
"@set.delete(6)"
22+
end
23+
24+
def valid?(input)
25+
input.include?("delete")
26+
end
27+
28+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'set'
2+
class DeleteIfDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@hints = ["This method is mutable and it will return self if the delete is successful, otherwise nil if the object doesn't exist. For that reason you can use it in the 'if' condition!",
7+
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-delete-3F"]
8+
end
9+
10+
def show
11+
puts %{
12+
@set = #{@set.inspect}
13+
14+
Use a method to delete 7 form the set, if it exists. This method will return nil if the object doesn't exist in the set, therefore, you can use it in the 'if' condition.
15+
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
16+
to delete 6, and 7 if they exist (doesn't exist in the set) to see what happens!
17+
}
18+
end
19+
20+
def reference
21+
"@set.delete?(6)"
22+
end
23+
24+
def valid?(input)
25+
input.include?("delete?")
26+
end
27+
28+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'set'
2+
class DifferenceDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@another_set = Set.new([1, 2, 7, 9, 6])
7+
@hints = ["Alias for '-''",
8+
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-difference"]
9+
end
10+
11+
def show
12+
puts %{
13+
@set = #{@set.inspect}
14+
@another_set = #{@another_set.inspect}
15+
16+
Find the difference between @set and @another_set
17+
Bonus: Open irb, and initialize @set and @another_set, find the difference
18+
between @set and @anotherset and compare it with the difference between
19+
@another_set and @set!
20+
}
21+
end
22+
23+
def reference
24+
"@set.difference(@another_set)"
25+
end
26+
27+
def valid?(input)
28+
input.include?("difference") ||
29+
input.include?("-")
30+
end
31+
32+
end

lib/ruby_drills/set/each_drill.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'set'
2+
class EachDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@hints = ["http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-each"]
7+
end
8+
9+
def show
10+
puts %{
11+
@set = #{@set.inspect}
12+
For each element in the set, use "puts" to print twice that element into the console.
13+
}
14+
end
15+
16+
def reference
17+
"@set.each{ |s| puts s*2 }"
18+
end
19+
20+
def valid?(input)
21+
input.include?("each{")
22+
end
23+
24+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
require 'set'
3+
class IncludeDrill < Drill
4+
5+
def setup
6+
@set = Set.new([1, 2, 3, 4, 5, 6])
7+
@hints = ["http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-include-3F"]
8+
end
9+
10+
def show
11+
puts %{
12+
@set = #{@set.inspect}
13+
Use a method to find out if the set include 0?
14+
}
15+
end
16+
17+
def reference
18+
"@set.include?(0)"
19+
end
20+
21+
def valid?(input)
22+
input.include?("include?")
23+
end
24+
25+
end

lib/ruby_drills/set/merge_set.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'set'
2+
class MergeDrill < Drill
3+
4+
def setup
5+
@set = Set.new([1, 2, 3, 4, 5, 6])
6+
@another_set = Set.new([1, 2, 7, 9, 6])
7+
@hints = ["http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-merge"]
8+
end
9+
10+
def show
11+
puts %{
12+
@set = #{@set.inspect}
13+
@another_set = #{@another_set.inspect}
14+
15+
User a method to add all elements of @another_set to @set
16+
What happens to the duplicates?
17+
}
18+
end
19+
20+
def reference
21+
"@set.merge(@another_set)"
22+
end
23+
24+
def valid?(input)
25+
input.include?("merge")
26+
end
27+
28+
end

lib/ruby_drills/set/set_drills.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class SetDrills < Drills
2+
3+
def banner
4+
%{
5+
Ruby Drills: Sets
6+
7+
Set implements a collection of 'unordered' values with 'no duplicates'.
8+
This is a hybrid of Array's intuitive inter-operation facilities and Hash's fast lookup.
9+
10+
Set is easy to use with Enumerable objects (implementing each).
11+
Most of the initializer methods and binary operators accept generic Enumerable
12+
objects besides sets and arrays. An Enumerable object can be converted to Set using the to_set method.
13+
14+
Set uses Hash as storage, so you must note the following points:
15+
16+
Equality of elements is determined according to Object#eql? and Object#hash.
17+
Use #compare_by_identity to make a set compare its elements by their identity.
18+
Set assumes that the identity of each element does not change while it is stored.
19+
Modifying an element of a set will render the set to an unreliable state.
20+
When a string is to be stored, a frozen copy of the string is stored instead
21+
unless the original string is already frozen.
22+
------------------------------------------------------------------
23+
}
24+
end
25+
26+
end

0 commit comments

Comments
 (0)