Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

palindrome #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Write a method factorial that accepts an integer parameter n and that uses recur

## fib(n)
Write a method fib that accepts an integer n as a parameter and returns the nth fibonacci number.
fib(4) = 3
1 1 3 5 8 13
fib(1) == 1
fib(2) == 1


## pal(s)
Write a method pal that accepts a string s as a parameter and returns a boolean value indicating if that string is a palindrome or not.
Expand Down
34 changes: 33 additions & 1 deletion recursion.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@

def fact(n)
if n == 0 || n == 1
return 1
end

return n * fact(n-1)
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good solution.


def fib(n)
if n == 1 || n == 2
return 1
end

return fib(n-1) + fib(n-2)
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good solution.


def pal(s)
if s.length == 0 || s.length == 1
return true
end

if s.chars[0] == s.chars[s.length-1]
s.slice!(0)
s.slice!(s.length-1)
pal(s)
else
return false
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good solution! This implementation is readable and easy to follow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@malderi Thank you for reviewing my code!


def binary(n)
binary_helper(n)
end

def binary_helper(digits, binary="", result="")
if digits == 0
result += binary + " "
return result
else
result = binary_helper(digits-1,binary+"0", result)
result = binary_helper(digits-1,binary+"1", result)
end
return result
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good solution.


def travel(x,y)
Expand Down