Last active
February 21, 2019 08:19
-
-
Save rskelley9/a5639ed47aca2d3ba9ebbff87fd6e3c8 to your computer and use it in GitHub Desktop.
Demonstrate concepts of recursion and blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def my_map | |
new_arr = [] | |
for el in self | |
new_arr.push(yield(el)) | |
end | |
new_arr | |
end | |
def my_map! | |
ix = 0 | |
for el in self | |
self[ix] = yield(el) | |
ix += 1 | |
end | |
self | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def my_map | |
new_hash = {} | |
self.each do |k, v| | |
new_hash[k] = yield(k, v) | |
end | |
new_hash | |
end | |
def my_map! | |
self.each do |k,v| | |
self[k] = yield(k, v) | |
end | |
self | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_pal?(str) | |
return true if str.strip.length <= 1 | |
str_array = str.scan(/\w/) | |
str_next = str_array[1..-2].join | |
return false unless str_array.first == str_array.last | |
is_pal?(str_next) | |
end | |
is_pal?('racecar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment