-
Notifications
You must be signed in to change notification settings - Fork 24
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
Recursion Homework #21
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,71 @@ | ||
|
||
|
||
# RC: n! = n * ( n - 1 )! | ||
# BC: 1! = 1 | ||
# BC: 0! = 1 | ||
|
||
def fact(n) | ||
#BC: | ||
if n == 0 || n == 1 | ||
return 1 | ||
end | ||
#RC: | ||
return n * fact(n-1) | ||
end | ||
|
||
# adds the number before it: | ||
# 1 1 2 3 5 8 13 24 | ||
# fib(4) = 3 | ||
# fib(1) = 1 | ||
# fib(2) = 1 | ||
# fib(3) = fib(2) + fib(1) | ||
# fib(4) = fib(3) + fib(2) | ||
# fib(6) = fib(5) + fib(4) | ||
|
||
def fib(n) | ||
#BC: | ||
if n == 1 || n == 2 | ||
return 1 | ||
end | ||
#RC: | ||
return fib(n-1) + fib(n-2) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good solution. |
||
|
||
|
||
# do this for homework! | ||
def pal(s) | ||
i = 0 | ||
s.chars.length | ||
while i <= s / 2 | ||
if s[i] != s[length - 1 - i] | ||
return false | ||
i += 1 | ||
return true | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
def binary(n) | ||
binary_helper(n, "", "") | ||
end | ||
|
||
def binary_helper(digits, binary, result) | ||
#BC: | ||
if digits == 0 | ||
result += binary + " " | ||
else | ||
#RC: | ||
result = binary_helper(digits - 1, binary + "0", result) | ||
result = binary_helper(digits - 1, binary + "1", result) | ||
end | ||
return result | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good solution! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for checking out my code! On Sun, May 15, 2016 at 9:55 PM, malderi [email protected] wrote:
|
||
|
||
puts binary(2) | ||
|
||
#optional | ||
def travel(x,y) | ||
end | ||
|
||
|
@@ -19,6 +74,11 @@ def travel(x,y) | |
raise "factorial broke - fact(0)" unless fact(0) == 1 | ||
puts "passes all factorial 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 " | ||
|
||
# Fibanocci Tests | ||
raise "fib broke - fib(8)" unless fib(8) == 21 | ||
raise "fib broke - fib(20)" unless fib(20) == 6765 | ||
|
@@ -32,13 +92,7 @@ def travel(x,y) | |
raise "pal broke - pal('')" unless pal("") == true | ||
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" | ||
|
||
# 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" | ||
|
||
puts "All test passed" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good solution.