Skip to content

Instantly share code, notes, and snippets.

@fzrhrs
Last active August 29, 2015 14:06
Show Gist options
  • Save fzrhrs/cdcc8249333b7fce34b4 to your computer and use it in GitHub Desktop.
Save fzrhrs/cdcc8249333b7fce34b4 to your computer and use it in GitHub Desktop.
require 'rspec'
class Anagram
end
def ac(a,b)
if a.length == b.length
if a.split('').sort == b.split('').sort
true
else
false
end
else
false
end
end
describe Anagram do
it 'checks if A and B is anagram' do
a = "nama"
b = "aman"
expect(ac(a,b)).to eq true
end
it 'checks if A and C is anagram' do
a = "nama"
c = "hello"
expect(ac(a,c)).to eq false
end
it 'checks if A and D is anagram' do
a = "nama"
d = "hell"
expect(ac(a,d)).to eq false
end
it 'checks if A and E is anagram' do
a = "namakuraja"
e = "rajanamaku"
expect(ac(a,e)).to eq true
end
end
require 'rspec'
class Palindrome
end
def palindrome?(word)
if word == word.reverse
true
else
false
end
end
describe Palindrome do
it 'checks if A is palindrome' do
a = 'katak'
expect(palindrome?(a)).to eq true
end
it 'checks if B is palindrome' do
b = 'kaya'
expect(palindrome?(b)).to eq false
end
it "should tell if " do
expect(palindrome?('detartrated')).to eql true
end
end
require "rspec"
class Sort
end
def sort arry
sorted = []
arry.each do |item|
if sorted.empty?
sorted << item
else
recursive_sort sorted, item
end
end
sorted
end
def recursive_sort sorted, item
if sorted[-1] < item
sorted << item
else
prev = sorted[-1]
sorted[-1] = item
recursive_sort sorted, prev
end
end
describe Sort do
before do
@array_1 = [1,3,2]
@array_2 = [1,3,2,4]
@array_3 = [3,1,5,7,9,13,21,70,111]
end
it "sort @array_1" do
expect(sort(@array_1)).to eql [1,2,3]
end
it "sort @array_2" do
expect(sort(@array_2)).to eql @array_2.sort
end
it "sort @array_3" do
expect(sort(@array_3)).to eql @array_3.sort
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment