Skip to content

Instantly share code, notes, and snippets.

@altherlex
Created March 24, 2015 18:58
Show Gist options
  • Save altherlex/899ba969764e5929abd6 to your computer and use it in GitHub Desktop.
Save altherlex/899ba969764e5929abd6 to your computer and use it in GitHub Desktop.
Exercise solve for ruby beginners
# encoding: UTF-8
# Exercise description: http://goo.gl/2XsKhW
require 'minitest'
require "minitest/autorun"
############### Exercise 3 ##############
class Array
def combine_anagrams
self.group_by{|el| el.downcase.chars.sort}.values
end
end
describe Array do
describe "For Array#combine_anagrams" do
it "should have instance method combine_anagrams for Array class" do
[].methods.select{|i| i.to_s == "combine_anagrams"}.empty?.must_equal false
end
it "I should to see permutation for a word in words array" do
lst = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream']
lst.combine_anagrams.must_equal([["cars", "racs", "scar"], ["for"], ["potatoes"], ["four"], ["creams", "scream"]])
end
end
end
# encoding: UTF-8
# Exercise description: http://goo.gl/2XsKhW
require 'minitest'
require "minitest/autorun"
###### Exercise 4 ##############
class Dessert
attr_accessor :name, :calories
def initialize(name, calories)
self.name = name
self.calories = calories
end
def healthy?
self.calories < 200
end
def delicious?
true
end
end
class JellyBean < Dessert
attr_accessor :name, :calories, :flavor
def initialize(name, calories, flavor)
self.name = name
self.calories =calories
self.flavor = flavor
end
def delicious?
self.flavor != "black licorice"
end
end
#test4
describe Dessert do
before{
@gelatina = Dessert.new("gelatina", 100)
@pudim = Dessert.new("Pudim", 300)
@jujuba = JellyBean.new('jujuba', 250, "black licorice")
}
describe "Dessert class" do
it "healthy? should to respond false for many calories (til 200kal)" do
@gelatina.healthy?.must_equal true
@pudim.healthy?.must_equal false
end
end
describe "JellyBean class" do
it "flavor should to respond FALSE for 'black licorice' taste" do
@jujuba.delicious?.must_equal false
@jujuba.flavor = "coco"
@jujuba.delicious?.must_equal true
end
end
end
# encoding: UTF-8
# Exercise description: http://goo.gl/2XsKhW
require 'minitest'
require "minitest/autorun"
######### exercise 2 ##############
module JanKenPo
class WrongNumberOfPlayerError < StandardError; end
class NoSuchStrangyError < StandardError; end
#PE : Pedra
#PA: Papel
#TE: Tesoura
RN = [["PE,TE", :first], ["TE,PE", :last],["PA,PE", :first],["PE,PA", :last],["TE,PA", :first],["PA,TE", :last]]
VALIDS_GAMES = ["PE","PA","TE"]
def self.play(arr_players_and_game)
raise WrongNumberOfPlayerError unless arr_players_and_game.size==2
# It's need two parameters for first player
raise WrongNumberOfPlayerError unless arr_players_and_game.first.size==2
# It's need two parameters for second player
raise WrongNumberOfPlayerError unless arr_players_and_game.last.size==2
raise NoSuchStrangyError unless arr_players_and_game.map{|i| i.last.upcase}.detect{|i| !VALIDS_GAMES.include?(i) }.nil?
result_method = RN.detect{|i| i.first == arr_players_and_game.map{|i| i.last.upcase}.join(',')}.last
arr_players_and_game.send result_method
end
end
### Teste2
describe JanKenPo do
before{
@players1 = [["Armando", "PA"], ["Justen", "te"]] # TE > PA
@players2 = [["Armando", "PE"], ["Justen", "TE"]] # PE > TE
@players3 = [["Armando", "Pa"], ["Justen", "Pe"]] # PA > PE
}
describe "just a game" do
it "Tesoura winns Papel" do
JanKenPo.play(@players1).must_equal ["Justen", "te"]
end
it "Pedra winns Tesoura" do
JanKenPo.play(@players2).must_equal ["Armando", "PE"]
end
it "Papel winns Pedra" do
JanKenPo.play(@players3).must_equal ["Armando", "Pa"]
end
end
end
# encoding: UTF-8
# Exercise description: http://goo.gl/2XsKhW
require 'minitest'
require "minitest/autorun"
############## Exercise 1 ##############
class String
def palindrome?
str = self.split(/\W+/).join('')
str.reverse.downcase == str.downcase
end
def count_words
lst = self.split(/\W+/).map(&:downcase)
lst.inject({}){|ac, i| ac.merge!({i.downcase=>lst.count(i)}); ac}
end
end
describe String do
describe "about String's overwrite" do
it "should to respond positively for instance methods" do
'teste'.methods.select{|i| i.to_s == "count_words"}.empty?.must_equal false
'teste'.methods.select{|i| i.to_s == "palindrome?"}.empty?.must_equal false
end
end
describe "when check if string's palindrome" do
it "should to respond positively for 'amor a -- roma'" do
'amor a -- roma'.palindrome?.must_equal true
end
it "should to respond positively for upper and lower case like: 'A man, a plan, a canal -- Panama'" do
'A man, a plan, a canal -- Panama'.palindrome?.must_equal true
end
it "should to respond negatively for: 'abracadabra'" do
'abracadabra'.palindrome?.must_equal false
end
end
describe "when I confere words'number in a phrase and return a hash" do
it "should to respond for: 'amor a roma'" do
'amor a roma roma'.count_words.must_equal({'amor'=>1, 'a'=>1, 'roma'=>2})
end
it "should to respond for: 'A man, a plan, a canal -- Panama'" do
'A man, a plan, a canal -- Panama'.count_words.sort.must_equal({'a'=>3, 'man'=>1, 'canal'=>1, 'panama'=>1, 'plan'=>1}.sort)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment