Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
.idea/
31 changes: 16 additions & 15 deletions assignments/assignment01.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ def number_to_string(n, lang)

# it accepts a string
# and returns the same string with each word capitalized.
# assumes titleize does not need to preserve non-word characters in the original string, i.e.,
# titleize "hEllo, WORLD" => "Hello World"
def titleize(s)
words = s.split
caps = []
words.each do |word|
caps << word.capitalize
end
caps.join " "
word_array = s.split(/\W+/)
(word_array.map { |w| w.capitalize }).join(" ")
end

# Your method should generate the following results:
Expand All @@ -58,14 +56,16 @@ def titleize(s)
# Write your own implementation of `reverse` called `my_reverse`
# You may *not* use the built-in `reverse` method
def my_reverse(s)
output = ""
letters = s.split ""
n = letters.length
while n > 0
n -= 1
output << letters[n]
reversed_array = []

string_array = s.split("")
while not string_array.empty? do
# take a character from the end of the original string and
# add it to the beginning of the reversed string
reversed_array.push string_array.pop
end
output

reversed_array.join("")
end

# Your method should generate the following results:
Expand All @@ -80,8 +80,9 @@ def my_reverse(s)
# Write a method `palindrome?`
# that determines whether a string is a palindrome
def palindrome?(s)
stripped = s.delete(" ").delete(",").downcase
stripped == stripped.reverse
# remove all non-word characters, lowercase the string
canonical_string = s.gsub(/\W+/, '').downcase
canonical_string == canonical_string.reverse
end

# Your method should generate the following results:
Expand Down
Loading