From 389e5832f5b97ec7066bfc177782a85d5bbc34e4 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski <lisa.sano@gmail.com> Date: Wed, 27 Apr 2016 20:28:23 -0700 Subject: [PATCH] finished the exercise --- README.md | 13 +++++++-- recursion.rb | 76 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d7c61b3..0e244f5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,15 @@ Write a method factorial that accepts an integer parameter n and that uses recursion to compute and return the value of n factorial (also known as n!). ## fib(n) -Write a method fib that accepts an integer n as a parameter and returns the nth fibonacci number. +Write a method fib that accepts an integer n as a parameter and returns the nth fibonacci number. Adds the two numbers that came before it to get the next number. + +1 1 2 3 5 8 13 + +fib(1) = 1 +fib(2) = 1 +fib(3) = fib(2) + fib(1) +fib(4) = fib(3) + fib(2) + ## 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. @@ -12,6 +20,7 @@ Write a method pal that accepts a string s as a parameter and returns a boolean ## binary(n) Write a method binary that accepts an integer n as a parameter and that returns a string of all binary numbers that have n digits in ascending order. All n digits should be shown for all numbers, including leading zeros if necessary. You may assume that n is non-negative. If n is 0, an empty string should be returned. Do not use a loop in your solution; implement it recursively. + ## travel(x,y) Write a method travel that accepts integers x and y as parameters and uses recursive backtracking to return all solutions for traveling in the 2-D plane from (0, 0) to (x, y) by repeatedly using one of three moves: @@ -28,6 +37,6 @@ The following shows one such path to the point (5, 3). 5,1 5,2 5,3 -==> EEEEEENNN +==> EEEEENNN You may assume that the x/y values passed are non-negative. If x and y are both 0, return an empty string. diff --git a/recursion.rb b/recursion.rb index 27dcb41..dd8dd65 100644 --- a/recursion.rb +++ b/recursion.rb @@ -1,17 +1,82 @@ +# Lisa Rolczynski +# RC: n! = n * (n-1)! +# BC: 1! = 1 +# BC: 0! = 1 def fact(n) + if n == 1 || n == 0 + return 1 + end + + return n * fact(n-1) end +# BC: fib(1) = 1 +# BC: fib(2) = 1 def fib(n) + if n == 1 || n == 2 + return 1 + end + + fib(n-1) + fib(n-2) end +# palindromes +# BC: if there's 1 or 0 letters left, return true def pal(s) + s_array = s.chars + start_index = 0 + end_index = s_array.length - 1 + + pal_helper(s_array, start_index, end_index) end +def pal_helper(s_array, start_index, end_index) + return true if end_index - start_index <= 1 + + if s_array[start_index] == s_array[end_index] + return true && pal_helper(s_array, start_index+1, end_index-1) + else + return false + end +end + + 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 + + def travel(x,y) + travel_helper(x,y) +end + +def travel_helper(x,y,directions="",result="") + if x == 0 && y == 0 + result += directions + " " + return result + elsif x == 0 + return result = travel_helper(x,y-1,directions+"N",result) + elsif y == 0 + return result = travel_helper(x-1,y,directions+"E",result) + else + result = travel_helper(x-1,y,directions+"E",result) + result = travel_helper(x,y-1,directions+"N",result) + end + + return result end # Factorial Tests @@ -33,12 +98,13 @@ def travel(x,y) puts "passes all palindrome tests" # Binary Tests -raise "binary broke - binary(1)" unless binary(1) == "0 1" -raise "binary broke - binary(2)" unless binary(2) == "00 01 10 11" -raise "binary broke - binary(7)" unless binary(7) == "0000000 0000001 0000010 0000011 0000100 0000101 0000110 0000111 0001000 0001001 0001010 0001011 0001100 0001101 0001110 0001111 0010000 0010001 0010010 0010011 0010100 0010101 0010110 0010111 0011000 0011001 0011010 0011011 0011100 0011101 0011110 0011111 0100000 0100001 0100010 0100011 0100100 0100101 0100110 0100111 0101000 0101001 0101010 0101011 0101100 0101101 0101110 0101111 0110000 0110001 0110010 0110011 0110100 0110101 0110110 0110111 0111000 0111001 0111010 0111011 0111100 0111101 0111110 0111111 1000000 1000001 1000010 1000011 1000100 1000101 1000110 1000111 1001000 1001001 1001010 1001011 1001100 1001101 1001110 1001111 1010000 1010001 1010010 1010011 1010100 1010101 1010110 1010111 1011000 1011001 1011010 1011011 1011100 1011101 1011110 1011111 1100000 1100001 1100010 1100011 1100100 1100101 1100110 1100111 1101000 1101001 1101010 1101011 1101100 1101101 1101110 1101111 1110000 1110001 1110010 1110011 1110100 1110101 1110110 1110111 1111000 1111001 1111010 1111011 1111100 1111101 1111110 1111111" +raise "binary broke - binary(1)" unless binary(1) == "0 1 " +raise "binary broke - binary(2)" unless binary(2) == "00 01 10 11 " +raise "binary broke - binary(7)" unless binary(7) == "0000000 0000001 0000010 0000011 0000100 0000101 0000110 0000111 0001000 0001001 0001010 0001011 0001100 0001101 0001110 0001111 0010000 0010001 0010010 0010011 0010100 0010101 0010110 0010111 0011000 0011001 0011010 0011011 0011100 0011101 0011110 0011111 0100000 0100001 0100010 0100011 0100100 0100101 0100110 0100111 0101000 0101001 0101010 0101011 0101100 0101101 0101110 0101111 0110000 0110001 0110010 0110011 0110100 0110101 0110110 0110111 0111000 0111001 0111010 0111011 0111100 0111101 0111110 0111111 1000000 1000001 1000010 1000011 1000100 1000101 1000110 1000111 1001000 1001001 1001010 1001011 1001100 1001101 1001110 1001111 1010000 1010001 1010010 1010011 1010100 1010101 1010110 1010111 1011000 1011001 1011010 1011011 1011100 1011101 1011110 1011111 1100000 1100001 1100010 1100011 1100100 1100101 1100110 1100111 1101000 1101001 1101010 1101011 1101100 1101101 1101110 1101111 1110000 1110001 1110010 1110011 1110100 1110101 1110110 1110111 1111000 1111001 1111010 1111011 1111100 1111101 1111110 1111111 " +puts "passes all binary tests" # Travel Tests -raise "travel broke - travel(1,2)" unless travel(1,2) == "ENN NEN NNE" -raise "travel broke - travel(2,2)" unless travel(2,2) == "EENN ENEN ENNE NEEN NENE NNEE" +raise "travel broke - travel(1,2)" unless travel(1,2) == "ENN NEN NNE " +raise "travel broke - travel(2,2)" unless travel(2,2) == "EENN ENEN ENNE NEEN NENE NNEE " puts "All test passed"