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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Write a method `factorial` that accepts an integer parameter n and that uses rec

- e.g. fact(4) = 4 * 3 * 2 * 1 = 24


### Write #2
`reverse(s)`
Write a method `reverse` that accepts a string as a parameter
Expand Down
122 changes: 117 additions & 5 deletions recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,125 @@

def factorial(n)
raise ArgumentError.new("Not an integer") if
!(n.class==Integer)
raise ArgumentError.new("Not a non-negative integer") if n < 0
if n==0
return 1
else
return n * factorial(n-1)
end
end


#with tail recursion (just for fun):
def factorialB(n, total=1)
raise ArgumentError.new("Not an integer") if
!(n.class==Integer)
raise ArgumentError.new("Not a non-negative integer") if n < 0

if n==0
return total
else
return factorialB(n-1, n * total)
end
end


def reverse(s)
if s.nil? || s.length==0
return ""
else
return reverse(s[1..-1])+s[0]
end
end


def bunny(n)
raise ArgumentError.new("Not an integer") if
!(n.class==Integer)
raise ArgumentError.new("Not a non-negative integer") if n < 0
if n==0
return 0
else
return bunny(n-1) + 2
end
end

def nested(s)

#Accepts ()() as acceptable 'nestings' of parentheses.
def nested(s, counter = 0)
s.each_char do |paren|
raise ArgumentError.new("Includes non-paren characters") if !['(',')'].include?(paren)
end

if s.length == 0
(counter == 0)? true:false
elsif s[0]=='('
counter += 1
return nested(s[1..-1], counter)
elsif s[0]==')'
counter -= 1
if counter < 0
return false
else
return nested(s[1..-1], counter)
end
end
end


# Only accepts strictly nested strings of parenteses.
# Ex ()() will count as false.
def strictly_nested(s, counter = 0)
s.each_char do |paren|
raise ArgumentError.new("Includes non-paren characters") if !['(',')'].include?(paren)
end

if s.length == 0
(counter == 0)? true:false
elsif s[0]=='('
counter += 1
return strictly_nested(s[1..-1], counter)
elsif s[0]==')'
counter -= 1
#Since we only want strictly nested parentheses [ie don't want to allow ()() ], if counter hits 0 and there are more characters in the string, the method should return false.
if counter == 0 && s.length != 1
return false
else
return strictly_nested(s[1..-1], counter)
end
end
end


####### Added Fun

def fib(n)
raise ArgumentError.new("Not an integer") if
!(n.class==Integer)
raise ArgumentError.new("Not a positive integer") if n < 0
if n == 1
return 1
elsif n == 2
return 1
else
return fib(n-1) + fib(n-2)
end
end


def pal(s)
if (s.length == 1) || (s.length == 0)
return true
elsif s[0] == s[-1]
return pal(s[1..-2])
else
return false
end
end



# Factorial Tests
raise "factorial broke - factorial(4)" unless factorial(4) == 24
raise "factorial broke - factorial(0)" unless factorial(0) == 1
Expand All @@ -26,9 +135,12 @@ def nested(s)
raise "bunny broke - bunny(10)" unless bunny(10) == 20
puts "passes all bunny tests"

# Nested Tests
raise "nested broke - nested('((()))')" unless reverse("((()))") == true
raise "nested broke - nested('())')" unless reverse("())") == false
puts "passes all nested tests"
# # Nested Tests
# raise "nested broke - nested('((()))')" unless reverse("((()))") == true
# raise "nested broke - nested('())')" unless reverse("())") == false
# puts "passes all nested tests"

puts 'typo in the nested tests. written as reverse instead of nested'


puts "All test passed"
23 changes: 23 additions & 0 deletions sandbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Only accepts strictly nested strings of parenteses.
# Ex ()() will count as false.

def strictly_nested(s, counter = 0)
s.each_char do |paren|
raise ArgumentError.new("Includes non-paren characters") if !['(',')'].include?(paren)
end

if s.length == 0
(counter == 0)? true:false
elsif s[0]=='('
counter += 1
return strictly_nested(s[1..-1], counter)
elsif s[0]==')'
counter -= 1
#Since we only want strictly nested parentheses [ie don't want to allow ()() ], if counter hits 0 and there are more characters in the string, the method should return false.
if counter == 0 && s.length != 1
return false
else
return strictly_nested(s[1..-1], counter)
end
end
end