Skip to content

Instantly share code, notes, and snippets.

@RickArora

RickArora/.rb Secret

Last active December 25, 2018 18:59
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/d7b582815ba6d8170061533ecc692f69 to your computer and use it in GitHub Desktop.
Save RickArora/d7b582815ba6d8170061533ecc692f69 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

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

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