Skip to content

Instantly share code, notes, and snippets.

@RickArora
Created December 25, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickArora/fed3164ec37936395d882c02885ae53b to your computer and use it in GitHub Desktop.
Save RickArora/fed3164ec37936395d882c02885ae53b to your computer and use it in GitHub Desktop.
# Write a method that capitalizes each word in a string like a book title
# Do not capitalize words like 'a', 'and', 'of', 'over' or 'the'.
LITTLE_WORDS = [
"and",
"the",
"over",
"of",
"a"
]
def titleize(title)
words = title.split(" ")
titleized_string = ""
titleized_words = words.map.each_with_index do |word, i|
if (LITTLE_WORDS.include?(word) && i > 0)
titleized_string = titleized_string + word.downcase + " "
else
titleized_string = titleized_string + word.capitalize + " "
end
end
return titleized_string.rstrip
end
# Write a method that returns the largest prime factor of a given integer.
def prime?(num)
(2..num-1).none? { |factor| num % factor == 0 }
end
def largest_prime_factor(num)
if (num == 0 || num == 1)
return nil
end
num.downto(2) do |factor|
if (num % factor).zero?
return factor if prime?(factor)
end
end
end
# Write a symmetric_substrings method that takes a string and returns an array
# of substrings that are palindromes, e.g. symmetric_substrings("cool") => ["oo"]
# Only include substrings of length > 1.
def symmetric_substrings(str)
symm_subs = ""
str.length.times do |start_pos|
(2..(length - start_pos).each do |len|
substr = str[start_pos...(start_pos + len)]
symm_subs << substr if substr = substr.reverse
end
end
symm_subs
end
# Write a method that returns `true` if all characters in the string
# are unique and `false` if they are not.
def all_unique_chars?(str)
letters = str.split('').reject{ |char| char == ' '}
letters.uniq.length = letters.length
end
@RickArora
Copy link
Author

require "rspec"
require "exercises"

describe "titleize" do
it "capitalizes a word" do
expect(titleize("jaws")).to eq("Jaws")
end

it "capitalizes every word (aka title case)" do
expect(titleize("david copperfield")).to eq("David Copperfield")
end

it "doesn't capitalize 'little words' in a title" do
expect(titleize("war and peace")).to eq("War and Peace")
end

it "does capitalize 'little words' at the start of a title" do
expect(titleize("the bridge over the river kwai")).to eq("The Bridge over the River Kwai")
end
end

describe 'largest_prime_factor' do
it 'returns 2 for 2' do
expect(largest_prime_factor(2)).to eq(2)
end

it 'returns 5 for 10' do
expect(largest_prime_factor(10)).to eq(5)
end
it 'can detect larger factors' do
expect(largest_prime_factor(152)).to eq(19)
end
it 'can detect even larger factors' do
expect(largest_prime_factor(2098)).to eq(1049)
end

it "returns original number if it is prime" do
expect(largest_prime_factor(13)).to eq(13)
end

it 'returns nil for zero and one' do
expect(largest_prime_factor(0)).to be_nil
expect(largest_prime_factor(1)).to be_nil
end
end

describe "symmetric_substrings" do
it "handles a simple example" do
expect(symmetric_substrings("aba")).to match_array(["aba"])
end

it "handles two substrings" do
expect(symmetric_substrings("aba1cdc")).to match_array(["aba", "cdc"])
end

it "handles nested substrings" do
expect(symmetric_substrings("xabax")).to match_array(["aba", "xabax"])
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment