Skip to content

Instantly share code, notes, and snippets.

@brittanmcg
Created December 10, 2013 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brittanmcg/7884072 to your computer and use it in GitHub Desktop.
Save brittanmcg/7884072 to your computer and use it in GitHub Desktop.
The code from my 1.2 pairing with Zohar and Maria.
# Compare Fibonacci!
# At this point you should be good at refactoring your code to make it better,
# but do you feel comfortable determining "good" vs. "bad" code? In this exercise
# you will evaluate four different solutions to the same challenge and evaluate
# their clarity, effectiveness, and overall "good"ness.
# By the end of this challenge, you should be able to:
# 1) Confidently read other people's code and figure out how it works
# 2) Recognize good and not so good practices
# 3) Consider ways to improve solutions
# For each solution below:
# 1. Read each line of code. Write a comment above each method that explains what
# it's doing and how.
# 2. Is this an iterative or recursive solution? How can you tell?
# 3. Consider the variable and method names. What do you think about them?
# Could they be more clear?
# 4. What do you think of this code overall? Do you think it's an elegant solution?
# Why or why not?
# 5. How would you improve this solution?
# 6. Write your comments together and submit it here: https://socrates.devbootcamp.com/challenges/445
# 7. Submit feedback for your session on feedbackinator!
# Your Names:
# 1) Brittan McGinnis
# 2) Zohar Liran
############################################################
############################################################
# Solution 1
# Iterative or Recursive?
# Iterative
# Method Description
# Calling the generate method from below and asking if it includes the argument.
#
def is_fibonacci?(num)
generate_fibonacci(num).include?(num)
end
# Method Description
# This method is generating a fibonacci sequence and returning it as an array.
# It is starting from the base_num variable up to num.
def generate_fibonacci(end_num, base_num=1)
fibonacci_sequence = [0,1]
while base_num <= end_num
puts "i entered the while loop and base_num = #{base_num}"
fibonacci_sequence << base_num
puts "I just added #{base_num} to the fib_sequence"
base_num += fibonacci_sequence[-2]
end
return fibonacci_sequence
end
# Naming
# Overall we think it's pretty good and descriptive with the exception of
# changing the num argument to end_num.
# Overall solution
# It's pretty good except for it leaves room to use up unecessary memory because
# The user could put in more numbers than needed.
# Suggested improvements?
# We feel that since there is no use in the entire array we can reduce the
# memory use to just one variable that will replace fibonacci_sequence
#
############################################################
############################################################
# Solution 2
# Iterative or Recursive?
# Iterative
# Method Description
# We have an array with 0 and 1 and a counter that starts at 0 then we are
# looping through 500 times and adding the first 500 fibonacci numbers into the
# array. Then we are returning the boolean value which determines if the
# number that was passed is in the array.
def is_fibonacci?(i)
fib_array = [0, 1]
counter = 0
500.times do
fib_num = fib_array[counter] + fib_array[counter+1]
fib_array << fib_num
counter += 1
end
fib_array.include?(i)
end
# Naming
# Naming is not good because it doesn't allow for changes to be easily implemented
#
# Overall solution
# This is a poor solution because it runs 500 times whether you want it to or not
#
# Suggested improvements?
# Change naming and check for the argument so that the running of the loop would
# not be a fixed number.
#
############################################################
############################################################
# Solution 3
# Iterative or Recursive?
# neither
# Method Description
# the method uses the math algorithm that determines if a naumber is
# one of the Fibonacci sequence
def is_fibonacci?(i)
n1 = 5 * i**2 + 4
n2 = n1 - 8
is_square?(n1) or is_square?(n2)
end
# find floor(sqrt(i)) by nesting intervals
def sqrt(i)
a, b = 0, i
while a + 1 < b
m = (a + b) / 2
if m**2 > i
b = m
else
a = m
end
end
a
end
# Method Description
# The method receives a number and determines if it is a perfect square
# it is done by calling a function that calculates the integer square root
def is_square?(i)
s = sqrt(i)
s ** 2 == i
end
# Naming
# The naming here is too short. For example, instead of m we can use avg
# to describe the meaning of the variables better
# Overall solution
# The solution is very efficient
#
# Suggested improvements?
# we can have just one line saying sqrt(i) ** 2 == i
# there was no use in s
#
############################################################
############################################################
# Solution 4
# Iterative or Recursive?
# recursive
# Method Description
# returns true if the number is in the fibonacci sequence
# Starts off from 1 and 0 in the sequence and then calculates all the numbers
# until we get or pass the number that was given in the arg.
def is_fibonacci?(i, current = 1, before = 0)
return true if current == i || i == 0
return false if current > i
is_fibonacci?(i, current + before, current)
end
# Naming
# We think that the naming here is appropriate.
#
# Overall solution
# We think that it is elegant, concise and to the point.
#
# Suggested improvements?
# This solution leaves open the door for error if someone passes in the wrong
# number of arguments.
# This could be solved by creating a method that does the calling of more arguments
# and performs the above recursion.
############################################################
############################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment